-
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
Allow for unmarshalling Numbers to Strings #875
Comments
The project has pretty much entirely rewritten the json protobuf encoding system in the “v2” reimplementation, and is hoping to release that soon. As such, work on this implementation is largely isolated to bug fixes only. Addressing your PR at merit, though: The JSON standard states:
As the JSON implementation works in Go though, the full precision of a However, the Protobuf standard only defines the types But that does not matter so much, because the code you offer only changes the behavior to include parsing an unquoted JSON number into a protobuf field of type Finally, implementing a new WellKnownType is out of scope for this project, and should be brought up with the principle protobuf team, as this project is concerned solely with the Go implementation of the protobuf standard. I hope this has been more informative than just simply a, “we have rewritten the whole package, and are not accepting anything but non-critical bug fixes to the current implementation.” |
Interesting. I based csvpb on jsonpb - so naturally will have a look whether to incorporate
Understandably.
Not sure I get your point. Go's JSON implementation explicitly implements decoding Numbers to json.Number (backed by a
That is fair. Having a practical need, though:
it seems excessive to pre-transform JSON just to appease Protobuf. That said - having a quick look at v2 it seems like there might be some better ways of implementing this kind of "custom" decoding without having to change the library internas, right?
Yes, thanks for taking the time. Side note - would you consider moving v2 to another URL? I think these breaking changes could disrupt a lot of projects. |
Yes, Go’s JSON implementation does allow decoding numbers into
But this is the very purpose of If you are looking for far more generalized use of JSON, then you should be using the standard library |
✔️
Really? From looking at the code there are precedences, where the mapping is way looser than I would expect from a narrow implementation.
Our scenario is rather that some teams haven't bought into Protobuf yet. And for them we convert JSON to Protobuf. What we ideally want is to have a way to custom unmarshal e.g. a Number to string. |
We cannot accept this change as our JSON implementation is fundamentally subservient to the protobuf <-> JSON specification. Since that specification does not permit string fields to accept numbers, neither can we. |
Is your feature request related to a problem? Please describe.
With regards to the JSON spec, a
Number
is not limited in scale or precision. As such, there are cases where currently there is no way of unmarshalling huge numbers with jsonpb without losing information.Describe the solution you'd like
Allow
Number
to be unmarshalled tostring
.Since the Protobuf protocol does not natively or via a WellKnownType support 64bit+ scalar types, rather allow application specific code, later on, to convert that information properly.
Should probably make use of
json.Number
, then.Describe alternatives you've considered
We could add a WellKnownType and implement unmarshalling to that.
For a general purpose type it could be something like:
which looks ugly enough and probably also not particularly useful.
The text was updated successfully, but these errors were encountered: