-
Type:
Improvement
-
Resolution: Done
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
The current behavior might be intended, but it seems that the JSON utility class does not properly handle ObjectIds when serializing and parsing. I would expect that creating a DBObject (and saving to generate an ObjectId), serializing it, and then parsing would result in a new DBObject instance with the same _id as the original. That is, I would expect that parse(serialize(o)).id to equal o.id. This is not the case.
The following code illustrates the problem:
DBCollection c = getMeACollection();
// clear the collection
c.remove(new BasicDBObject());
DBObject o = new BasicDBObject();
c.save(o);
String json = JSON.serialize(o);
o = (DBObject)JSON.parse(json);
c.save(o);
results in two documents in the collection (I would expect one):
{ "_id" : ObjectId("4bd62964037394994a295cd3") } { "_id" : "4bd62964037394994a295cd3" }-
- Interestingly, the ObjectId class makes an attempt at dealing with this (see ObjectId.equals and ObjectId.massageToObjectId), but it does not seem to be handled thoroughly enough to prevent the type of behavior I illustrate in my example.