如何在 Java 中创建自定义 JsonDeserializer?
我有一个 Map; fieldOfC
作为类 C 的字段。当我尝试用 Jackson 反序列化 C 时,会抛出异常,因为它找不到 Map 的键 A 的反序列化器。所以,我想解决方案是扩展 StdJsonDeserializer 和手动执行。
我的问题是,我找不到有关如何使用解析器以及我必须实现的方法“反序列化”的上下文的示例。
任何人都可以编写这个简单示例的代码,以便我可以用它作为构建真正的反序列化器的开始吗?
public class A{
private String a1;
private Integer a2;
}
public class B{
private String b1;
}
public class C{
@JsonDeserialize(keyUsing=ADeserializer.class)
//also tried this: @JsonDeserialize(keyAs=A.class) without success
private Map<A,B> fieldOfC;
private String c1;
}
public class ADeserializer extends StdKeyDeserializer {
protected ADeserializer(Class<A> cls) {
super(cls);
}
protected Object _parse(String key, DeserializationContext ctxt) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(key, A.class);
}
}
预先感谢
编辑:谷歌搜索,我找到了测试我也有同样的问题。这正是我的问题
编辑:当我阅读此处 在方法中findKeyDeserializer(org.codehaus.jackson.map.DeserializationConfig, org.codehaus.jackson.type.JavaType, org.codehaus.jackson.map.BeanProperty)
编辑:解决后这个问题我得到了相关的这个。
I have a Map<A,B> fieldOfC
as a field of a class C. When I try to deserialize C with Jackson, an Exception is thrown because it can't find a Deserializer for Map's key A. So, I guess the solution is to extend StdJsonDeserializer and do it manually.
My problem is that I can't find an example on how to use the parser and the context of the method "deserialize" that I have to implement.
Can anyone write the code for this simple example so I can use it as a start to build my real deserializer?
public class A{
private String a1;
private Integer a2;
}
public class B{
private String b1;
}
public class C{
@JsonDeserialize(keyUsing=ADeserializer.class)
//also tried this: @JsonDeserialize(keyAs=A.class) without success
private Map<A,B> fieldOfC;
private String c1;
}
public class ADeserializer extends StdKeyDeserializer {
protected ADeserializer(Class<A> cls) {
super(cls);
}
protected Object _parse(String key, DeserializationContext ctxt) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(key, A.class);
}
}
Thanks in advance
EDIT: googling, I found a test of the same problem I have. This is exactly my problem
EDIT: changed extended class from StdDeserializer to StdKeyDeserializer as I read here in method findKeyDeserializer(org.codehaus.jackson.map.DeserializationConfig, org.codehaus.jackson.type.JavaType, org.codehaus.jackson.map.BeanProperty)
EDIT: After solving this issue I got this one that is related.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对杰克逊来说是个新手,但以下内容对我有用。
首先,我向 A 添加一个 JsonCreator 方法:
仅此一项就解决了反序列化问题。对我来说最困难的部分是密钥的正确序列化。我所做的就是定义一个关键序列化器,该序列化器将命名类序列化为 JSON 序列化,如下所示:
然后为了证明它有效,序列化和反序列化类 C 的实例:
对我来说,这产生了输出:
我觉得应该有是说明如何使用注释序列化密钥的更好方法,但我无法解决。
I am a complete newbie with Jackson, but the following works for me.
First I add a JsonCreator method to A:
That alone solves the deserialization problem. The harder part for me was the correct serialization of the keys. What I did there was to define a key serializer that serializes named classes as there JSON serialization, like this:
Then to prove it works, serializing and deserializing an instance of class C:
For me this produced the output:
I feel there ought to have been a better way of doing saying how to serialize the key by using annotations, but I could not work it out.