杰克逊地图序列化模块 [英] Jackson Modules for Map Serialization
问题描述
我有一个包含Map(带非String键)和其他一些字段的Class。
I have a Class that contains a Map (with non String key) and some other fields.
public class MyClass() {
private Map<KeyObject, OtherObject> map;
private String someField;
public MyClass(Map<KeyObject, OtherObject> map, String someField) {
this.map = map;
this.someField = someField;
}
// Getters & Setters
}
我想使用Jackson序列化和反序列化这个类。
我看到了不同的方法,并决定尝试使用 jackson模块。
I would like to serialize and deserialize this class using Jackson. I saw a different ways of doing that and decided to try using jackson modules.
我跟着这篇文章并扩展了JsonDeserializer和JsonSerializer。问题是这些类应该键入,所以它应该看起来像
I followed this post and extended JsonDeserializer and JsonSerializer. The problem is that those classes should be typed, so it should look like
public class keyDeserializer extends JsonDeserializer<Map<KeyObject, OtherObject>> {
...
}
KeySerializer的相同内容。
The same for the KeySerializer.
然后添加到模块:
module.addSerializer(new keySerializer());
module.addDeserializer(Map.class, new keyDeserializer());
但这显然是错误的,因为我得到了一个例外:
But this is wrong apparently since I'm getting an exception:
keySerializer does not define valid handledType() -- must either register with method that takes type argument or make serializer extend 'org.codehaus.jackson.map.ser.std.SerializerBase'
我可以将我的序列化器和反序列化器输入 MyClass ,但后来我不得不手动解析所有这些,这是不合理的。
I could have my serializer and deserializer to be typed to MyClass, but then I had to manually parse all of it, which is not reasonable.
更新:
我设法通过使用注释绕过了代码中的模块创建
I managed to bypass the module creation in the code by using annotations
@JsonDeserialize(using = keyDeserializer.class)
@JsonSerialize(using = keySerializer.class)
private Map<KeyObject, OtherObject> map;
但是我必须自己从toString()输出序列化/反序列化整个地图结构。所以尝试了不同的注释:
But then I have to serialize/deserialize the whole map structure on my own from the toString() output. So tried a different annotation:
@JsonDeserialize(keyUsing = MyKeyDeserializer.class)
private Map<KeyObject, OtherObject> map;
MyKeyDeserializer扩展 org.codehaus.jackson.map.KeyDeserializer
并覆盖方法
Where MyKeyDeserializer extends org.codehaus.jackson.map.KeyDeserializer
and overriding the method
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {...}
然后再次从我的密钥类的toString()输出反序列化我的密钥。
Then manually deserializing my key but again from the toString() output of my key class.
这不是最佳的(这种对toString()方法的依赖)。有更好的方法吗?
This is not optimal (this dependency on the toString() method). Is there a better way?
推荐答案
结束使用此序列化程序:
Ended up using this serializer:
public class MapKeySerializer extends SerializerBase<Object> {
private static final SerializerBase<Object> DEFAULT = new StdKeySerializer();
private static final ObjectMapper mapper = new ObjectMapper();
protected MapKeySerializer() {
super(Object.class);
}
@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 (null == value) {
throw new JsonGenerationException("Could not serialize object to json, input object to serialize is null");
}
StringWriter writer = new StringWriter();
mapper.writeValue(writer, value);
jgen.writeFieldName(writer.toString());
}
}
此解串器:
public class MapKeyDeserializer extends KeyDeserializer {
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return mapper.readValue(key, MyObject.class);
}
}
注释我的地图:
@JsonDeserialize(keyUsing = MapKeyDeserializer.class)
@JsonSerialize(keyUsing = MapKeySerializer.class)
private Map<KeyObject, OtherObject> map;
这是对我有用的解决方案,希望这有助于其他人。
This is the solution that worked for me, hope this helps other.
这篇关于杰克逊地图序列化模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!