[英]Why not make some methods in com.fasterxml.jackson.databind.ObjectMapper static?
In my project i used a lot com.fasterxml.jackson.databind.ObjectMapper to deal with JSON, for example: 在我的项目中,我使用了很多com.fasterxml.jackson.databind.ObjectMapper来处理JSON,例如:
ObjectMapper mapper = new ObjectMapper();
A a = mapper.readValue(file.getBytes(), A.class);
At first sight I think it's better it's better to make the readValue method static, then we can use it as ObjectMapper.readValue(), much more clear. 乍一看,我认为最好将readValue方法设为静态,然后我们可以将它用作ObjectMapper.readValue(),更加清晰。 But I know there must be some reasons that it shouldn't, does anyone know it? 但我知道必须有一些理由不应该,有人知道吗?
Other reason is with each instance of ObjectMapper you bind certain specific confuguration ready by ObjectMapper while serialization and desrialization process somethinglike below. 其他原因是ObjectMapper的每个实例都绑定了ObjectMapper准备好的特定配置,而序列化和反序列化过程则类似于下面的内容。 So you can configure different startegies while object conversion process. 因此,您可以在对象转换过程中配置不同的startegies。
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String json = mapper.writeValueAsString(new MyBean());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.