Skip to content

Commit 0634e21

Browse files
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

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

README.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ Generated code will be placed in the Gradle build directory.
414414

415415
The default behavior is `keys_json`, i.e. both will be camel cased, and `json_name` will be used if set.
416416

417+
- With `--ts_proto_opt=protoJsonFormat=true` (default), the `toJSON` and `fromJSON` methods follow the [protoJSON format](https://protobuf.dev/programming-guides/json). This means `toJSON` will output standard lowerCamelCase keys (or custom `json_name` if set). `fromJSON` will accept both the lowerCamelCase key (or `json_name`) AND the original proto field name.
418+
With `--ts_proto_opt=protoJsonFormat=false`, strict proto3 compliance is disabled. The JSON keys will simply follow the `snakeToCamel` option, and `fromJSON` will only accept that specific format.
419+
417420
- With `--ts_proto_opt=outputEncodeMethods=false`, the `Message.encode` and `Message.decode` methods for working with protobuf-encoded/binary data will not be output.
418421

419422
This is useful if you want "only types".

integration/angular/simple-message.ts

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/avoid-import-conflicts-folder-name/ui/simple.ts

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/avoid-import-conflicts/simple.ts

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/before-after-request-streaming/example.ts

Lines changed: 20 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/before-after-request/simple.ts

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/emit-default-values-json/test.ts

Lines changed: 64 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/fieldmask/fieldmask.ts

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/file-suffix/parent.pb.ts

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/from-partial-no-initialize/test.ts

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)