16

I have this POJO :

public class JsonObj {

    private String id;
    private List<Location> location;


    public String getId() {
        return id;
    }

    public List<Location> getLocation() {
        return location;
    }

    @JsonSetter("location")
    public void setLocation(){
        List<Location> list = new ArrayList<Location>();
        if(location instanceof Location){
            list.add((Location) location);
            location = list;
        }
    }
}

the "location" object from the json input can be either a simple instance of Location or an Array of Location. When it is just one instance, I get this error :

Could not read JSON: Can not deserialize instance of java.util.ArrayList out of   START_OBJECT token

I've tried to implement a custom setter but it didn't work. How could I do to map either a Location or a List depending on the json input?

TrtG
  • 2,778
  • 6
  • 26
  • 39
  • Different JSON means different JSON DTO. If your JSON has an `[]` you need to use `List`, if it has a `{}` you need a simple object. – Sotirios Delimanolis Jan 11 '14 at 14:22
  • It is sometimes [] and sometimes {} for the same key and unfortunally I can not change this. – TrtG Jan 11 '14 at 14:24
  • 1
    Create a custom deserializer that checks if its a `[]` or a `{}` and appropriately creates a instance and adds it to the `List`. – Sotirios Delimanolis Jan 11 '14 at 14:30
  • It seems to be the solution. Could you provide some code or doc in order to help me implement this? – TrtG Jan 11 '14 at 14:34
  • Just google `jackson deserializer`. Something like [this](http://pilhuhn.blogspot.ca/2013/07/custom-deserializer-in-jackson-and.html). – Sotirios Delimanolis Jan 11 '14 at 14:35
  • I'm sorry but I'm new to jackson mapping and I didn't succeed to create this deserializer. See my question http://stackoverflow.com/questions/21064003/jackson-custom-deserializer-mapping if you wish to help me. Thank you anyway! – TrtG Jan 11 '14 at 15:22
  • I found [this](https://stackoverflow.com/questions/39041496/how-to-enforce-accept-single-value-as-array-in-jacksons-deserialization-process#answer-39043513) answer useful if you want to do this on a single property – logee Jul 31 '20 at 07:12

2 Answers2

24

Update: Mher Sarkissian's soulution works fine, it can also be used with annotations as suggested here, like so:.

@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List<Item> item;

My deepest sympathies for this most annoying problem, I had just the same problem and found the solution here: https://stackoverflow.com/a/22956168/1020871

With a little modification I come up with this, first the generic class:

public abstract class OptionalArrayDeserializer<T> extends JsonDeserializer<List<T>> {

    private final Class<T> clazz;

    public OptionalArrayDeserializer(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    public List<T> deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException {
        ObjectCodec oc = jp.getCodec();
        JsonNode node = oc.readTree(jp);
        ArrayList<T> list = new ArrayList<>();
        if (node.isArray()) {
            for (JsonNode elementNode : node) {
                list.add(oc.treeToValue(elementNode, clazz));
            }
        } else {
            list.add(oc.treeToValue(node, clazz));
        }
        return list;
    }
}

And then the property and the actual deserializer class (Java generics is not always pretty):

@JsonDeserialize(using = ItemListDeserializer.class)
private List<Item> item;

public static class ItemListDeserializer extends OptionalArrayDeserializer<Item> {
    protected ItemListDeserializer() {
        super(Item.class);
    }
}
Love
  • 1,709
  • 2
  • 22
  • 30
  • Thanks for this snippet, it solved my problem! Just in case [here is scala version of the code above](https://gist.github.com/Aivean/6bb90e3942f3bf966608). – Aivean Mar 28 '16 at 20:31
5

This is already supported by jackson

objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Mher Sarkissian
  • 131
  • 2
  • 5