Jackson ObjectMapper用方括号包装每个元素(即使是单个值)

时间:2016-02-14 09:48:31

标签: java json jackson guava jackson-modules

我有两个Map对象,我将它们合并到Guava的Multimap中。合并后我试图通过jackson ObjectMapper使用GuavaModule序列化最终地图(否则它不能正确序列化)。但是,问题是,即使只有一个值(objectmapper用方括号包装每个值),键的每个值都会作为列表元素打印出来。我得到的基本输出如下,

{
    "address": {
        "city": ["san francisco"]
    },
    "company": {
        "employees": [
            [{
                "name": "David",
                "lastname": "Foobar",
                "age": 22
            }, {
                "name": "Michael",
                "lastname": "FizBuzz",
                "age": 35
            }]
        ]
    },
    "config": {
        "key1": ["value1", "value3"],
        "key2": ["value2"],
        "key4": ["value4"]
    }
}

正如您所见,“地址” - > “城市” - > “旧金山”用方括号括起来,同时company -> employees被包裹两次另外,我已经尝试了SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true未来,但没有任何改变。

    Person person = new Person();
    person.setName("David");
    person.setLastname("Foobar");
    person.setAge(22);

    Person person2 = new Person();
    person2.setName("Michael");
    person2.setLastname("FizBuzz");
    person2.setAge(35);

    Map<String, Map<String, Object>> map1 = new ImmutableMap.Builder<String, Map<String, Object>>()
            .put("config", ImmutableMap.of(
                    "key1", "value1",
                    "key2", "value2"))
            .put("address", ImmutableMap.of("city", "san francisco"))
            .put("company", ImmutableMap.of("employees", Arrays.asList(person, person2)))
            .build();

    Map<String, Map<String, Object>> map2 = new ImmutableMap.Builder<String, Map<String, Object>>()
            .put("config", ImmutableMap.of(
                    "key1", "value3",
                    "key4", "value4"))
            .build();

    Map<String, Multimap<String, Object>> merged = new HashMap<>();
    BiFunction<Multimap<String, Object>,
                Multimap<String, Object>,
                Multimap<String, Object>> remappingFunction = (value1, value2) -> {
        Multimap<String, Object> multimap = HashMultimap.<String, Object>create();
        multimap.putAll(value1);
        multimap.putAll(value2);
        return multimap;
    };


    map1.forEach((key, value) -> merged.merge(key, Multimaps.forMap(value), remappingFunction));
    map2.forEach((key, value) -> merged.merge(key, Multimaps.forMap(value), remappingFunction));

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new GuavaModule());
    objectMapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
    String str = objectMapper.writeValueAsString(merged);
    System.out.print(str);
}

我感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:4)

根据Jackson documentationMultimapSerializer“包含JSON数组中的所有值集,无论值的数量是多少”。我相信这意味着它实际上忽略了WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED功能。

通过在序列化之前将所有内部Multimap<String, Object>实例转换为标准Map<String, Collection<Object>>实例,可以避免此问题。您可以使用Multimap.asMap()

执行此操作
Map<String, Map<String, Collection<Object>>> newMerged = new HashMap<>();
merged.forEach((key, value) -> newMerged.put(key, value.asMap()));
String newStr = objectMapper.writeValueAsString(newMerged);
System.out.println(newStr);

这将打印您期望的输出:

{"address":{"city":"san francisco"},"company":{"employees":[{"name":"David","lastname":"Foobar","age":22},{"name":"Michael","lastname":"FizBuzz","age":35}]},"config":{"key1":["value1","value3"],"key2":"value2","key4":"value4"}}