Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize ArrayList from String using Jackson ACCEPT_SINGLE_VALUE_AS_ARRAY

Problem Deserializing array as string with Jackson 2

This is a similar problem to Deserialize ArrayList from String using Jackson

The incoming JSON (which I can't control) has an element 'thelist' which is an array. However, sometimes this comes in as an empty string instead of an array:

eg. instead of "thelist" : [ ]
it comes in as "thelist" : ""

I'm having trouble parsing both cases.

The 'sample.json' file which works fine:

{
   "name" : "widget",
   "thelist" : 
    [
       {"height":"ht1","width":"wd1"}, 
       {"height":"ht2","width":"wd2"}
    ]
}

The classes:

public class Product { 
    private String name; 
    private List<Things> thelist; 
    // with normal getters and setters not shown
}

public class Things {
        String height;
        String width;
        // with normal getters and setters not shown
}

The code that works fine:

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test2 {
 public static void main(String[] args) 
    {
        ObjectMapper mapper = new ObjectMapper(); 
        Product product = mapper.readValue( new File("sample.json"), Product.class);
    }
}

However, when the JSON has got an empty string instead of an array, ie. "thelist" : ""
I get this error:

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [collection type; class java.util.ArrayList, contains [simple type, class com.test.Things]] from JSON String; no single-String constructor/factory method (through reference chain: com.test.Product["thelist"])

If I add this line (which works for Ryan in Deserialize ArrayList from String using Jackson and seemingly supported by the documentation),

mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

it makes no difference.

Is there some other setting, or do I need to write a custom deserializer?
If the latter, is there a simple example of doing this with Jackson 2.0.4 ?
I'm new to Jackson (and first time poster, so be gentle). I have done lots of searching, but can't find a good working example.

like image 962
user1721784 Avatar asked Oct 08 '12 02:10

user1721784


2 Answers

Problem is that although single-element-to-Array works, you are still trying a conversion from (empty) String into an Object. I am assuming this is the problem you are facing, although without exception it is hard to say.

But there is also DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT which might do the trick, combined with the first feature. If so, you would get a List with a single "empty Object", meaning Things instance without values.

Now ideally what should happen is that if you only enabled ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, that would probably do what you want: null value for thelist property.

like image 116
StaxMan Avatar answered Sep 30 '22 08:09

StaxMan


Hi I was solving similar problem, when I get object like this

{  
   "name" : "objectname",
   "param" : {"height":"ht1","width":"wd1"}, 
}

from external system so "param" was OBJECT for me which I try to deserilize. When this object was defined in external system it works without problem. But when OBJECT "param" in external system was not defined I get empty ARRAY instead of empty OBJECT

{  
   "name" : "objectname",
   "param" : [], 
}

which cause mapping exception. I solve it with creating custom json deserializer which has very good example here and for testing of type I used something like

    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);
    if (JsonNodeType.OBJECT == node.getNodeType()) {
        ParamObject result = new ParamObject();
        result.setHeight(node.get("height").asText());
        result.setWidth(node.get("width").asText());
        return result;
    }
    // object in MailChimp is not defined
    return null;
like image 45
Mr.B Avatar answered Sep 30 '22 08:09

Mr.B