JSon - 嵌套映射的自定义键序列化

时间:2017-10-19 18:14:58

标签: java json serialization jackson

我有一个嵌套的Map<StructureNode, Map<String, String>>我需要一个自定义密钥序列化器&amp;反序列化器(StructureNode包含对作为此映射的键所需的其他对象的引用)。我使用了以下方法:

Jackson Modules for Map Serialization

给出以下结果。自定义序列化器:

public class StructureNodeKeySerializer extends JsonSerializer<StructureNode> {

    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public void serialize(StructureNode value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        StringWriter writer = new StringWriter();
        mapper.writeValue(writer, value.copyUpwards());
        gen.writeFieldName(writer.toString());
    }
}

自定义反序列化器:

public class StructureNodeKeyDeserializer extends KeyDeserializer  {

    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException {
        return mapper.readValue(key, StructureNode.class);
    }
}

用法:

@JsonDeserialize(keyUsing = StructureNodeKeyDeserializer.class) @JsonSerialize(keyUsing = StructureNodeKeySerializer.class)
private Map<StructureNode, String> structureIds;
@JsonDeserialize(keyUsing = StructureNodeKeyDeserializer.class) @JsonSerialize(keyUsing = StructureNodeKeySerializer.class)
private Map<StructureNode, Map<String, String>> metadata;

这正确地序列化Map<StructureNode, String>,但应用于嵌套的Map<StructureNode, Map<String, String>>,它会出现以下错误:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: java.lang.String cannot be cast to structure.StructureNode

杰克逊似乎对“子地图”使用了相同的自定义序列化方法。有没有一种很好的方法来解决这个问题,而不用另一个自定义(非Map)对象替换“子地图”?

2 个答案:

答案 0 :(得分:2)

您可以使用

解决此问题
public static class Bean{
    @JsonSerialize(using = MapStructureNodeKeySerializer.class)
    public Map<StructureNode, Map<String, String>> metadata;
}

以稍微不同的方式实现您的序列化程序:

public static class MapStructureNodeKeySerializer 
        extends JsonSerializer<Map<StructureNode, Object>> {

    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public void serialize(Map<StructureNode, Object> value, JsonGenerator gen, 
                          SerializerProvider serializers) throws IOException {
        gen.writeStartObject();

        for(Map.Entry<StructureNode, Object> val: value.entrySet()){
            // your custom serialization code here
            StringWriter writer = new StringWriter();
            mapper.writeValue(writer, val.getKey().copyUpwards());

            gen.writeObjectField(writer.toString(), val.getValue());
        }

        gen.writeEndObject();
    }
}

或者,如果您想保留keyUsing = StructureNodeKeySerializer.class

public static class Bean{
    @JsonSerialize(keyUsing = StructureNodeKeySerializer.class)
    public Map<StructureNode, Map<String, String>> metadata;
}

您可以像下面这样实现:

public static class StructureNodeKeySerializer extends JsonSerializer {

    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public void serialize(Object value, JsonGenerator gen,
                          SerializerProvider serializers) throws IOException {

        if (value instanceof StructureNode){ // <= type of 1-st level Map key
            // your custom serialization code here
            StringWriter writer = new StringWriter();
            mapper.writeValue(writer, ((StructureNode)value).copyUpwards());
            gen.writeFieldName(writer.toString());
        }else if(value instanceof String){   // <= type of 2-nd level Map key
            gen.writeFieldName((String) value);
        }
    }
}

答案 1 :(得分:0)

如果你想更一般地将它序列化为keySerializer,你可以如下重写else子句

if (value instanceof StructureNode) {
  // ...
} else {
  serializers
    .findKeySerializer(value.class, null)
    .serialize(value, gen, serializers);
}