Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON serialization options #737

Closed
nicole0707 opened this issue Mar 6, 2024 · 2 comments · Fixed by #741
Closed

JSON serialization options #737

nicole0707 opened this issue Mar 6, 2024 · 2 comments · Fixed by #741

Comments

@nicole0707
Copy link
Contributor

From the docs https://github.com/bufbuild/protobuf-es/blob/main/docs/runtime_api.md#json-serialization-options, Message.fromJson is supposed to allow some option like enumAsInteger.

However, JsonReadOptions seems to include different options from code https://github.com/bufbuild/protobuf-es/blob/main/packages/protobuf/src/json-format.ts#L86.

Just wondering if the doc is out of date, is there any recommended way to support these options with fromJSON ?

Cheers.

@timostamm
Copy link
Member

Hey Nicole, when parsing an enum value from JSON, the full enum value name or the integer value are both accepted.

So for the Protobuf enum Foo { FOO_BAR=1 }, both JSON values "FOO_BAR" and 1 parse the same.

There isn't a JsonReadOption for it because both forms must be accepted to interop with other Protobuf implementations. If you serialize to JSON, you need to make a choice, so it is available as a JsonWriteOption.

@timostamm
Copy link
Member

Oops, @smaye81 just pointed out I'm, wrong. Yes, the docs for "JSON serialization options" are swapped. Thanks for letting us know, @nicole0707.

@timostamm timostamm reopened this Mar 6, 2024
smaye81 added a commit that referenced this issue Mar 6, 2024
Fixes #737 

This fixes the JSON serialization docs to list options for the correct
methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants