I'm trying to consume a webservice (on which I have no power) that is serving me an array of objects in JSON. the result is kind of malformed in the form:
[
[ #this is first object
{
"attribute1":"value1"
},
{
"attribute2":"value2"
}
],
[ # this is second object
{
"attribute1":"value1"
},
{
"attribute2":"value2"
}
]
]
So I'm trying to deserialize it to a pojo using jersey client 2.22.1 and jackson core 2.5.4. Since basic Jackson deserialising wasn't working I've created a custom deserializer.
Pojo class:
@JsonDeserialize(using = MyDeserializer.class)
public class Pojo {
private String attribute1;
private String attribute2;
*default constructor, getter and setters*
}
MyDeserializer class:
public class MyDeserializer extends JsonDeserializer<Pojo> {
@Override
public Pojo deserialize(JsonParser jParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Pojo pojoObj = new Pojo();
while (jParser.nextToken() != JsonToken.END_ARRAY) {
String fieldname = jParser.getCurrentName();
if ("attribute1".equals(fieldname)) {
jParser.nextToken();
pojoObj.setAttribute1(jParser.getText());
}
if ("attribute2".equals(fieldname)) {
jParser.nextToken();
pojoObj.setAttribute2(jParser.getText());
}
}
jParser.close();
return pojoObj;
}
}
The jersey/jackson call:
Client client = ClientBuilder.newClient().register(JacksonJaxbJsonProvider.class);
WebTarget webTarget = client.target("http://server/service/ressource").queryParam("param1", value);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
Response response = invocationBuilder.get();
list = Arrays.asList(response.readEntity(Pojo[].class));
but now when I call it I get:
java.lang.ArrayIndexOutOfBoundsException: 1054
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2732)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:652)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:149)
which let me think either jackson isn't using my custom deserializer, or that I've missed something.