Mark Boon
Mark Boon

Reputation: 113

Deserializing MongoDB $oid using jackson library

I have a very simple POJO that I write to MongoDB. It then returns me JSON that includes the _id property as an $oid object. I'd like to write it into my POJO, but so far have failed. I've seen many people struggle with it and suggesting different solutions, but I have yet to get any of them to work. Probably because nobody provides exact details which libraries they are actually using. There's codehaus.jackson.* there's com.fastxml.jackson., jongo, org.mongo., com.mongo.* etc.

My object looks like this:

package com.blueplanetsoftware.metrics.rest;

import javax.persistence.Id;

import com.fasterxml.jackson.annotation.JsonProperty;

public class MongoObject
{
    @JsonProperty("_id")
    private String _id;

    @JsonProperty("userID")
    private String userID;
    @JsonProperty("appID")
    private String appID;  
    @JsonProperty("message")
    private String message = "";
    @JsonProperty("session")
    private String session;

    public MongoObject() {}

    /**
     * @return the id
     */
    @Id
    @JsonProperty("_id")
    public String getId()
    {
        return _id;
    }

    /**
     * @param id the id to set
     */
    @Id
    @JsonProperty("_id")
    public void setId(String id)
    {
        this._id = id;
    }
}

The JSON that I get back looks like this:

{ 
  "userID" : "aap" ,
  "appID" : "noot" , 
  "message" : "JSON" , 
  "session" : "ea944555b5ea8ea6" , 
  "_id" : { "$oid" : "5245f1063004348555e54815"}
}

when I try to deserialize that JSON into my class with this code:

com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
MongoObject o = mapper.readValue(response, MongoObject.class);

it gives me an exception:

java.lang.AssertionError: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.StringReader@456c1227; line: 1, column: 93] (through reference chain: com.blueplanetsoftware.metrics.rest.MongoObject["_id"])

So how to get the _id / $oid string into my _id property? I'm surprised there's isn't an easy way to do this. At least I haven't found any so far.

Upvotes: 4

Views: 4661

Answers (1)

Trisha
Trisha

Reputation: 3931

Have you tried?

@ObjectId @Id 
private String id;

I've written a test to see this working:

@Test
public void shouldConvertJSONStringIntoPojo() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    MongoObject actual = mapper.readValue("{\n" +
                                     "        \"userID\" : \"aap\" ,\n" +
                                     "        \"appID\" : \"noot\" ,\n" +
                                     "        \"message\" : \"JSON\" ,\n" +
                                     "        \"session\" : \"ea944555b5ea8ea6\" ,\n" +
                                     "        \"_id\" : { \"$oid\" : \"5245f1063004348555e54815\"}\n" +
                                     "}", MongoObject.class);

    assertThat(actual, equalTo(new MongoObject("5245f1063004348555e54815", "aap", "noot", "JSON", "ea944555b5ea8ea6")));
}

The libraries I'm using for this:

import com.fasterxml.jackson.databind.ObjectMapper; (jackson-databind-2.1.4,jar)
import org.junit.Test;                              (junit-4.11.jar)
import static org.hamcrest.CoreMatchers.equalTo;    (hamcrest-core-1.3.jar)
import static org.junit.Assert.assertThat;          (junit-4.11.jar)

The only changes I've made to your MongoObject posted in the question is adding a constructor that takes all the fields, and hashCode, equals & toString (so I can test it). If I run that test with that MongoObject, then I get the same error as you:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.StringReader@7a7ff2aa; line: 5, column: 40] (through reference chain: MongoObject["_id"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:599)

But if I make one small change to your MongoObject:

public class MongoObject
{
    @JsonProperty("_id")
    @org.jongo.marshall.jackson.oid.ObjectId
    private String _id;

    @JsonProperty("userID")
    private String userID;
    @JsonProperty("appID")
    private String appID;
    @JsonProperty("message")
    private String message;
    @JsonProperty("session")
    private String session;

    public MongoObject() {}

//... insert constructors, hashCode, equals and toString just for testing...
}

Then the test passes.

Upvotes: 4

Related Questions