Java Jackson:反序列化为 Map<String, Object>,每个值的类型都正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27871226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:11:30  来源:igfitidea点击:

Hymanson: Deserialize to a Map<String, Object> with correct type for each value

javajsonspringHymanson

提问by Michael Minella

I have a class that looks like the following

我有一个如下所示的课程

public class MyClass {
   private String val1;
   private String val2;
   private Map<String,Object> context;
   // Appropriate accessors removed for brevity.
   ...
}

I'm looking to be able to make the round trip with Hymanson from object to JSON and back. I can serialize the object above fine and receive the following output:

我希望能够与 Hymanson 从对象到 JSON 再返回。我可以很好地序列化上面的对象并收到以下输出:

{
    "val1": "foo",
    "val2": "bar",
    "context": {
        "key1": "enumValue1",
        "key2": "stringValue1",
        "key3": 3.0
    }
}

The issue I'm running into is that since the values in the serialized map do not have any type information, they are not deserialized correctly. For example, in the sample above, enumValue1 should be deserialized as an enum value but is instead deserialized as a String. I've seen examples for basing what type on a variety of things, but in my scenario, I won't know what the types are (they will be user generated objects that I won't know in advance) so I need to be able to serialize the type information with the key value pair. How can I accomplish this with Hymanson?

我遇到的问题是,由于序列化映射中的值没有任何类型信息,因此它们没有正确反序列化。例如,在上面的示例中, enumValue1 应该反序列化为枚举值,但反序列化为字符串。我已经看到了基于各种事物的类型的示例,但在我的场景中,我不知道类型是什么(它们将是我事先不知道的用户生成的对象)所以我需要能够使用键值对序列化类型信息。我怎样才能和Hyman逊一起做到这一点?

For the record, I'm using Hymanson version 2.4.2. The code I'm using to test the round trip is as follows:

作为记录,我使用的是 Hymanson 2.4.2 版。我用来测试往返的代码如下:

@Test
@SuppressWarnings("unchecked")
public void testJsonSerialization() throws Exception {
    // Get test object to serialize
    T serializationValue = getSerializationValue();
    // Serialize test object
    String json = mapper.writeValueAsString(serializationValue);
    // Test that object was serialized as expected
    assertJson(json);
    // Deserialize to complete round trip
    T roundTrip = (T) mapper.readValue(json, serializationValue.getClass());
    // Validate that the deserialized object matches the original one
    assertObject(roundTrip);
}

Since this is a Spring based project, the mapper is being created as follows:

由于这是一个基于 Spring 的项目,映射器的创建方式如下:

@Configuration
public static class SerializationConfiguration {

    @Bean
    public ObjectMapper mapper() {
        Map<Class<?>, Class<?>> mixins = new HashMap<Class<?>, Class<?>>();
        // Add unrelated MixIns
        .. 

        return new Hymanson2ObjectMapperBuilder()
                .featuresToDisable(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)
                .dateFormat(new ISO8601DateFormatWithMilliSeconds())
                .mixIns(mixins)
                .build();
    }
}

采纳答案by fonkap

I think the simplest way of achieve what you want is using:

我认为实现您想要的最简单方法是使用:

ObjectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

This will add type information in the serialized json.

这将在序列化的 json 中添加类型信息。

Here you are a running example, that you will need to adapt to Spring:

这是一个正在运行的示例,您需要适应 Spring:

public class Main {

    public enum MyEnum {
        enumValue1
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        MyClass obj = new MyClass();
        obj.setContext(new HashMap<String, Object>());

        obj.setVal1("foo");
        obj.setVal2("var");
        obj.getContext().put("key1", "stringValue1");
        obj.getContext().put("key2", MyEnum.enumValue1);
        obj.getContext().put("key3", 3.0);

        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);

        System.out.println(json);

        MyClass readValue = mapper.readValue(json, MyClass.class);
        //Check the enum value was correctly deserialized
        Assert.assertEquals(readValue.getContext().get("key2"), MyEnum.enumValue1);
    }

}

The object will be serialized into something similar to:

该对象将被序列化为类似于:

[ "so_27871226.MyClass", {
  "val1" : "foo",
  "val2" : "var",
  "context" : [ "java.util.HashMap", {
    "key3" : 3.0,
    "key2" : [ "so_27871226.Main$MyEnum", "enumValue1" ],
    "key1" : "stringValue1"
  } ]
} ]

And will be deserialized back correctly, and the assertion will pass.

并且将被正确反序列化,并且断言将通过。

Bytheway there are more ways of doing this, please look at https://github.com/FasterXML/Hymanson-docs/wiki/HymansonPolymorphicDeserializationfor more info.

顺便说一下,还有更多方法可以做到这一点,请查看https://github.com/FasterXML/Hymanson-docs/wiki/HymansonPolymorphicDeserialization了解更多信息。

I hope it will help.

我希望它会有所帮助。