Benny
Benny

Reputation: 3917

Jackson Json View for Deserialization

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);

Upvotes: 5

Views: 7146

Answers (2)

Brian Clozel
Brian Clozel

Reputation: 59141

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

Upvotes: 6

Programmer Bruce
Programmer Bruce

Reputation: 66943

In Jackson, JSON Views are for serialization only.

To deserialize JSON data into sometimes a String and other times an object would require custom deserialization processing.

Upvotes: 3

Related Questions