gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:06:25 26 4
gpt4 key购买 nike

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

任何人都可以为这个简单的示例编写代码,以便我可以将它用作构建我真正的反序列化器的开始吗?

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);
}
}

提前致谢

编辑:谷歌搜索,我发现了一个 test我有同样的问题。这正是我的问题

编辑:在我阅读 here 时,将扩展类从 StdDeserializer 更改为 StdKeyDeserializer在方法中findKeyDeserializer(org.codehaus.jackson.map.DeserializationConfig, org.codehaus.jackson.type.JavaType, org.codehaus.jackson.map.BeanProperty)

编辑:解决这个问题后我得到了this one这是相关的。

最佳答案

我是 Jackson 的新手,但以下内容对我有用。

首先我向 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;
}
}

仅此一项就解决了反序列化问题。对我来说更难的部分是 key 的正确序列化。我在那里所做的是定义一个 key 序列化程序,它像 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);
}
}
}

然后为了证明它有效,序列化和反序列化类 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());

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

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"}}}

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

关于java - 如何在 Java 中创建自定义 JsonDeserializer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5609321/

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