Commit 0634e21
authored
feat: add protoJsonFormat option to support standard Protobuf JSON message (#1241)
The [Protobuf JSON Mapping
specification](https://protobuf.dev/programming-guides/json/#:~:text=Format%20Description-,Representation%20of%20each%20type,-The%20following%20table)
requires that messages parsers accept both the lowerCamelCase name (or
explicit json_name) and the original proto field name (often snake_case)
when parsing JSON. Before this change, ts-proto's fromJSON generated
method only supported one format based on the snakeToCamel
configuration, typically prioritizing camelCase.
**Mechanism**: This PR introduces a new boolean option: protoJsonFormat.
When enabled:
- snakeToCamel includes json, enforcing typical camelCase keys for
output (toJSON).
- fromJSON is enhanced to check for both the standard JSON name
(camelCase or json_name) and the original proto field name.
- If the two names are identical, no extra check is generated to avoid
code bloat.
Example:
``` protobuf
message Message {
string my_field = 1;
string second_field = 2 [json_name = "myCustomName"];
}
```
Generated fromJSON:
```typescript
fromJSON(object: any): Message {
return {
myField: isSet(object.myField)
? globalThis.String(object.myField)
: isSet(object.my_field)
? globalThis.String(object.my_field)
: "",
secondField: isSet(object.myCustomName)
? globalThis.String(object.myCustomName)
: isSet(object.second_field)
? globalThis.String(object.second_field)
: "",
};
}
```
Changes:
- Added protoJsonFormat to Options.
- Updated src/main.ts fromJSON generation generation logic to include
fallback che
- Added comprehensive integration tests in
integration/proto-json-format/.
Fixes #1012
This change is required to adopt the ts-proto library in the [A2A-js
project](https://github.com/a2aproject/a2a-js)1 parent 6b095e8 commit 0634e21
File tree
80 files changed
+2406
-398
lines changed- integration
- angular
- avoid-import-conflicts-folder-name/ui
- avoid-import-conflicts
- before-after-request-streaming
- before-after-request
- emit-default-values-json
- fieldmask
- file-suffix
- from-partial-no-initialize
- grpc-js/google/protobuf
- grpc-web-abort-signal
- grpc-web-go-server
- grpc-web-no-streaming-observable
- grpc-web-no-streaming
- grpc-web
- import-mapping
- import-suffix
- nested-repeated-values-optional-all
- nice-grpc/google/protobuf
- no-comments
- oneof-properties
- oneof-unions-snake
- google/protobuf
- oneof-unions-value
- google/protobuf
- oneof-unions
- google/protobuf
- optional-type-definitions
- proto-json-format
- proto2-long
- proto2-no-default-vals
- proto2-no-optionals
- proto2
- simple-deprecated-fields
- simple-json-name
- simple-optionals
- import_dir
- simple-prototype-defaults
- import_dir
- simple-snake
- google/protobuf
- import_dir
- simple-string-enums/google/protobuf
- simple-unrecognized-enum
- import_dir
- simple
- import_dir
- static-only-type-registry/google/protobuf
- static-only/google/protobuf
- struct/google/protobuf
- suffix-type
- google/protobuf
- suffix-usedate
- test-1110
- type-annotations/google/protobuf
- type-registry/google/protobuf
- use-date-false
- use-date-string
- use-date-true
- use-json-name
- use-json-timestamp-raw
- use-map-type
- google/protobuf
- use-numeric-enum-json/google/protobuf
- use-objectid-true-external-import
- use-optionals-all
- google/protobuf
- use-optionals-deprecated-only
- use-optionals-no-undefined
- google/protobuf
- use-readonly-types/google/protobuf
- value/google/protobuf
- vector-tile
- src
- tests
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
80 files changed
+2406
-398
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
414 | 414 | | |
415 | 415 | | |
416 | 416 | | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
417 | 420 | | |
418 | 421 | | |
419 | 422 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments