Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting BSON Type ObjectId to JSON (Storing in Mongodb) -Java

new Gson().toJson(new ObjectId())

When I do the above, the output I get is

"_id" : { "_time" : 1374347520 , "_machine" : -1025067326 , "_inc" : 585905201 , "_new" : false}

But Actually I want it to be as

"_id":{"$oid":51eae100c2e6b6c222ec3431}

which is the usual mongodb ID format. What is the preferable method in Java for 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 am trying to store this to mongodb with a custom _id

TaskObject taskObject = new TaskObject();
taskObject.set_id(new ObjectId());
TaskMongoDBClient.getInstance().
        persistNewTaskData(new Gson().toJson(taskObject));

What is stored in the mongodb looks like this.

_id: { "_time" : 1397464341 , "_machine" : 1441187434 , "_inc" : -1687457948 , "_new" : true}

Instead of _id:{"$oid": xxxx} which i can query by using a the oid value.

What I am doing wrong here? please help.

Thanks

like image 486
Chinthaka Dharmasiri Avatar asked Apr 14 '14 13:04

Chinthaka Dharmasiri


People also ask

Can you convert BSON to JSON?

Synopsis. The bsondump converts BSON files into human-readable formats, including JSON.

What is ObjectId in BSON?

An ObjectId is a 12-byte BSON type having the following structure − The first 4 bytes representing the seconds since the unix epoch. The next 3 bytes are the machine identifier. The next 2 bytes consists of process id. The last 3 bytes are a random counter value.

What is the type of ObjectId in MongoDB?

Every document in the collection has an “_id” field that is used to uniquely identify the document in a particular collection it acts as the primary key for the documents in the collection. “_id” field can be used in any format and the default format is ObjectId of the document.

Does MongoDB store data in BSON?

MongoDB stores data in BSON format both internally, and over the network, but that doesn't mean you can't think of MongoDB as a JSON database. Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.


1 Answers

There are at least two possible ways to do this. Probably the most correct way is to use the GSON TypeAdapter to configure how the ObjectId is written to (and read from) JSON. You need to create something that implements TypeAdapter and register it with the GsonBuilder, this way GSON knows there's a special way to handle ObjectIds.

I've written a small test to prove this works, using your use case and example object (but omitted the revNo field for 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);

    // then
    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 that the test asserts the JSON looks the way you want it to, and that I had to create an ObjectIdTypeAdapter that knows the field name of ObjectId is "$oid" and the value is simply the string value of the ObjectID, and does not serialise all of the fields individually.

Note also that if you change the way the object is written to JSON, you also need to change the way it's read. So I've also implemented read to check that the field name is the correct one (you should probably throw an Exception here instead of using a simple assert if it's not correct), and then read the value and create the ObjectId from this String value.

In real life, you probably also want to check for null values in both of those cases too.

Because I'm a responsible coder, I also wrote a test to show the 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);

    // then
    TaskObject taskObject = new TaskObject();
    taskObject._id = new ObjectId("51eae100c2e6b6c222ec3431");
    assertThat(actualTaskObject._id, is(taskObject._id));
}

Further reading:

  • TypeAdapter
  • GsonBuilder.registerTypeAdapter
  • JsonReader and JsonWriter
like image 108
Trisha Avatar answered Oct 24 '22 00:10

Trisha