[JAVA-529] NumberFormatException parsing should provide exception subclass or better descriptions Created: 06/Mar/12  Updated: 03/Feb/16  Resolved: 28/Mar/12

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.7.3
Fix Version/s: 2.8.0

Type: Improvement Priority: Minor - P4
Reporter: Jared D. Cottrell Assignee: Bryan Reinero
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Java 6 on Mac OS X 10.7.3 (Lion)


Backwards Compatibility: Minor Change

 Description   

When trying to JSON.parse() the following String, we get a NumberFormatException. Presumably this is because the number is larger than java.lang.Long.MAX_VALUE.

{
"_id":

{ "$oid": "4f5535221a880ab3956efea0" }

,
"foo": 109997115319321890123
}

java.lang.NumberFormatException: For input string: "109997115319321890123"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:422)
at java.lang.Long.valueOf(Long.java:525)
at com.mongodb.util.JSONParser.parseNumber(JSON.java:636)
at com.mongodb.util.JSONParser.parse(JSON.java:370)
at com.mongodb.util.JSONParser.parseObject(JSON.java:414)
at com.mongodb.util.JSONParser.parse(JSON.java:378)
at com.mongodb.util.JSONParser.parse(JSON.java:328)
at com.mongodb.util.JSON.parse(JSON.java:287)



 Comments   
Comment by Jeffrey Yemin [ 03/Feb/16 ]

aleksey.korolev This issue is closed, so we won't be doing anything further with it. As what you're asking for seems to be a feature request, please open a new issue if you'd like to see it addressed. But before you do that, please consider:

  • Converting to String would silently change the value type
  • Converting to BigInteger or BigDecimal is a possibility, but currently the driver is not able to encode either of those into BSON, as there is no BSON type large enough to hold a number larger than a long or double.
  • An upcoming version of MongoDB will support 128-bit decimal values, which may be large enough to hold your numbers.
Comment by Aleksey Korolev [ 03/Feb/16 ]

Just caught this exception on MongoDB 3.2 when the number was too big for java.lang.Long and behavior was unexpected for me.
How about rethinking this to not fail but rather parse such numbers as String or BigInteger, may be with help of options passed to parse method?

Comment by Jeffrey Yemin [ 18/Jun/12 ]

Closing for 2.8.0 release.

Comment by Bryan Reinero [ 28/Mar/12 ]

This change may impact applications which have been coded explicitly to catch NumberFormatExceptions. These applications should instead catch JSONParseExceptions.

Comment by auto [ 28/Mar/12 ]

Author:

{u'login': u'breinero', u'name': u'Bryan Reinero', u'email': u'bryan.reinero@10gen.com'}

Message: Jira ticket; JAVA-529
NumberFormatException thrown within method parseNumber
is now caught and chained as a JSONParseException
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/49b7788c8a2276d6404e298880f5e424ff504585

Comment by Jeffrey Yemin [ 06/Mar/12 ]

How about we wrap the NumberFormatException in a com.mongodb.util.JSONParseException, which already exists and is thrown for other parsing errors.

Comment by Scott Hernandez (Inactive) [ 06/Mar/12 ]

All numbers in javascript are fp (64bit) by default. NumberLong is what you want if you want a signed int (64bit).

Comment by Jared D. Cottrell [ 06/Mar/12 ]

Though there does seem to be some inconsistency with how the shell behaves. It will take the number without error, but rounds it. I assume it's turning it into a floating point number.

> db.stuff.save(

{"foo": 109997115319321890123}

)
> db.stuff.find()

{ "_id" : ObjectId("4f5560695d4a54c0b7ae6a76"), "foo" : 109997115319321890000 }
Comment by Jared D. Cottrell [ 06/Mar/12 ]

It would be helpful to have descriptive text as well as an Exception subclass that can reliably be filtered on to pull out user-readable messages.

So, one solution might be to throw InvalidJsonExceptions where the message can be counted on to be clean and in this case might say, "The number \"109997115319321890123\" is too large to be stored."

Comment by Scott Hernandez (Inactive) [ 06/Mar/12 ]

How would you like this to be handled? An error the number is too long and can't be stored? Or invalid?

Generated at Thu Feb 08 08:52:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.