-
-
Notifications
You must be signed in to change notification settings - Fork 795
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
Support writing only (some?) longs as strings (opt-in) for Javascript compatibility #1044
Comments
This would need to be an optional feature to not change behaviour for existing users. See StreamWriteFeature. @rdesgroppes any interest in submitting a PR yourself (targeted at 2.16 branch)? |
This would need to be an optional feature to not change behaviour for existing users. See StreamWriteFeature. @rdesgroppes any interest in submitting a PR yourself (targeted at 2.16 branch)? In the end of the day, users wanting max interoperability should treat all numbers as JSON strings or use |
@pjfanning This would definitely be disabled by default. Whether this should be Note, too, that one could consider alternative approach of implementation at This is something that has been requested before, fwtw, so will mark as highly-requested. |
@rdesgroppes Quick question: just to make sure, would this only affect values that are beyond I changed title slightly but based on specific request feel free to further improve it. |
@cowtowncoder is it possible that this feature should affect writing BigInteger and BigDecimal too? Feels like it should. Those number types could represent numbers that make big longs look puny. |
I'm not sure about hardcoding Javascript magic numbers into Jackson, to be honest. Would we better off just sticking with WRITE_NUMBERS_AS_STRINGS and leaving it at that? Also, users can write their own custom serializers in Jackson. |
Well, I have to admit I'm a little embarrassed because the need I initially expressed (HubSpot/jackson-datatype-protobuf#98) applied to serializing protocol buffer messages (typically to pass them through a JSON proxy) and, according to the JSON Mapping of the proto3 specification, |
@pjfanning I think I'll disagree a bit here: given that JSON use is so heterogenous (client/server running on different platforms), we have long since given up on only supporting Java-to-Java case. So while I am not a big fan of language-specific support per se, this particular thing seems to be common enough request. Still, this is just a request and we can and should carefully consider what to do here. |
|
Thinking about this bit more, I wonder if instead of adding a Feature (on/off) of some kind, it'd make more sense to allow configuring a predicate that determines if given number ought to be stringified? Along with perhaps a standard implementation for this use case. The idea being there'd be multiple methods called, passing typed number -- first, maybe just one for integrals, so This would allow supporting wider range of conventions; starting with "all integer numbers" and "JS-range compatible" but allowing others, if any found. |
I think a better approach would be to offer a bouquet of number serializers, each dealing with a different quirk (stripping trailing zeros from BigDecimal, quoting numbers that JS can't handle properly). The API should be simple and just take a number and return either String or Number. This would then allow to write a wrapper which accepts Number and either returns them unmodified or converts them to String. The code in Jackson should look at the return type and write Number as before and when it's a String, it should delegate to writing a string (which would then add quotes). That way, users can quickly build a custom module picking and choosing whatever they need without having to litter the code base with |
While availability of alternate serializers can be useful, there are some challengers there: for example, serialization of For what it is worth, most Number deserializers will (by default) accept JSON String, although this permissibility can be changed with I agree that perhaps another approach would be a new "standard" value type of |
|
FWTW, I do not have immediate plans on working on this. It's more a placeholder at this point. |
Although Jackson exposes a WRITE_NUMBERS_AS_STRINGS feature that forces all regular number values to be written as JSON Strings rather than JSON Numbers, it doesn't currently provide a way to write only 64-bit integers using their canonical representation, which poses a problem for strict downstream parsers given the limitation of the maximum value to 2^53 - 1 (alt) in JavaScript, whereas this value is for instance 2^63 - 1 in Java.
Related specs:
Related issues:
long
, Javascript compatibility) jackson-databind#911,The text was updated successfully, but these errors were encountered: