-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
protojson: Add MarshalOption to write int64 as a number and not as a string #1414
Comments
The Changing the standard mapping is out of scope for the Go implementation, as it is not specific to any particular language. If you would like to propose changes to it, you could file an issue on the general protobuf issue tracker: https://github.com/protocolbuffers/protobuf/issues |
Adding an option to bypass this is out of the question as well? |
Go only adds options that the other major language implementations add. There's no such option in C++: https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.util.json_util#JsonPrintOptions |
See also:
|
Is your feature request related to a problem? Please describe.
When using the
protonjson
libraryint64
numbers are outputted as strings in the JSON due to this code:https://github.com/protocolbuffers/protobuf-go/blob/b92717ecb630d4a4824b372bf98c729d87311a4d/encoding/protojson/encode.go#L275-L278
From what I understand, it is written as a string since JavaScript cannot handle that precision.
However, from a JSON stand point (not JavaScript), it should handle any integers (JSON should not care about precision, just as XML shouldn't care about precision).
We are only interested in marshaling a message to a JSON, not to JavaScript, so would like the
protonjson
to write int64 as an integer/number and not as a string.Describe the solution you'd like
Add a new to option to
MarshalOptions
:https://github.com/protocolbuffers/protobuf-go/blob/b92717ecb630d4a4824b372bf98c729d87311a4d/encoding/protojson/encode.go#L43
Something like:
And then in
marshalSingular
use this option:Describe alternatives you've considered
AFAK, there are no other feasible solutions to write int64s as integers in the JSON output.
Additional context
If you use the default JSON marshaller (
encoding/json
),int64
is written as a number.However, since we need to output default values, we are forced to use this library due to the
EmitUnpopulated
option.So right now our organization is between a hard rock and a stone; Either omit default values (such as
0
andfalse
) or write int64 as strings (and breaking other tools that reads in JSON data).The text was updated successfully, but these errors were encountered: