我有一个可以输出以下任何内容的类:
(注意每种情况下判断的不同之处:它可以是未定义的,null或值)
该课程如下:
class Claim {
String title;
Nullable<String> judgment;
}
和Nullable是这样的:
class Nullable<T> {
public T value;
}
使用自定义序列化程序:
SimpleModule module = new SimpleModule("NullableSerMod", Version.unknownVersion());
module.addSerializer(Nullable.class, new JsonSerializer<Nullable>() {
@Override
public void serialize(Nullable arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException, JsonProcessingException {
if (arg0 == null)
return;
arg1.writeObject(arg0.value);
}
});
outputMapper.registerModule(module);
摘要:此设置允许我输出值,或null,或undefined 。
现在,我的问题:如何编写相应的反序列化器?
我想它看起来像这样:
SimpleModule module = new SimpleModule("NullableDeserMod", Version.unknownVersion());
module.addDeserializer(Nullable.class, new JsonDeserializer<Nullable<?>>() {
@Override
public Nullable<?> deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
if (next thing is null)
return new Nullable(null);
else
return new Nullable(parser.readValueAs(inner type));
}
});
但我不知道该为“下一件事是空的”或“内部类型”放什么。
关于我如何做到这一点的任何想法?
谢谢!
答案 0 :(得分:7)