Deserializing Bson.ObjectId with Jackson

I am trying to serialize and deserialize a class using Jackson. I want to serialize a specific field of type ObjectId as a String. So for serializing, I need to do something like objectId.toString() and to deserialize I need to do objectId = new ObjectId(stringValue).

My fields of my POJO are annotated like this:

@JsonSerialize(using = ToStringSerializer.class)

@JsonDeserialize(using = ObjectIdDeserializer.class)

private ObjectId anchorId;

private String name;

private double xPosition;

private double yPosition;

private double zPosition;

I have implemented a custom serializer for this.

public class ObjectIdDeserializer extends JsonDeserializer {


public ObjectId deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {

final String...

Read More »

By: StackOverFlow - Monday, 16 April

Related Posts