Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson Unrecognized field

Tags:

java

json

jackson

I use jackson for converting JSON to Object class.

JSON:

{     "aaa":"111",     "bbb":"222",      "ccc":"333"  } 

Object Class:

class Test{     public String aaa;     public String bbb; } 

Code:

ObjectMapper mapper = new ObjectMapper(); Object obj = mapper.readValue(content, valueType); 

My code throws exception like that:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "cccc" (Class com.isoftstone.banggo.net.result.GetGoodsInfoResult), not marked as ignorable 

And I don't want to add a prop to class Test,I just want jackson convert the exist value whith is also exist in Test.

like image 891
YETI Avatar asked Dec 07 '11 02:12

YETI


People also ask

How do I ignore unrecognized fields Jackson?

You can ignore the unrecognized fields by configuring the ObjectMapper class: mapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false);

Does Jackson ignore unknown properties?

Jackson API provides two ways to ignore unknown fields, first at the class level using @JsonIgnoreProperties annotation and second at the ObjectMapper level using configure() method.

What is unrecognized property exception?

If the JSON string consists of properties that cannot be mapped to java class attributes, then we encounter UnrecognizedPropertyException.

How do I ignore JSON fields?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.


2 Answers

Jackson provides a few different mechanisms to configure handling of "extra" JSON elements. Following is an example of configuring the ObjectMapper to not FAIL_ON_UNKNOWN_PROPERTIES.

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; import org.codehaus.jackson.annotate.JsonMethod; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper;  public class JacksonFoo {   public static void main(String[] args) throws Exception   {     // { "aaa":"111", "bbb":"222", "ccc":"333" }     String jsonInput = "{ \"aaa\":\"111\",                           \"bbb\":\"222\",                           \"ccc\":\"333\" }";      ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD,                          Visibility.ANY);     mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,                      false);      Test test = mapper.readValue(jsonInput, Test.class);   } }  class Test {   String aaa;   String bbb; } 

For other approaches, see http://wiki.fasterxml.com/JacksonHowToIgnoreUnknown

like image 175
Programmer Bruce Avatar answered Oct 26 '22 22:10

Programmer Bruce


As of Jackson 2.0 the inner enum (DeserializationConfig.Feature) has been moved to a standalone enum (DeserializationFeature):

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

like image 31
Owen Pauling Avatar answered Oct 26 '22 23:10

Owen Pauling