Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-312

com.mongodb.util.JSON.serialize does not support UUID

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.5.3
    • Affects Version/s: 2.5.2
    • Component/s: None
    • Labels:
      None
    • Environment:
      any
    • Fully Compatible

      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.

            Assignee:
            antoine Antoine Girbal
            Reporter:
            sannies Sebastian Annies
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: