Java Jackson Deserialization - Custom add to list

25 Views Asked by At

I have a question regarding how Jackson deserializes a List.

I searched online before writing this question but I didn't find anything (probably I didn't find anything because in reality I wouldn't know exactly what to look for)...

I have this json

{
    "list": [
        {"name": "Paul"},
        {"name": "Charles"}
    ]
}

and I have this classes

public class OuterClass {
    private List<InnerClass> list;
    // Getters and Setters
}
public class InnerClass {
    private int prog;
    private String name;
    // Getters and Setters
}

I can deserialize the json with the statement

OuterClass outerClass = new ObjectMapper().readValue(json, OuterClass.class);

achieving this result

OuterClass [list=[InnerClass [prog=0, name=Paul], InnerClass [prog=0, name=Charles]]]

Obviously the prog property of the InnerClass class is not valued (since it is not present in the json). What I would need is, in some way, to set prog with a progressive. That is, get this

OuterClass [list=[InnerClass [prog=1, name=Paul], InnerClass [prog=2, name=Charles]]]

Thanks in advance

0

There are 0 best solutions below