Converting BSON Type ObjectId to JSON (Storing in Mongodb) -Java -
new gson().tojson(new objectid())
when above, output
"_id" : { "_time" : 1374347520 , "_machine" : -1025067326 , "_inc" : 585905201 , "_new" : false}
but want
"_id":{"$oid":51eae100c2e6b6c222ec3431}
which usual mongodb id format. preferable method in java this?
update:
my value object
import com.google.gson.annotations.serializedname; import org.bson.types.objectid; public class taskobject { @serializedname("_id") private objectid _id; @serializedname("revno") private int revno; }
i trying store mongodb custom _id
taskobject taskobject = new taskobject(); taskobject.set_id(new objectid()); taskmongodbclient.getinstance(). persistnewtaskdata(new gson().tojson(taskobject));
what stored in mongodb looks this.
_id: { "_time" : 1397464341 , "_machine" : 1441187434 , "_inc" : -1687457948 , "_new" : true}
instead of _id:{"$oid": xxxx} can query using oid value.
what doing wrong here? please help.
thanks
there @ least 2 possible ways this. correct way use gson typeadapter configure how objectid written (and read from) json. need create implements typeadapter
, register gsonbuilder
, way gson knows there's special way handle objectids.
i've written small test prove works, using use case , example object (but omitted revno
field brevity):
@test public void shouldwritecorrectjson() { // given taskobject taskobject = new taskobject(); taskobject._id = new objectid("51eae100c2e6b6c222ec3431"); gson gson = new gsonbuilder().registertypeadapter(objectid.class, new objectidtypeadapter()).create(); // when string gsonstring = gson.tojson(taskobject); // assertthat(gsonstring, is("{\"_id\":{\"$oid\":\"51eae100c2e6b6c222ec3431\"}}")); } private class objectidtypeadapter extends typeadapter<objectid> { @override public void write(final jsonwriter out, final objectid value) throws ioexception { out.beginobject() .name("$oid") .value(value.tostring()) .endobject(); } @override public objectid read(final jsonreader in) throws ioexception { in.beginobject(); assert "$oid".equals(in.nextname()); string objectid = in.nextstring(); in.endobject(); return new objectid(objectid); } }
note test asserts json looks way want to, , had create objectidtypeadapter
knows field name of objectid "$oid" , value string value of objectid, , not serialise of fields individually.
note if change way object written json, need change way it's read. i've implemented read
check field name correct 1 (you should throw exception here instead of using simple assert if it's not correct), , read value , create objectid string value.
in real life, want check null values in both of cases too.
because i'm responsible coder, wrote test show reading case works too:
@test public void shouldreadfromjson() { // given gson gson = new gsonbuilder().registertypeadapter(objectid.class, new objectidtypeadapter()).create(); // when taskobject actualtaskobject = gson.fromjson("{\"_id\":{\"$oid\":\"51eae100c2e6b6c222ec3431\"}}", taskobject.class); // taskobject taskobject = new taskobject(); taskobject._id = new objectid("51eae100c2e6b6c222ec3431"); assertthat(actualtaskobject._id, is(taskobject._id)); }
further reading:
Comments
Post a Comment