5

Good day,

I am currently integration attempting to consume a REST service that produces JSON (written in .NET) using Jackson (with Jersey). The JSON consists of a possible error message and an array of objects. Below is a sample of the JSON returned as produced by Jersey's logging filter:

{
    "error":null,
    "object":"[{\"Id\":16,\"Class\":\"ReportType\",\"ClassID\":\"4\",\"ListItemParent_ID\":4,\"Item\":\"Pothole\",\"Description\":\"Pothole\",\"Sequence\":1,\"LastEditDate\":null,\"LastEditor\":null,\"ItemStatus\":\"Active\",\"ItemColor\":\"#00AF64\"}]"
}

I have two classes to represent the type (the outer ListResponse):

public class ListResponse { 

    public String error;    
    public ArrayList<ListItem> object;  

    public ListResponse() { 
    }
}

and (the inner ListItem):

public class ListItem {
    @JsonProperty("Id")
    public int id;      
    @JsonProperty("Class")
    public String classType;
    @JsonProperty("ClassID")
    public String classId;  
    @JsonProperty("ListItemParent_ID")
    public int parentId;    
    @JsonProperty("Item")
    public String item; 
    @JsonProperty("Description")
    public String description;

    @JsonAnySetter 
    public void handleUnknown(String key, Object value) {}

    public ListItem() {
    }
}

The class that invokes and returns the JSON looks like this:

public class CitizenPlusService {
    private Client client = null;   
    private WebResource service = null;     

    public CitizenPlusService() {
        initializeService("http://localhost:59105/PlusService/"); 
    }

    private void initializeService(String baseURI) {    
        // Use the default client configuration. 
        ClientConfig clientConfig = new DefaultClientConfig();      
        clientConfig.getClasses().add(JacksonJsonProvider.class);                       

        client = Client.create(clientConfig);

        // Add a logging filter to track communication between server and client. 
        client.addFilter(new LoggingFilter()); 
        // Add the base URI
        service = client.resource(UriBuilder.fromUri(baseURI).build()); 
    }

    public ListResponse getListItems(String id) throws Exception
    {           
        ListResponse response = service.path("GetListItems").path(id).accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).get(ListResponse.class);                                  
        return response;            
    }
}

The important call here is the getListItems method. Running the code in a test harness, produces the following:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
at [Source: java.io.StringReader@49497eb8; line: 1, column: 14] (through reference chain: citizenplus.types.ListResponse["object"])

Please assist.

Regards, Carl-Peter Meyer

Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
Carl Meyer
  • 53
  • 1
  • 1
  • 4
  • Were you able to resolve this issue? – varaprakash Oct 02 '12 at 21:07
  • my answer might help you if you find a way to change your JSON support in .NET http://stackoverflow.com/a/14760003/2022175 – Manolo Feb 07 '13 at 20:10
  • I think the problem here is in the input JSON: the 'object' property consists of a **String which contains a JSON array** but Jackson expects a native array (without the wrapping apostrophies). I'm currently having the same problem... If I come up with a solution I will leet you know ;) – Philipp Maschke May 23 '13 at 11:43

3 Answers3

7

You may be missing a @JsonDeserialize attribute as the type information does get lost in generics at run-time. Also you should avoid using concrete classes for collections if you can.

public class ListResponse { 

    public String error;

    @JsonDeserialize(as=ArrayList.class, contentAs=ListItem.class)
    public List<ListItem> object;  

}
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
4

Your problem is that the 'object' property value is a String and not an array! The string contains a JSON array but Jackson expects a native array (without the wrapping quotes).

I had the same problem and I created a custom deserializer, which will deserialize a string value to a generic collection of the desired type:

public class JsonCollectionDeserializer extends StdDeserializer<Object> implements ContextualDeserializer {

  private final BeanProperty    property;

  /**
   * Default constructor needed by Jackson to be able to call 'createContextual'.
   * Beware, that the object created here will cause a NPE when used for deserializing!
   */
  public JsonCollectionDeserializer() {
    super(Collection.class);
    this.property = null;
  }

  /**
   * Constructor for the actual object to be used for deserializing.
   *
   * @param property this is the property/field which is to be serialized
   */
  private JsonCollectionDeserializer(BeanProperty property) {
    super(property.getType());
    this.property = property;
  }

  @Override
  public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
    return new JsonCollectionDeserializer(property);
  }


  @Override
  public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    switch (jp.getCurrentToken()) {
      case VALUE_STRING:
        // value is a string but we want it to be something else: unescape the string and convert it
        return JacksonUtil.MAPPER.readValue(StringUtil.unescapeXml(jp.getText()), property.getType());
      default:
        // continue as normal: find the correct deserializer for the type and call it
        return ctxt.findContextualValueDeserializer(property.getType(), property).deserialize(jp, ctxt);
    }
  }
}

Note that this deserializer will also work if the value actually is an array and not a string, because it delegates the actual deserialization accordingly.

In your example you would now have to annotate your collection field like so:

public class ListResponse { 

    public String error;    
    @JsonDeserialize(using = JsonCollectionDeserializer.class)
    public ArrayList<ListItem> object;  

    public ListResponse() {}    
}

And that should be it.

Note: JacksonUtil and StringUtil are custom classes, but you can easily replace them. For example by using new ObjectMapper() and org.apache.commons.lang3.StringEscapeUtils.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Philipp Maschke
  • 646
  • 1
  • 5
  • 8
  • I like this solution, but I'm seeing a stack overflow in some cases. Specifically I have a `List>` field. In this case, `return ctxt.findContextualValueDeserializer(property.getType(), property).deserialize(jp, ctxt);` infinitely recurses when interrogating the inner list. Haven't had a chance to dive deeply into it, but I believe it's because the property passed in is based on the outer list, not the inner one. – Michael Haefele Jun 15 '15 at 16:33
0

The register subTypes works!

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
public interface Geometry {

}

public class Point implements Geometry{
 private String type="Point";
  ....
}
public class Polygon implements Geometry{
   private String type="Polygon";
  ....
}
public class LineString implements Geometry{
  private String type="LineString";
  ....
}


GeoJson geojson= null;
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.registerSubtypes(Polygon.class,LineString.class,Point.class);
try {
    geojson=mapper.readValue(source, GeoJson.class);

} catch (IOException e) {
    e.printStackTrace();
}
yglodt
  • 13,807
  • 14
  • 91
  • 127