Skip to content
Browse files

Jira ticket; JAVA-529

NumberFormatException thrown within method parseNumber
is now caught and chained as a JSONParseException
  • Loading branch information...
1 parent e68d98d commit 49b7788c8a2276d6404e298880f5e424ff504585 @breinero breinero committed Mar 28, 2012
View
18 src/main/com/mongodb/util/JSON.java
@@ -630,14 +630,18 @@ public Number parseNumber() {
break outer;
}
}
-
- if (isDouble)
- return Double.valueOf(s.substring(start, pos));
- Long val = Long.valueOf(s.substring(start, pos));
- if (val <= Integer.MAX_VALUE && val >= Integer.MIN_VALUE)
- return val.intValue();
- return val;
+ try{
+ if (isDouble)
+ return Double.valueOf(s.substring(start, pos));
+
+ Long val = Long.valueOf(s.substring(start, pos));
+ if (val <= Integer.MAX_VALUE && val >= Integer.MIN_VALUE)
+ return val.intValue();
+ return val;
+ }catch(NumberFormatException e){
+ throw new JSONParseException(s, start, e);
+ }
}
/**
View
6 src/main/com/mongodb/util/JSONParseException.java
@@ -49,4 +49,10 @@ public JSONParseException(String s, int pos) {
this.s = s;
this.pos = pos;
}
+
+ public JSONParseException(String s, int pos, Throwable cause) {
+ super(cause);
+ this.s = s;
+ this.pos = pos;
+ }
}
View
7 src/test/com/mongodb/util/JSONTest.java
@@ -63,6 +63,13 @@ public void testLongValues() {
Long smallVal = Integer.MIN_VALUE - 1L;
String test2 = String.format("{ \"x\" : %d}", smallVal);
assertEquals(JSON.serialize(JSON.parse(test2)), test2);
+
+ try{
+ JSON.parse("{\"ReallyBigNumber\": 10000000000000000000 }");
+ fail("JSONParseException should have been thrown");
+ }catch(JSONParseException e) {
+ // fall through
+ }
}
@org.testng.annotations.Test(groups = {"basic"})

0 comments on commit 49b7788

Please sign in to comment.
Something went wrong with that request. Please try again.