Skip to content

Conversation

@guglielmo-san
Copy link
Contributor

@guglielmo-san guglielmo-san commented Jan 13, 2026

The Protobuf JSON Mapping specification 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:

message Message {
  string my_field = 1;
  string second_field = 2 [json_name = "myCustomName"];
}

Generated fromJSON:

  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

README.markdown Outdated

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

- With `--ts_proto_opt=protoJsonFormat=true`, the `fromJSON` method will accept both the `json_name` (or camelCased name) and the original proto field name (often snake_cased), without duplication of the field if the two names are identical. This option also implies `snakeToCamel=json` to ensure that standard JSON keys are generated in lowerCamelCase (or use `json_name`), as mandated by the spec.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @guglielmo-san ; thanks for the PR, this looks great!

Given you're making ts-proto more spec compliant, I'm kinda wondering if we should default this new option to true, and then allow users who really actively don't want the fallback/spec behavior, to turn it off by setting =false.

Particularly since you made the output conditional on fieldName !== jsonName, which I really appreciate it.

Does that sound good to you?

Otherwise I think just a small diff error in the CI build, and once that is fixed I'll hit merge -- thanks again!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the hassle here @guglielmo-san , but with the default flipped, it looks like all the codegen files will need pushed into the PR -- sorry this can seem like a pita, but its a form our of "regression tests" to see how each PR affects the generated code output, given that is our primary deliverable. :-)

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stephenh , yes it is a good idea to have the protoJsonFormat as default behavior. I updated the value and the README and generated again all the .ts files in the integration tests.
Now the workflows should pass successfully.

@stephenh
Copy link
Owner

This is great @guglielmo-san , hitting merge -- thanks again!

@stephenh stephenh merged commit 0634e21 into stephenh:main Jan 14, 2026
6 checks passed
stephenh pushed a commit that referenced this pull request Jan 14, 2026
# [2.11.0](v2.10.1...v2.11.0) (2026-01-14)

### Features

* add protoJsonFormat option to support standard Protobuf JSON message ([#1241](#1241)) ([0634e21](0634e21)), closes [#1012](#1012)
@stephenh
Copy link
Owner

🎉 This PR is included in version 2.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fromJSON should accept both snake_case and camelCase keys

2 participants