[JAVA-111] JSON.parse() and serialize() do not preserve the _id field across serializations as expected Created: 26/Apr/10  Updated: 29/Oct/10  Resolved: 25/May/10

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

Type: Improvement Priority: Major - P3
Reporter: William Shulman Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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.


 Comments   
Comment by William Shulman [ 25/May/10 ]

This can be closed now. Made changes to the Java driver to fix this

Comment by William Shulman [ 27/Apr/10 ]

Looks related to PYTHON-69 (http://jira.mongodb.org/browse/PYTHON-69) and http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON.

More generally it is probably an issue for most drivers.

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