Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson: Object Mapper annotations to deserialize a inner collection

Tags:

java

json

jackson

I want to convert the following json into a java object, using as much as possible annotations.

{"user":{
  "id":1,
  "diets":[
    {"diet":{
      "name":"...",
      "meals":[]
      }
    }
  ]
 }
}

I'm getting trouble with the collection diets. I tried to use @JsonProperty but it doesn't work properly. Is there a special annotation for map inner aggregates?

Diet.java

    @JsonRootName(value = "diet")
    public class Diet {

        @JsonProperty(value="name")
        private String name;
        @JsonProperty(value="meals")
        private List<Meal> meals;
        private User user;

        // Rest of the class omitted.
}

User.java

@JsonRootName(value = "user")
public class User {

    @JsonProperty("id")
    private long id;
    @JsonProperty("diets")
    private List<Diet> diets = new ArrayList<Diet>();

    // Rest of the class omitted.
}

Thanks!

like image 659
tehAnswer Avatar asked Aug 07 '14 14:08

tehAnswer


People also ask

What is the use of @JsonIgnore annotation?

@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setters, getters or fields. If you add @JsonIgnore to a field or its getter method, the field is not going to be serialized.

What is JsonProperty annotation?

@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.

What is difference between JsonProperty and JsonAlias?

@JsonProperty can change the visibility of logical property using its access element during serialization and deserialization of JSON. @JsonAlias defines one or more alternative names for a property to be accepted during deserialization.

Which methods does Jackson rely upon to deserialize a JSON formatted string?

The @JsonSetter annotation tells Jackson to deserialize the JSON into Java object using the name given in the setter method. Use this annotation when your JSON property names are different to the fields of the Java object class, and you want to map them.


1 Answers

The diets object in your json is not a List. Its a List of key-value pair with key "diet" and value a diet object. So you have three options here.

One is to create a wrapper object say DietWrapper and use List of diet wrapper in User like

@JsonRootName(value = "user")
class User {

    @JsonProperty(value = "id")
    private long id;
    @JsonProperty(value = "diets")
    private List<DietWrapper> diets;

    //Getter & Setters
}

class DietWrapper {
    @JsonProperty(value = "diet")
    Diet diet;
}

Second option is to keep diest as simple list of maps like List>

@JsonRootName(value = "user")
class User {

    @JsonProperty(value = "id")
    private long id;
    @JsonProperty(value = "diets")
    private List<Map<String, Diet>> diets;

    //Getter & Setters
}

Third option is to use a custom deserializer which would ignore your diet class.

@JsonRootName(value = "user")
class User {

    @JsonProperty(value = "id")
    private long id;
    @JsonProperty(value = "diets")
    @JsonDeserialize(using = DietDeserializer.class)
    private List<Diet> diets;

    //Getter & Setters
}

class DietDeserializer extends JsonDeserializer<List<Diet>> {

    @Override
    public List<Diet> deserialize(JsonParser jsonParser, 
            DeserializationContext deserializationContext) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(jsonParser);
        List<Diet> diets = mapper.convertValue(node.findValues("diet"), new TypeReference<List<Diet>>() {});
        return diets;
    }
}
like image 55
Syam S Avatar answered Oct 31 '22 16:10

Syam S