codehaus jackson ObjectReader上的反序列化功能

时间:2015-10-19 05:36:36

标签: java jackson deserialization

如何为对象阅读器应用反序列化功能(FAIL_ON_UNKNOWN_PROPERTIES)?我知道如何为objectMapper做,但是,如何在codehaus jackson中的objectReader上做到这一点?

1 个答案:

答案 0 :(得分:2)

由于某些版本Jackson引入了ObjectReader#with方法来实现您想要的功能。也许移动到更高版本会更容易。

但是,如果您无法使用更高版本,则可以使用ObjectMapper#configure方法设置此功能。将此功能设置为true后,您可以使用reader方法从ObjectReader创建ObjectMapper

正如您在ObjectReader的文档中所看到的,您无法在创建后更改其功能。但是读者并没有分享地图制作者。因此,如果您需要从一个映射器创建多个读取器,并且只有少数读取器需要此选项,则只能为ObjectMapper设置功能。您可以获得此功能的当前状态

boolean enabled = mapper.getDeserializationConfig().isEnabled(feature);

更改状态,然后将其恢复为原始状态。请参阅示例中的createReader(Class<T> clazz, DeserializationConfig.Feature feature)方法。

这是我们将反序列化的POJO

class ExamplePojo {
    private int a1;
    private String b1;

    public ExamplePojo() {

    }

    public ExamplePojo(int a1, String b1) {
        this.a1 = a1;
        this.b1 = b1;
    }

    public int getA1() {
        return a1;
    }

    public void setA1(int a1) {
        this.a1 = a1;
    }

    public String getB1() {
        return b1;
    }

    public void setB1(String b1) {
        this.b1 = b1;
    }
}

这是一个例子

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.codehaus.jackson.map.exc.UnrecognizedPropertyException;

public class JacksonExample {

    static ObjectMapper mapper = new ObjectMapper();

    public static <T> ObjectReader createReader(Class<T> clazz, DeserializationConfig.Feature feature) {
        boolean enabled = mapper.getDeserializationConfig().isEnabled(feature);

        mapper.configure(feature, true);
        ObjectReader reader = mapper.reader(clazz);
        mapper.configure(feature, enabled);

        return reader;
    }

    public static <T> ObjectReader createReader(Class<T> clazz) {
        return mapper.reader(clazz);
    }

    public static void main(String[] args) throws Exception {

        // by default turn off this feature
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // Should fail
        ObjectReader r1 = createReader(ExamplePojo.class, DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
        try {
            ExamplePojo ex = r1.readValue("{\"a1\":1,\"b1\":\"Hello!\",\"c1\":1}");
            System.out.println(ex.getA1() + " " + ex.getB1());
        } catch (UnrecognizedPropertyException ex) {
            System.out.println(ex.getMessage());
        }

        // Should be ok
        ObjectReader r2 = createReader(ExamplePojo.class);
        try {
            ExamplePojo ex = r2.readValue("{\"a1\":2,\"b1\":\"Hello!\",\"c1\":2}");
            System.out.println(ex.getA1() + " " + ex.getB1());
        } catch (UnrecognizedPropertyException ex) {
            System.out.println(ex.getMessage());
        }

        // Should fail
        try {
            ExamplePojo ex = r1.readValue("{\"a1\":3,\"b1\":\"Hello!\",\"c1\":3}");
            System.out.println(ex.getA1() + " " + ex.getB1());
        } catch (UnrecognizedPropertyException ex) {
            System.out.println(ex.getMessage());
        }

        // Should be ok
        try {
            ExamplePojo ex = r2.readValue("{\"a1\":4,\"b1\":\"Hello!\",\"c1\":4}");
            System.out.println(ex.getA1() + " " + ex.getB1());
        } catch (UnrecognizedPropertyException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

此示例的输出

  

无法识别的字段&#34; c1&#34; (Class ExamplePojo),未在[来源:java.io.StringReader@7c30a502;中标记为可忽略; line:1,column:29](通过引用链:ExamplePojo [&#34; c1&#34;])

     

2你好!

     

无法识别的字段&#34; c1&#34; (类ExamplePojo),未在[来源:java.io.StringReader@49e4cb85;标记为可忽略; line:1,column:29](通过引用链:ExamplePojo [&#34; c1&#34;])

     

4你好!