Jackson JSON - Deserialize Commons MultiMap

I want to serialize and deserialize MultiMap (Apache Commons 4) using JSON.

Part of the code to test:

MultiMap<String, String> map = new MultiValueMap<>(); map.put("Key 1", "Val 11"); map.put("Key 1", "Val 12"); map.put("Key 2", "Val 21"); map.put("Key 2", "Val 22"); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(map); MultiMap<String, String> deserializedMap = mapper.readValue(jsonString, MultiValueMap.class); 

Serialization works fine and gives the format I would expect:

 {"Key 1":["Val 11","Val 12"],"Key 2":["Val 21","Val 22"]} 

Unfortunately, deserialization produces a result that does not look like this: After deserialization, the Multimap contains an ArrayList inside an ArrayList for key values, not one ArrayList for a key containing values.

This result is created because the put() method for the multiple card is called to add the array found in the json string, since MultiMap implements the map interface.

The implementation of MultiMap itself creates an ArrayList if the new value is placed in a non-existing key.

Is there any way around this?

Thank you for your help!

+6
source share
1 answer

Confident from the Oxford vocabulary, which bypasses the means to β€œfind a way around (an obstacle)”, a simple job has been done here.

First, I created a method that generates the same MultiValueMap as above. And I use the same approach to parse it as a json string.

Then I created the following deserialization method

 public static MultiMap<String,String> doDeserialization(String serializedString) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); Class<MultiValueMap> classz = MultiValueMap.class; MultiMap map = mapper.readValue(serializedString, classz); return (MultiMap<String, String>) map; } 

Of course, this in itself refers to the exact problem that you talked about above, so I created the doDeserializationAndFormat method: it will doDeserializationAndFormat over each "list inside the list" corresponding to a given key, and bind the values ​​to the key one by one

 public static MultiMap<String, String> doDeserializationAndFormat(String serializedString) throws JsonParseException, JsonMappingException, IOException { MultiMap<String, String> source = doDeserialization(serializedString); MultiMap<String, String> result = new MultiValueMap<String,String>(); for (String key: source.keySet()) { List allValues = (List)source.get(key); Iterator iter = allValues.iterator(); while (iter.hasNext()) { List<String> datas = (List<String>)iter.next(); for (String s: datas) { result.put(key, s); } } } return result; } 

Here is a simple call in the main method:

 MultiValueMap<String,String> userParsedMap = (MultiValueMap)doDeserializationAndFormat(stackMapSerialized); System.out.println("Key 1 = " + userParsedMap.get("Key 1") ); System.out.println("Key 2 = " + userParsedMap.get("Key 2") ); 

json to multivaluemap

Hope this helps.

0
source

Source: https://habr.com/ru/post/985057/


All Articles