序列化和反序列化Map <Object,Object> Jackson

时间:2019-11-26 16:59:45

标签: java json serialization jackson

我想使用杰克逊(2.9.10)使用其ID引用对象的JSON序列化/反序列化以下类。

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Key {

    @JsonProperty("id")
    private String id;
    @JsonProperty("random_field_key")
    private boolean randomFieldKey;

    // Getters and setters
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Value {

    @JsonProperty("id")
    private String id;
    @JsonProperty("random_field_value")
    private boolean randomFieldValue;

    // Getters and setters
}
@JsonPropertyOrder({"keys", "values", "relationships"})
public class Relationship {
    @JsonProperty("keys")
    private List<Key> keys;
    @JsonProperty("values")
    private List<Value> values;
    @JsonProperty("relationships")
    private Map<Key, Value> relationships;

    // Getters and setters
}
public class Main {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws IOException {


        Value value = new Value();
        value.setId("valueId");
        value.setRandomFieldValue(true);

        Key key = new Key();
        key.setId("keyId");
        key.setRandomFieldKey(false);

        Map<Key, Value> map = new HashMap<Key, Value>();
        map.put(key, value);

        Relationship relationship = new Relationship();
        relationship.setKeys(Collections.singletonList(key));
        relationship.setValues(Collections.singletonList(value));
        relationship.setRelationships(map);

        String serialisedRelationship = mapper.writeValueAsString(relationship);
        Relationship deserialisedRelationship = mapper.readValue(serialisedRelationship, Relationship.class);

    }
}

预期结果如下:

{
    "keys": [
        {
            "id": "keyId",
            "random_field_key": false
        }
    ],
    "values": [
        {
            "id": "valueId",
            "random_field_value": true
        }
    ],
    "relationships": {
        "keyId": "valueId"
    }
}

相反,我得到:

{
    "keys": [
        {
            "id": "keyId",
            "random_field_key": false
        }
    ],
    "values": [
        {
            "id": "valueId",
            "random_field_value": true
        }
    ],
    "relationships": {
        "util.teststack.Key@8646db9": "valueId"
    }
}

我添加了一个自定义序列化程序,该序列化程序允许将Key序列化为一个ID(我认为JsonIdentityInfo批注会解决这个问题)并设法获得预期的结果。

public class KeySerializer extends JsonSerializer<Key> {

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

        gen.writeFieldName(value.getId());
    }
}
@JsonPropertyOrder({"keys", "values", "relationships"})
public class Relationship {
    @JsonProperty("keys")
    private List<Key> keys;
    @JsonProperty("values")
    private List<Value> values;
    @JsonSerialize(keyUsing = KeySerializer.class)
    @JsonProperty("relationships")
    private Map<Key, Value> relationships;

    // Getters and setters
}

我似乎无法反序列化JSON,但总是会收到异常:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot find a (Map) Key deserializer for type [simple type, class util.teststack.Key]

尽管添加自定义反序列化器也是一个好主意,但我一直在努力理解如何引用“外部”(已实例化)Key实例。我似乎在DeserializerContext内部所做的只是实例化一个新的Key,而不是引用以前创建的Key。

检查DeserializerContext我可以看到我需要的所有实例都在那儿,但是我不知道如何引用(并返回它们)。 令人惊讶的是,没有序列化器/反序列化器,“映射值”的引用可以正常工作。

public class KeyDeserializer extends com.fasterxml.jackson.databind.KeyDeserializer {

    @Override
    public Key deserializeKey(
            String key,
            DeserializationContext ctxt) {

        Key newKey = new Key();
        newKey.setId(key);

        return newKey;
    }
}
@JsonPropertyOrder({"keys", "values", "relationships"})
public class Relationship {
    @JsonProperty("keys")
    private List<Key> keys;
    @JsonProperty("values")
    private List<Value> values;
    @JsonSerialize(keyUsing = KeySerializer.class)
    @JsonDeserialize(keyUsing = KeyDeserializer.class)
    @JsonProperty("relationships")
    private Map<Key, Value> relationships;
}

Debugger

我实施了可接受的答案以这种方式解决问题:

public class KeyDeSerializer extends com.fasterxml.jackson.databind.KeyDeserializer {

    @Override
    public Key deserializeKey(String key, DeserializationContext ctxt) {
        Relationship obj = (Relationship) ctxt.getParser().getParsingContext().getParent().getCurrentValue();
        return getKey(key, obj);
    }

    private Key getKey(String key, Relationship obj) {
        for (Key ck : obj.getKeys()) {
            if (ck.getId().equals(key)) {
                return ck;
            }
        }
        return null;
    }
}

2 个答案:

答案 0 :(得分:1)

能否请您尝试覆盖Key类中的toString()方法并返回“ id”的值。

public String toString() {
  this.id;
}

希望这会有所帮助!

答案 1 :(得分:0)

在对象 DeserializationContext 中,您可以检索您的 Relationship 对象。

尝试这样的事情

ctxt.getParser().getParsingContext().getParent().getCurrentValue();

从该对象中,您可以检索所需的实例