Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson list deserialization. nested Lists

Tags:

java

json

jackson

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.

like image 605
Josh Wilson Avatar asked Oct 25 '13 03:10

Josh Wilson


Video Answer


2 Answers

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;
}
like image 84
Michał Ziober Avatar answered Sep 20 '22 04:09

Michał Ziober


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

like image 45
StaxMan Avatar answered Sep 20 '22 04:09

StaxMan