Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing field from nested objects within JSON response with Jackson

This seems like it should be a fairly solved/well-addressed issue, but I'm not finding much guidance on it -- hoping this isn't a dupe.

My scenario is basically that I am consuming paginated JSON responses that look something like this:

{ 
  "objects": [...],
  "meta": {
    "total": 5000,
    "page": 1,
    "result_pages": 20,
    "links": {
      "prev": null,
      "next": "/api/v3/objects/somequery?page=2"
    }
  }
}

Obviously this is simplified, but hopefully it gets the point across.

All I really care about are the objects and next fields, but it's looking like I'll have to create a whole hierarchy of DTOs to successfully deserialize the nested fields.

Is there a jackson annotation that would let me skip all of this? If not, is there a set of best practices for doing this that doesn't involve a host of mostly-empty classes and files?

like image 449
a p Avatar asked Jun 21 '16 18:06

a p


1 Answers

Is there a jackson annotation that would let me skip all of this?

You can use JsonDeserialize and define custom JsonDeserializer.

class MetaDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
        JsonNode tree = jp.readValueAsTree();
        return tree.get("links").get("next").asText();
    }
}

Here I used simple map deserialization, but if you want you can implement your own fast deserialization.

And the object

class MetaObject {
    public List<Integer> objects;

    @JsonDeserialize(using = MetaDeserializer.class)
    public String meta;

    @Override
    public String toString() {
        return "MetaObject{" + "objects=" + objects + ", meta='" + meta + '\'' + '}';
    }
}

And if you invoke MetaObject result = mapper.readValue("...", MetaObject.class) you will get what you want

MetaObject{objects=[1, 2, 3], meta='/api/v3/objects/somequery?page=2'}
like image 160
SerCe Avatar answered Oct 24 '22 01:10

SerCe