Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Json View for Deserialization

Tags:

java

json

jackson

I have a Json object coming back with the same property with different purposes based on the request (the resulting Json is out of my control). I only care about the property value when I make a specific request. Is there any way to use views for deserialization or something else that will conditionally propagate the pojo?

Example: Json

"boo":
{
  "a": "foo"
}

"boo":
{
  "a": { "x": 3 }
}

Java

public class Views {
  public static class WhatIWant {}
  public static class SomeOtherThings {}
}

public class Result {
  @JsonView(View.WhatIWant)
  public string a;
}

Result r = mapper.getDeserializationConfig()
                 .setDeserializationView(Views.WhatIWant.class)??
                 .readValue(node, Result.class);
like image 890
Benny Avatar asked Oct 21 '11 16:10

Benny


1 Answers

As of Jackson 2.0, JSONViews are available for deserialization too.

like image 165
Brian Clozel Avatar answered Sep 22 '22 14:09

Brian Clozel