16

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.

JJD
  • 50,076
  • 60
  • 203
  • 339
Josh Wilson
  • 3,585
  • 7
  • 32
  • 53

4 Answers4

11

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;
}
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Hah, this is what I was thinking of, but didn't find it in the jackson-annotations javadocs (it's in jackson-databind). – vanza Oct 25 '13 at 16:50
  • The member type InnerItems cannot be declared static; static types can only be declared in static or top level types – coltonfranco Feb 06 '17 at 17:52
5

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

JJD
  • 50,076
  • 60
  • 203
  • 339
StaxMan
  • 113,358
  • 34
  • 211
  • 239
1

Your JSON translates to: "the object named items is of a type that has a property named elements which is a list of some sort".

So your Item class just needs an elements property:

class Item {
    List<Something> getElements();
}

Note that your Java code doesn't map to your JSON. Your Java classes would map to something like:

{
    "name" : "foo",
    "items" : [
        { /* encoded version of Item */ }
    ]
}
vanza
  • 9,715
  • 2
  • 31
  • 34
  • Yes. A one to one mapping would work out of the box with Jackson. I am trying to create this nesting on the response without adding complexity to my java objects. – Josh Wilson Oct 25 '13 at 04:10
  • 1
    Then I don't understand your question. You want a list on the JSON encoding but no list in the source data? How so? It sounds like you're trying to do something like [this question](http://stackoverflow.com/questions/15378853/jackson-custom-serializer-that-overrides-only-specific-fields) is talking about, but that sounds like a lot of trouble. – vanza Oct 25 '13 at 04:16
  • BTW another thing you can look at is the `com.fasterxml.jackson.databind.Module` class, which allows you to create custom serializers for types; don't know if you can override the serializer for List/Collection/etc, but it might be a start. – vanza Oct 25 '13 at 04:26
  • Yes, it is possible to override handles for all types, including `List` and `Collection`. But for that `SimpleModule` isn't enough; need a custom `Serializers` implementation for structured types. – StaxMan Oct 27 '13 at 23:15
0

For scala, one can try:

class ItemsJsonDeserializer extends JsonDeserializer[List[Item]] {
  val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)

  override def deserialize(jp: JsonParser, dc: DeserializationContext): List[Item] = {
    val oc = jp.getCodec

    val nodes = oc.readTree[ObjectNode](jp).get("elements").asScala.toList
    val res = nodes.map { node =>
      mapper.readValue[Item](node.toString)
    }

    res 
  }
}

case class Item(price: Int)
case class Order {
    name: String,
    @JsonDeserialize(using = classOf[ItemsJsonDeserializer])
    items: List<Item>
}

Eric
  • 271
  • 3
  • 5