ObjectId deserializes to Java's native toString()

639 views
Skip to first unread message

Sebastian Gaul

unread,
Feb 18, 2013, 8:23:22 AM2/18/13
to mongo-jack...@googlegroups.com
I use JacksonDBCollection (mongojack 2.0.0-RC1)  to write Java objects to my MongoDB. Classes to be mapped inherit from this implementation:

import org.mongojack.ObjectId;
import com.fasterxml.jackson.annotation.JsonProperty;

public class IdentifiableObject implements Serializable {
private String id;

@ObjectId
@JsonProperty("_id")
public String getId() {
return id;
}

@ObjectId
@JsonProperty("_id")
public void setId(String id) {
this.id = id;
}

}

Basically, this seems to work. An object written to Mongo looks like this:

{ "_id" : ObjectId("512203b24cca3cbcac0fbad6"), ... }

JacksonDBCollection.insert's WriteResult.getSavedId() returns the expected 512203b24cca3cbcac0fbad6. But reading these object from Mongo does not convert the ObjectId as expected:

IdentifiableObject myObj = myJacksonDBCollection.findOneById("512203b24cca3cbcac0fbad6");
System.out.print(myObj.getId()); // prints "de.undercouch.bson4jackson.types.ObjectId@6f45d7f2"

As you can see the ObjectId is converted using Java's native toString or something similar. I want the ID to be 512203b24cca3cbcac0fbad6 again. I hope somebody can tell me why this happens and how I can fix this.

James Roper

unread,
Feb 18, 2013, 9:37:26 AM2/18/13
to mongo-jack...@googlegroups.com
This looks like a regression... though I thought we had tests for this.  I'll take a look.


--
You received this message because you are subscribed to the Google Groups "mongo-jackson-mapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongo-jackson-ma...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sebastian Gaul

unread,
Feb 18, 2013, 11:17:52 AM2/18/13
to mongo-jack...@googlegroups.com
I want to add that I use a slightly modified ObjectMapper. I don't think that this is the problem, but who knows. It uses the original Mongojack configuratior:

ObjectMapper objectMapper = new ObjectMapper();
MongoJacksonMapperModule.configure(objectMapper);
objectMapper.registerModule(new JodaModule());

The additional module is jackson-datatype-joda (https://github.com/FasterXML/jackson-datatype-joda) for Joda time support.
Message has been deleted

Phillip Roberts

unread,
Feb 27, 2013, 7:01:05 PM2/27/13
to mongo-jack...@googlegroups.com
+1 on this issue and I'm not using a custom ObjectMapper.  I'm using the simple POJO and get similar output:
Person [id=de.undercouch.bson4jackson.types.ObjectId@ca56214, name=Phillip]

ETA on a fix?  Appreciate the work.

Thanks

Oddmar Sandvik

unread,
Mar 18, 2013, 6:54:06 PM3/18/13
to mongo-jack...@googlegroups.com

Thanks for this nice little framework. I also have the same issue with the following construct on 2.0.0-RC1.
In 2.0.0-SNAPSHOT, this seems to be fixed. When are you planning a new release candidate?


public class Point {
    @Id
    public String id;
    
    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

Thanks
Oddmar
Reply all
Reply to author
Forward
0 new messages