0
votes

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.

2

2 Answers

0
votes

try this code:

public Pojo deserialize(JsonParser jParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectCodec oc = jp.getCodec();
    JsonNode node = oc.readTree(jp);

    Iterator<JsonNode> iterator = node.iterator();
    List<Pojo> pojos = new ArrayList<Pojo>();

    while (iterator.hasNext()) {
        JsonNode next = iterator.next();
        pojos.add(
            new Pojo(
                next.get("attribute1"),
                next.get("attribute2")));
    }

    return pojos;
}
0
votes

ok i'll get you the answer since you made most of the code, the solution was:

public Pojo deserialize(JsonParser jParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  ObjectCodec oc = jp.getCodec();
  JsonNode node = oc.readTree(jp);
  Iterator<JsonNode> iterator = node.iterator();
  JsonNode next = iterator.next();
  String attribute1 = null
  if (next.get("attribute1") != null) {
     attribute1 = next.get("attribute1").asText();
  }
  next = iterator.next();
  String attribute2 = null
  if (next.get("attribute2") != null) {
     attribute2 = next.get("attribute2").asText();
  }
  Pojo objPojo = new Pojo(attribute1,attribute2);
  return objPojo;
}