杰克逊对象映射器将 java 映射转换为 json 维护键的顺序

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

Hymanson Object mapper to convert java map to json maintaining order of keys

javajsonHymanson

提问by Ani

I am using Hymanson.map.ObjectMapperAPI to convert map to json string. I am using writeValueAsString method to achieve this.

我正在使用Hymanson.map.ObjectMapperAPI 将地图转换为 json 字符串。我正在使用 writeValueAsString 方法来实现这一点。

I pass a map sorted on the basis of values to writeValueAsStringmethod. The JSON string which I get is resorted on the basis of keys.

我将根据值排序的地图传递给writeValueAsString方法。我得到的 JSON 字符串是基于键的。

Is there a way to convert maps to JSON string using Hymanson without disturbing the order of items in the map.

有没有办法使用 Hymanson 将地图转换为 JSON 字符串,而不会影响地图中项目的顺序。

I tried setting Feature.SORT_PROPERTIES_ALPHABETICALLYto false, but as per documentation it is applicable only for POJO types.

我尝试设置Feature.SORT_PROPERTIES_ALPHABETICALLY为 false,但根据文档,它仅适用于 POJO 类型。

Any idea to implement the said behavior.

实现上述行为的任何想法。

回答by pdem

with Hymanson 2.3.1 (don't know for previous versions) you can serialize a SortedMap, for example a TreeMap, the order will be respected.

使用 Hymanson 2.3.1(不知道以前的版本)您可以序列化 SortedMap,例如 TreeMap,顺序将被尊重。

Here is an exempale in junit 4:

这是junit 4中的一个示例:

    @Test
public void testSerialize() throws JsonProcessingException{
    ObjectMapper om = new ObjectMapper();
    om.configure(SerializationFeature.WRITE_NULL_MAP_VALUES,false);
    om.configure(SerializationFeature.INDENT_OUTPUT,true);
    om.setSerializationInclusion(Include.NON_NULL);

    SortedMap<String,String> sortedMap = new TreeMap<String,String>();
    Map<String,String> map = new HashMap<String,String>();
    map.put("aaa","AAA");

    map.put("bbb","BBB");
    map.put("ccc","CCC");
    map.put("ddd","DDD");

    sortedMap.putAll(map);

    System.out.println(om.writeValueAsString(map));

    System.out.println(om.writeValueAsString(sortedMap));


}

and here is the result:`

结果如下:`

with a Map

有地图

{
  "aaa" : "AAA",
  "ddd" : "DDD",
  "ccc" : "CCC",
  "bbb" : "BBB"
}

with a SortedMap

使用 SortedMap

{
  "aaa" : "AAA",
  "bbb" : "BBB",
  "ccc" : "CCC",
  "ddd" : "DDD"
}

`

`

The 1st serialization with a Map will not be ordered, The second one with a TreeMap will be ordered alphabeticaly using keys. you can pass a Comparator to the treeMap for a different order.

第一个带有 Map 的序列化不会被排序,带有 TreeMap 的第二个序列将使用键按字母顺序排序。您可以将 Comparator 传递给 treeMap 以获得不同的顺序。

Edit: It also work on Hymanson with a LinkedHashMap() even if this is not a SortedMap. This is an implementation of Map that keep the order which keys were inserted into the map. This could be what your are looking for.

编辑:它也适用于带有 LinkedHashMap() 的 Hymanson,即使这不是 SortedMap。这是 Map 的一个实现,它保持将键插入到地图中的顺序。这可能是您正在寻找的。