Skip to content

Commit a7013b1

Browse files
[JAVA-327] fixes json parsing issue with int/long values
1 parent 33cb108 commit a7013b1

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

src/main/com/mongodb/util/JSON.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
package com.mongodb.util;
44

5-
import java.lang.reflect.*;
6-
import java.text.*;
5+
import java.lang.reflect.Array;
6+
import java.text.SimpleDateFormat;
77
import java.util.*;
8-
import java.util.regex.*;
8+
import java.util.regex.Pattern;
99

10-
import org.bson.*;
10+
import org.bson.BSONCallback;
1111
import org.bson.types.*;
1212

1313
import com.mongodb.*;
@@ -584,7 +584,11 @@ public Number parseNumber() {
584584

585585
if (isDouble)
586586
return Double.valueOf(s.substring(start, pos));
587-
return Long.valueOf(s.substring(start, pos));
587+
588+
Long val = Long.valueOf(s.substring(start, pos));
589+
if (val <= Integer.MAX_VALUE)
590+
return val.intValue();
591+
return val;
588592
}
589593

590594
/**

src/main/com/mongodb/util/JSONCallback.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
package com.mongodb.util;
44

5-
import java.text.*;
5+
import java.text.ParsePosition;
6+
import java.text.SimpleDateFormat;
67
import java.util.*;
7-
import java.util.regex.*;
8+
import java.util.regex.Pattern;
89

910
import org.bson.*;
1011
import org.bson.types.*;
@@ -61,8 +62,8 @@ public Object objectDone(){
6162
setRoot(o);
6263
}
6364
} else if ( b.containsField( "$ts" ) ) {
64-
Long ts = (Long) b.get("$ts");
65-
Long inc = (Long) b.get("$inc");
65+
Long ts = ((Number)b.get("$ts")).longValue();
66+
Long inc = ((Number)b.get("$inc")).longValue();
6667
o = new BSONTimestamp(ts.intValue(), inc.intValue());
6768
if (!isStackEmpty()) {
6869
cur().put( name, o );

src/test/com/mongodb/JavaClientTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
package com.mongodb;
1919

20-
import java.io.*;
21-
import java.nio.*;
20+
import java.io.IOException;
21+
import java.nio.ByteBuffer;
2222
import java.util.*;
23-
import java.util.regex.*;
23+
import java.util.regex.Pattern;
2424

25-
import org.bson.*;
25+
import org.bson.Transformer;
2626
import org.bson.types.*;
27-
import org.testng.annotations.*;
27+
import org.testng.annotations.Test;
2828

2929
import com.mongodb.util.*;
3030

@@ -635,7 +635,7 @@ public void testWriteResult(){
635635
assertEquals( 1 , res.getN() );
636636
assertFalse( res.isLazy() );
637637
}
638-
638+
639639
@Test
640640
public void testWriteResultMethodLevelWriteConcern(){
641641
DBCollection c = _db.getCollection( "writeresult2" );

src/test/com/mongodb/util/JSONTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@
1818

1919
package com.mongodb.util;
2020

21+
import java.text.ParseException;
22+
import java.text.SimpleDateFormat;
2123
import java.util.*;
24+
import java.util.regex.Pattern;
2225

23-
import java.util.regex.*;
24-
25-
import java.text.*;
26-
27-
import org.bson.types.*;
28-
29-
import com.mongodb.*;
3026
import org.bson.BSON;
3127
import org.bson.BasicBSONObject;
28+
import org.bson.types.*;
3229

33-
import org.testng.annotations.Test;
30+
import com.mongodb.*;
3431

3532
public class JSONTest extends com.mongodb.util.TestCase {
3633

@@ -195,7 +192,7 @@ public void testBasic(){
195192
assertEquals(threw, false);
196193
threw = false;
197194

198-
assertEquals( 4L , JSON.parse( "4" ) );
195+
assertEquals( 4 , JSON.parse( "4" ) );
199196
}
200197

201198
@org.testng.annotations.Test
@@ -304,10 +301,11 @@ public void testDate() {
304301

305302
@org.testng.annotations.Test
306303
public void testJSONEncoding() throws ParseException {
307-
String json = "{ 'str' : 'asdfasd' , 'long' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null, 'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
304+
String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null, 'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
308305
BasicDBObject a = (BasicDBObject) JSON.parse(json);
309306
assert (a.get("str").equals("asdfasd"));
310-
assert (a.get("long").equals(5L));
307+
assert (a.get("int").equals(5));
308+
assert (a.get("long").equals(123123123123L));
311309
assert (a.get("float").equals(0.4d));
312310
assert (a.get("bool").equals(false));
313311
SimpleDateFormat format =

0 commit comments

Comments
 (0)