1

I am doing POJO serialization / deserialization using Jackson. Here is a POJO exemple :

public class Pojo {
    public String productId;
    public String name;
}

I have to read the field productId in this JSON :

{"productId":"1","name":"exemple"}

But also in :

{"_id":"1","name":"exemple"}

To make it short, I would like to use the same object to read the field in a JSON file found somewhere and to save the object as this in MongoDB, using productId as the primary key, which has to be named _id. Since I am using Jackson (fasterxml) both to read from the file and to write to the database, I can not find a way to do so, except by creating a new class with the same fields (or inheritance) and fill them one by one. Basically, I would like to find a way to put 2 @JsonProperty annotations on productId.

S. Aury
  • 81
  • 3
  • possible duplicate of [Serialize one class in two different ways with Jackson](http://stackoverflow.com/questions/12141561/serialize-one-class-in-two-different-ways-with-jackson) – Ross Taylor-Turner Nov 20 '14 at 16:14

1 Answers1

0

Works with both strings:

public class Pojo {
    @JsonProperty("_id")
    public String productId;
    public String name;

    @JsonProperty("productId")
    public void setProductId(String id) {
        productId = id;
    }

}
Ruslan Ostafiichuk
  • 4,422
  • 6
  • 30
  • 35