如何在 Java 中创建自定义 JsonDeserializer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5609321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:53:27  来源:igfitidea点击:

how to create a custom JsonDeserializer in Java?

javajsonHymansondeserialization

提问by Neuquino

I have a Map<A,B> fieldOfCas a field of a class C. When I try to deserialize C with Hymanson, 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.

我有一个Map<A,B> fieldOfCC 类的字段。当我尝试用 Hymanson 反序列化 C 时,会抛出异常,因为它找不到 Map 的键 A 的反序列化器。所以,我想解决方案是扩展 StdJsonDeserializer 并执行它手动。
我的问题是我找不到关于如何使用解析器和我必须实现的“反序列化”方法的上下文的示例。

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 testof the same problem I have. This is exactly my problem

编辑:谷歌搜索,我发现了我遇到的同样问题的测试。这正是我的问题

EDIT: changed extended class from StdDeserializer to StdKeyDeserializer as I read herein method findKeyDeserializer(org.codehaus.Hymanson.map.DeserializationConfig, org.codehaus.Hymanson.type.JavaType, org.codehaus.Hymanson.map.BeanProperty)

编辑:改变扩展类从StdDeserializer到StdKeyDeserializer当我读到这里的方法findKeyDeserializer(org.codehaus.Hymanson.map.DeserializationConfig, org.codehaus.Hymanson.type.JavaType, org.codehaus.Hymanson.map.BeanProperty)

EDIT: After solving this issue I got this onethat is related.

编辑:解决这个问题后,我得到了一个相关的。

采纳答案by Simon G.

I am a complete newbie with Hymanson, but the following works for me.

我是Hyman逊的完全新手,但以下内容对我有用。

First I add a JsonCreator method to A:

首先我向 A 添加一个 JsonCreator 方法:

public class A {
    private String a1;
    private Integer a2;
    public String getA1() { return a1; }
    public Integer getA2() { return a2; }
    public void setA1(String a1) { this.a1 = a1; }
    public void setA2(Integer a2) { this.a2 = a2; }

    @JsonCreator
    public static A fromJSON(String val) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        A a = mapper.readValue(val,A.class);
        return 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:

仅此一项就解决了反序列化问题。对我来说更难的部分是正确序列化密钥。我在那里所做的是定义一个关键序列化器,该序列化器将命名类序列化为 JSON 序列化,如下所示:

public class KeySerializer extends SerializerBase<Object> {
    private static final SerializerBase<Object> DEFAULT = new StdKeySerializer();

    private Set<Class<?>> objectKeys_ = Collections.synchronizedSet(new HashSet<Class<?>>());

    protected KeySerializer(Class<?>... objectKeys) {
        super(Object.class);
        for(Class<?> cl:objectKeys) {
            objectKeys_.add(cl);
        }
    }


    @Override
    public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
        return DEFAULT.getSchema(provider, typeHint);
    }


    @Override
    public void serialize(Object value, JsonGenerator jgen,
            SerializerProvider provider) throws IOException,
            JsonGenerationException {
        if (objectKeys_.contains(value.getClass())) {
            ObjectMapper mapper = new ObjectMapper();
            StringWriter writer = new StringWriter();
            mapper.writeValue(writer, value);
            jgen.writeFieldName(writer.toString());
        } else {
            DEFAULT.serialize(value, jgen, provider);
        }
    }
}

Then to prove it works, serializing and deserializing an instance of class C:

然后为了证明它有效,序列化和反序列化 C 类的实例:

    ObjectMapper mapper = new ObjectMapper();
    StdSerializerProvider provider = new StdSerializerProvider();
    provider.setKeySerializer(new KeySerializer(A.class));
    mapper.setSerializerProvider(provider);

    StringWriter out = new StringWriter();
    mapper.writeValue(out, c);
    String json = out.toString();
    System.out.println("JSON= "+json);

    C c2 = mapper.readValue(json, C.class);
    System.out.print("C2= ");
    StringWriter outC2 = new StringWriter();
    mapper.writeValue(outC2, c2);
    System.out.println(outC2.toString());

For me this produced the output:

对我来说,这产生了输出:

JSON= {"c1":"goo","map":{"{\"a1\":\"1ccf\",\"a2\":7376}":{"b1":"5ox"},"{\"a1\":\"1cd2\",\"a2\":7379}":{"b1":"5p0"},"{\"a1\":\"1cd5\",\"a2\":7382}":{"b1":"5p3"},"{\"a1\":\"1cd8\",\"a2\":7385}":{"b1":"5p6"}}}
C2= {"c1":"goo","map":{"{\"a1\":\"1ccf\",\"a2\":7376}":{"b1":"5ox"},"{\"a1\":\"1cd2\",\"a2\":7379}":{"b1":"5p0"},"{\"a1\":\"1cd5\",\"a2\":7382}":{"b1":"5p3"},"{\"a1\":\"1cd8\",\"a2\":7385}":{"b1":"5p6"}}}

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.

我觉得应该有更好的方法来说明如何使用注释序列化密钥,但我无法解决。