Skip to content

Commit e96cebc

Browse files
committed
[JAVA-312]: com.mongodb.util.JSON.serialize does not support UUID
1 parent e9c3858 commit e96cebc

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ public static void serialize( Object o , StringBuilder buf ){
193193
return;
194194
}
195195

196+
if ( o instanceof UUID ){
197+
UUID uuid = (UUID)o;
198+
BasicDBObject temp = new BasicDBObject();
199+
temp.put( "$uuid" , uuid.toString() );
200+
serialize( temp, buf );
201+
return;
202+
}
203+
196204
if ( o instanceof CodeWScope ){
197205
CodeWScope c = (CodeWScope)o;
198206

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public Object objectDone(){
8787
} else {
8888
setRoot(o);
8989
}
90+
} else if ( b.containsField( "$uuid" ) ) {
91+
o = UUID.fromString((String)b.get("$uuid"));
92+
if (!isStackEmpty()) {
93+
cur().put( name, o );
94+
} else {
95+
setRoot(o);
96+
}
9097
}
9198
}
9299
return o;

src/test/com/mongodb/JavaClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ public void testBadKey(){
750750
public void testAllTypes(){
751751
DBCollection c = _db.getCollectionFromString( "foo" );
752752
c.drop();
753-
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 }";
753+
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' }}";
754754
BasicDBObject a = (BasicDBObject) JSON.parse(json);
755755
c.insert(a);
756756
DBObject b = c.findOne();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public void testDate() {
304304

305305
@org.testng.annotations.Test
306306
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 }";
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' }}";
308308
BasicDBObject a = (BasicDBObject) JSON.parse(json);
309309
assert (a.get("str").equals("asdfasd"));
310310
assert (a.get("long").equals(5L));
@@ -325,6 +325,7 @@ public void testJSONEncoding() throws ParseException {
325325
assert (a.get("code").equals(new Code("asdfdsa")));
326326
assert (a.get("codews").equals(new CodeWScope("ggggg", new BasicBSONObject())));
327327
assert (a.get("ts").equals(new BSONTimestamp(1300474885, 10)));
328+
assert (a.get("uuid").equals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5")));
328329
String json2 = JSON.serialize(a);
329330
BasicDBObject b = (BasicDBObject) JSON.parse(json2);
330331
a.equals(b);

0 commit comments

Comments
 (0)