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

also parse scientific notation as a double/decimal #852

Merged
merged 1 commit into from Jun 30, 2015

Conversation

chadselph
Copy link
Contributor

BigDecimal and Double's .toString uses scientific notation when the number has more than 6 decimal points.

So, when you have a Jackson generated JSON payload, you may end up with something like

{"my-double": "3E-8"}

will fail with a NumberFormatException:

! java.lang.NumberFormatException: For input string: "3E-8"
! at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
! at java.lang.Long.parseLong(Long.java:589)
! at java.lang.Long.parseLong(Long.java:631)
! at com.fasterxml.jackson.databind.util.TokenBuffer$Parser.getNumberValue(TokenBuffer.java:1392)
! at com.fasterxml.jackson.databind.util.TokenBuffer$Parser.getDoubleValue(TokenBuffer.java:1340)
! at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:207)
! at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:430) 
! at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:312)

It's a bit of a corner case; for me, I was converting a class with a non-standard descendant of Number (scala.math.BigDecimal) into a Map<String, Object> and it only broke once I got smaller numbers.

Happy to write a unit test if that helps; I only didn't because I opened this from the github web UI.

`BigDecimal` and `Double`'s `.toString` uses scientific notation when the number has more than 6 decimal points.

So, when you have a Jackson generated JSON payload, you may end up with something like 

```
{"my-double": "3E-8"}
```

will fail with a NumberFormatException:

```
! java.lang.NumberFormatException: For input string: "3E-8"
! at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
! at java.lang.Long.parseLong(Long.java:589)
! at java.lang.Long.parseLong(Long.java:631)
! at com.fasterxml.jackson.databind.util.TokenBuffer$Parser.getNumberValue(TokenBuffer.java:1392)
! at com.fasterxml.jackson.databind.util.TokenBuffer$Parser.getDoubleValue(TokenBuffer.java:1340)
! at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:207)
! at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:430) 
! at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:312)
```

It's a bit of a corner case; for me, I was converting a class with a non-standard descendant of `Number` (`scala.math.BigDecimal`) into a `Map<String, Object>` and it only broke once I got smaller numbers.

Happy to write a unit test if that helps; I only didn't because I opened this from the github web UI.
@cowtowncoder
Copy link
Member

Yes, I think you are right, detection of non-integer numbers is insufficient here, as scientific notation is indeed only allowed for floating point numbers in JSON (or rather, floating-point handling is needed with it). I think 'e' is also needed, but I can easily add that.

A simple unit test would be welcome, if you have time; if not, I can merge add something myself.

@cowtowncoder
Copy link
Member

Actually, I can do additions on my own.

cowtowncoder added a commit that referenced this pull request Jun 30, 2015
also parse scientific notation as a double/decimal
@cowtowncoder cowtowncoder merged commit 5390217 into FasterXML:master Jun 30, 2015
cowtowncoder added a commit that referenced this pull request Jun 30, 2015
@chadselph
Copy link
Contributor Author

cool, thanks

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 this pull request may close these issues.

None yet

2 participants