I'm working on creating an API that has nested lists. Jackson seems like a great tool to create objects, but I can't quite figure out how to nest a list, and I'm wondering if its possible.
My object looks like this.
public class Order {
    public String name;
    public List<Item> items;
}
I'm hoping there is a way to map it to json that looks something like:
{
    name : "A name"
    items : { 
        elements : [{
            price : 30
        }]
    }
}
We want to be able to do this so we can add properties to lists.
You can write custom deserializer for List<Item> items. See below example:
class ItemsJsonDeserializer extends JsonDeserializer<List<Item>> {
    @Override
    public List<Item> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        InnerItems innerItems = jp.readValueAs(InnerItems.class);
        return innerItems.elements;
    }
    private static class InnerItems {
        public List<Item> elements;
    }
}
Now, you have to inform Jackson to use it for your property. You can do this in this way:
public class Order {
  public String name;
  @JsonDeserialize(using = ItemsJsonDeserializer.class)
  public List<Item> items;
}
                        In general it is best to map JSON structure exactly to Java. In your case you could use something like:
public class Order {
  public String name;
  public ItemList items;
}
public class ItemList {
  public List<Item> elements;
  // and any properties you might want...
}
alternatively, you could probably also use (relatively) new @JsonFormat annotation:
public class Order {
  public String name;
  public ItemList items;
}
// annotation can be used on propery or class
@JsonFormat(shape=Shape.OBJECT) // instead of Shape.ARRAY
public class ItemList extends ArrayList<Item>
{
   public Iterator<Item> getElements() { return this.iterator(); }
   public String getSomeAttribute() { ... }
}
where you are forcing List or Collection to be serialized as if it was POJO, instead of normal special handling. There may be some side-effects, since introspection is used to find possible accessors, but the general approach should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With