我有一个包含Map(带非String键)和其他一些字段的Class。
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 modules。
我跟着this post并扩展了JsonDeserializer和JsonSerializer。问题是应该键入这些类,所以它应该看起来像
public class keyDeserializer extends JsonDeserializer<Map<KeyObject, OtherObject>> {
...
}
KeySerializer也一样。
然后添加到模块:
module.addSerializer(new keySerializer());
module.addDeserializer(Map.class, new keyDeserializer());
但这显然是错误的,因为我得到了例外:
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 ,但是我必须手动解析所有这些,这是不合理的。
更新
我设法通过使用注释
绕过代码中的模块创建@JsonDeserialize(using = keyDeserializer.class)
@JsonSerialize(using = keySerializer.class)
private Map<KeyObject, OtherObject> map;
但是我必须自己从toString()输出序列化/反序列化整个地图结构。所以尝试了不同的注释:
@JsonDeserialize(keyUsing = MyKeyDeserializer.class)
private Map<KeyObject, OtherObject> map;
MyKeyDeserializer扩展org.codehaus.jackson.map.KeyDeserializer
并覆盖方法
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {...}
然后从我的密钥类的toString()输出手动反序列化我的密钥。
这不是最佳的(这种依赖于toString()方法)。还有更好的方法吗?
答案 0 :(得分:11)
结束使用此序列化程序:
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;
这是对我有用的解决方案,希望这有助于其他人。