-
-
Notifications
You must be signed in to change notification settings - Fork 796
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 precision loss on copyCurrentEvent()
for floats that require greater than double
precision
#730
Comments
It just occurred to me that this causes precision loss in jackson-core as well when going from JSON => JSON (e.g. when pretty printing). Failing test in #731. |
A simpler but more disruptive option might be to introduce a new |
Not commenting on earlier parts, but I will say that addition of a new I also thought I had added something in this space, but seems there's only I think that since nature of exactness is mostly binary (exact usually) vs textual (usually not), it'd be per-parser feature: if so, could add new |
That'd work. |
copyCurrentEvent()
for floats that require greater than DOUBLE precision
@pjfanning Forgot this one, related to work to try to retain best representation. Failing test accidentally referenced #733 (PR) so changed that name. |
@cowtowncoder I'm not up to speed on this issue. Have we broken something? |
@pjfanning No, not at all -- it's an older issue; basically value that is outside of |
copyCurrentEvent()
for floats that require greater than DOUBLE precisioncopyCurrentEvent()
for floats that require greater than double
precision
Hmmh. @pjfanning I think I may need to consider reverting functional change here, due to concerns wrt CBOR backend (and generally binary ones). As long as I am also thinking of adding new "copyCurrentEventExact()" method that would use the new mechanism, but then leave "copyCurrentEvent()" to use the old/existing (2.14) and before logic. |
Replaced by #984; closing as duplicate. |
Note: besides 2.15 adds lots of functionality to help (including #984), it's worth noting there is |
Feature Request
I'd like a way to determine if
getNumberType()
is guaranteed to return an exact response or might lose precision if parsed to the corresponding native type.Motivation
The JSON
JsonParser
s always returnNumberType.DOUBLE
-- neverFLOAT
orBIGDECIMAL
. Consequently, I've been (incorrectly) parsing all JSONJsonToken.VALUE_NUMBER_FLOAT
as doubles, losing precision for anything that doesn't fit. These comments suggest that the reason for always returningDOUBLE
is that BigDecimal parsing is expensive, so it's up to the jackson-core consumer to ask for one if they want it.As a user of the
JsonParser
I don't know if I want aBigDecimal
at the point I interact with theJsonParser
. I'm pushing the value downstream to be interpreted later. I'd like to get either a primitivefloat
/double
or else aCharSequence
. This is the approach I'm taking in rallyhealth/weePickle#102.However, converting all floats to strings is suboptimal when used with jackson-dataformats-binary.
SmileParser
knows that a double-tagged value (0x29
) is 100% guaranteed to fit in a primitive double, but I have to convert it to a base10 string (and probably back to a base2 double later), since I can't trustgetNumberType
's answer ofDOUBLE
.I've found #631, but for JSON, it appears always to return
BigDecimal
even for values like0.0
that would fit in a double. I've benched about a 50% throughput penalty for sending all floats throughBigDecimal
.AFAICT, it is not possible to determine whether or not to trust
getNumberType()
to avoid precision loss for any givenJsonParser
without hardcoding to its known behavior ahead of time (which may change in subsequent releases).Proposal
I don't see a way to update the JSON
JsonParser
to return exactgetNumberType()
values without increasing upfront parsing cost, but if I had a signal thatgetNumberType()
is trustworthy for a Smile JsonParser, but not for a JSON JsonParser, I could work around it. I would be able to ask for Strings for only non-exact values.I think some method like
boolean isNumberTypeGuaranteedExact()
would satisfy my needs. Being able to ask for each token seems ideal. For the JSON JsonParser, it might returnfalse
for allJsonToken.VALUE_NUMBER_FLOAT
.WDYT? And especially, is there some other existing option that I've overlooked?
Thanks!
The text was updated successfully, but these errors were encountered: