杰克逊 - 将地图实现反序列化为HashMap

时间:2022-02-22 18:02:16

So, I have configuration

所以,我有配置

.defaultTyping(NON_FINAL)

which is writing correctly serialized json as:

这是正确编写序列化json为:

"headers": [
        "org.springframework.messaging.MessageHeaders",
        {

Now, the problem is that MessageHeaders class is implementing Map, but also overrides put method so it throws an exception. In order to properly deserialize this I would need eaither to be able to serialize this as HashMap, so:

现在,问题是MessageHeaders类正在实现Map,但是也会覆盖put方法,因此会抛出异常。为了正确反序列化,我需要能够将其序列化为HashMap,因此:

"headers": [
        "java.util.HashMap",
        {

or to be able to explicitly deserialize MessageHeaders as HashMap (since it is actually just Map.

或者能够将MessageHeaders显式反序列化为HashMap(因为它实际上只是Map。

Once more question is: how to serialize Object implementing Map as HashMap, or how to have Object implementing Map deserialized as HashMap.

再一个问题是:如何序列化Object实现Map作为HashMap,或者如何让Object实现Map反序列化为HashMap。

1 个解决方案

#1


0  

Ok, i found how to do this with custom serializer (the trick was to tell serializer to use java.util.HashMap instead of the real class name):

好吧,我发现如何使用自定义序列化程序(诀窍是告诉序列化程序使用java.util.HashMap而不是真正的类名):

public class MessageHeadersJsonSerializer extends JsonSerializer<MessageHeaders>{
    @Override
    public void serializeWithType(MessageHeaders value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
        typeSer.writeTypePrefixForObject(value, gen, HashMap.class);
        serialize(value, gen, serializers);
        typeSer.writeTypeSuffixForObject(value, gen);
    }

    @Override
    public void serialize(MessageHeaders value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        for(Map.Entry<String, Object> entry : value.entrySet()){
            gen.writeFieldName(entry.getKey());
            gen.writeObject(entry.getValue());
        }
    }
}

And then just registered it as a MixIn:

然后将其注册为MixIn:

@JsonSerialize(using = MessageHeadersJsonSerializer.class)
public abstract class MessageHeadersMixIn {}

And on parent object I'm deserializing it as HashMap:

在父对象上我将它反序列化为HashMap:

public abstract class GenericMessageMixIn<T> {
    @JsonCreator
    public GenericMessageMixIn(
            @JsonProperty("payload") T payload,
            @JsonProperty("headers") Map<String, Object> headers
    ){}
}

And finally all works OK!

最后一切正常!

#1


0  

Ok, i found how to do this with custom serializer (the trick was to tell serializer to use java.util.HashMap instead of the real class name):

好吧,我发现如何使用自定义序列化程序(诀窍是告诉序列化程序使用java.util.HashMap而不是真正的类名):

public class MessageHeadersJsonSerializer extends JsonSerializer<MessageHeaders>{
    @Override
    public void serializeWithType(MessageHeaders value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
        typeSer.writeTypePrefixForObject(value, gen, HashMap.class);
        serialize(value, gen, serializers);
        typeSer.writeTypeSuffixForObject(value, gen);
    }

    @Override
    public void serialize(MessageHeaders value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        for(Map.Entry<String, Object> entry : value.entrySet()){
            gen.writeFieldName(entry.getKey());
            gen.writeObject(entry.getValue());
        }
    }
}

And then just registered it as a MixIn:

然后将其注册为MixIn:

@JsonSerialize(using = MessageHeadersJsonSerializer.class)
public abstract class MessageHeadersMixIn {}

And on parent object I'm deserializing it as HashMap:

在父对象上我将它反序列化为HashMap:

public abstract class GenericMessageMixIn<T> {
    @JsonCreator
    public GenericMessageMixIn(
            @JsonProperty("payload") T payload,
            @JsonProperty("headers") Map<String, Object> headers
    ){}
}

And finally all works OK!

最后一切正常!