Java 使用 jackson 反序列化为自定义对象的 HashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18002132/
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 21:41:33  来源:igfitidea点击:

Deserializing into a HashMap of custom objects with Hymanson

javaHymansonjson-deserialization

提问by wbj

I have the following class:

我有以下课程:

import org.codehaus.Hymanson.annotate.JsonIgnoreProperties;
import org.codehaus.Hymanson.annotate.JsonProperty;

import java.io.Serializable;
import java.util.HashMap;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Theme implements Serializable {

    @JsonProperty
    private String themeName;

    @JsonProperty
    private boolean customized;

    @JsonProperty
    private HashMap<String, String> descriptor;

    //...getters and setters for the above properties
}

When I execute the following code:

当我执行以下代码时:

    HashMap<String, Theme> test = new HashMap<String, Theme>();
    Theme t1 = new Theme();
    t1.setCustomized(false);
    t1.setThemeName("theme1");
    test.put("theme1", t1);

    Theme t2 = new Theme();
    t2.setCustomized(true);
    t2.setThemeName("theme2");
    t2.setDescriptor(new HashMap<String, String>());
    t2.getDescriptor().put("foo", "one");
    t2.getDescriptor().put("bar", "two");
    test.put("theme2", t2);
    String json = "";
    ObjectMapper mapper = objectMapperFactory.createObjectMapper();
    try {
        json = mapper.writeValueAsString(test);
    } catch (IOException e) {
        e.printStackTrace(); 
    }

The json string produced looks like this:

生成的 json 字符串如下所示:

{
  "theme2": {
    "themeName": "theme2",
    "customized": true,
    "descriptor": {
      "foo": "one",
       "bar": "two"
    }
  },
  "theme1": {
    "themeName": "theme1",
    "customized": false,
    "descriptor": null
  }
}

My problem is getting the above json string to de-serizlize back into a

我的问题是让上面的 json 字符串去序列化回一个

HashMap<String, Theme> 

object.

目的。

My de-serialization code looks like this:

我的反序列化代码如下所示:

HashMap<String, Themes> themes =
        objectMapperFactory.createObjectMapper().readValue(json, HashMap.class);

Which de-serializes into a HashMap with the correct keys, but does not create Theme objects for the values. I don't know what to specify instead of "HashMap.class" in the readValue() method.

它使用正确的键反序列化为 HashMap,但不会为值创建 Theme 对象。我不知道在 readValue() 方法中指定什么而不是“HashMap.class”。

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Micha? Ziober

You should create specific Map type and provide it into deserialization process:

您应该创建特定的 Map 类型并将其提供给反序列化过程:

TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Theme.class);
HashMap<String, Theme> map = mapper.readValue(json, mapType);

回答by user2824471

You can use TypeReference class which does the type casting for map with user defined types. More documentation at https://github.com/FasterXML/Hymanson-databind/

您可以使用 TypeReference 类,它使用用户定义的类型对地图进行类型转换。更多文档请访问https://github.com/FasterXML/Hymanson-databind/

ObjectMapper mapper = new ObjectMapper();
Map<String,Theme> result =
  mapper.readValue(src, new TypeReference<Map<String,Theme>>() {});

回答by 00500005

You can make a POJO that extends a Map.

您可以制作一个扩展 Map 的 POJO。

This is important for dealing with nested maps of objects.

这对于处理对象的嵌套映射很重要。

{
  key1: { nestedKey1: { value: 'You did it!' } }
}

This can be deserialized via:

这可以通过以下方式反序列化:

class Parent extends HashMap<String, Child> {}

class Child extends HashMap<String, MyCoolPojo> {}

class MyCoolPojo { public String value; }

Parent parent = new ObjectMapper().readValue(json, Parent.class);
parent.get("key1").get("nestedKey1").value; // "You did it!"