gpt4 book ai didi

java - jackson 反序列化错误处理

转载 作者:太空狗 更新时间:2023-10-29 22:39:42 26 4
gpt4 key购买 nike

我的问题很简单:我有以下简单类:

public class Foo {
private int id = -1;
public void setId(int _id){ this.id = _id; }
public int getId(){ return this.id; }
}

我正在尝试处理以下 JSON:

{
"id": "blah"
}

很明显,这里有一个问题(“blah”无法解析为int)

以前,Jackson 会抛出类似 org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Integer from String value 'blah': not a valid Integer value

我同意这一点,但我想在某处注册一些东西以允许忽略此类映射错误。我尝试使用注册的 DeserializationProblemHandler(参见 here),但它似乎只适用于未知属性,而不适用于反序列化问题。

你对这个问题有什么线索吗?

最佳答案

感谢Tatu from Jackson ML,我成功解决了我的问题.

我必须为 Jackson 中处理的每个原始类型使用自定义非阻塞反序列化器。像这个工厂的东西:

public class JacksonNonBlockingObjectMapperFactory {

/**
* Deserializer that won't block if value parsing doesn't match with target type
* @param <T> Handled type
*/
private static class NonBlockingDeserializer<T> extends JsonDeserializer<T> {
private StdDeserializer<T> delegate;

public NonBlockingDeserializer(StdDeserializer<T> _delegate){
this.delegate = _delegate;
}

@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
try {
return delegate.deserialize(jp, ctxt);
}catch (JsonMappingException e){
// If a JSON Mapping occurs, simply returning null instead of blocking things
return null;
}
}
}

private List<StdDeserializer> jsonDeserializers = new ArrayList<StdDeserializer>();

public ObjectMapper createObjectMapper(){
ObjectMapper objectMapper = new ObjectMapper();

SimpleModule customJacksonModule = new SimpleModule("customJacksonModule", new Version(1, 0, 0, null));
for(StdDeserializer jsonDeserializer : jsonDeserializers){
// Wrapping given deserializers with NonBlockingDeserializer
customJacksonModule.addDeserializer(jsonDeserializer.getValueClass(), new NonBlockingDeserializer(jsonDeserializer));
}

objectMapper.registerModule(customJacksonModule);
return objectMapper;
}

public JacksonNonBlockingObjectMapperFactory setJsonDeserializers(List<StdDeserializer> _jsonDeserializers){
this.jsonDeserializers = _jsonDeserializers;
return this;
}
}

然后像这样调用它(只传递那些你想要非阻塞的反序列化器):

JacksonNonBlockingObjectMapperFactory factory = new JacksonNonBlockingObjectMapperFactory();
factory.setJsonDeserializers(Arrays.asList(new StdDeserializer[]{
// StdDeserializer, here, comes from Jackson (org.codehaus.jackson.map.deser.StdDeserializer)
new StdDeserializer.ShortDeserializer(Short.class, null),
new StdDeserializer.IntegerDeserializer(Integer.class, null),
new StdDeserializer.CharacterDeserializer(Character.class, null),
new StdDeserializer.LongDeserializer(Long.class, null),
new StdDeserializer.FloatDeserializer(Float.class, null),
new StdDeserializer.DoubleDeserializer(Double.class, null),
new StdDeserializer.NumberDeserializer(),
new StdDeserializer.BigDecimalDeserializer(),
new StdDeserializer.BigIntegerDeserializer(),
new StdDeserializer.CalendarDeserializer()
}));
ObjectMapper om = factory.createObjectMapper();

关于java - jackson 反序列化错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9080904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com