[JAVA-312] com.mongodb.util.JSON.serialize does not support UUID Created: 29/Mar/11  Updated: 11/Mar/12  Resolved: 29/Mar/11

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.5.2
Fix Version/s: 2.5.3

Type: Bug Priority: Minor - P4
Reporter: Sebastian Annies Assignee: Antoine Girbal
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

any


Attachments: File AddedUUIDSupport.patch    
Issue Links:
Duplicate
is duplicated by JAVA-321 Exception on calling "toString()" on ... Closed
Related
Backwards Compatibility: Fully Compatible

 Description   

toString() on a DBObject with a UUID property fails since JSON.serialize(..) cannot handle UUID. This stacktrace is the result

java.lang.RuntimeException: json can't serialize type : class java.util.UUID
	at com.mongodb.util.JSON.serialize(JSON.java:202)
	at com.mongodb.util.JSON.serialize(JSON.java:109)
	at com.mongodb.util.JSON.serialize(JSON.java:28)
	at com.mongodb.BasicDBObject.toString(BasicDBObject.java:75)

Fix is pretty simple. We just have to add

        if ( o instanceof UUID ){
            UUID c = (UUID)o;
            BasicDBObject temp = new BasicDBObject();
            temp.put( "$uuid" , c.toString() );
            serialize( temp, buf );
            return;
        }

to public static void serialize( Object o , StringBuilder buf ) in JSON and everything is fine. Please find attached fix and testcase.



 Comments   
Comment by Antoine Girbal [ 29/Mar/11 ]

added support in JSON serialize and parse.
Added tests.

Comment by auto [ 29/Mar/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: JAVA-312: com.mongodb.util.JSON.serialize does not support UUID
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/e96cebc4858c9620ca294d2557e5ba767ffec962

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