hiaclibe
hiaclibe

Reputation: 694

Json and Guava Multimap

I would like to create a multimap, convert it to JSON and back again. The problem here is, that single values still shown as collection/array.

Here is what I have

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new GuavaModule());    

Multimap<String, String> map = ArrayListMultimap.create();    
map.put("Cheesecake", "mummy");
map.put("Cookie", "PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43");
map.put("Cookie", "yummy_cookie=choco; tasty_cookie=strawberry");

System.out.println(map);
System.out.println("---");

String json = objectMapper.writeValueAsString(map);
System.out.println("JSON:");
System.out.println(json);  
System.out.println("---");   

JsonNode node = objectMapper.readTree(json);
Multimap<String, String> multimap = objectMapper.readValue(
objectMapper.treeAsTokens(node),
objectMapper.getTypeFactory().constructMapLikeType(
Multimap.class, String.class, String.class));
System.out.println(multimap);

Output:

{Cookie=[PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43, yummy_cookie=choco; tasty_cookie=strawberry], Cheesecake=[mummy]}
---
JSON:
{"Cookie":["PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43","yummy_cookie=choco; tasty_cookie=strawberry"],"Cheesecake":["mummy"]}
---
{Cookie=[PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43, yummy_cookie=choco; tasty_cookie=strawberry], Cheesecake=[mummy]}

I would like to have sth like:

JSON:
{"Cookie":["PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43","yummy_cookie=choco; tasty_cookie=strawberry"],"Cheesecake":"mummy"}
//or even better 
JSON:
{"Cookie":"PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43","Cookie":"yummy_cookie=choco; tasty_cookie=strawberry","Cheesecake":"mummy"}

So "mummy" is a string value instead inside json array.


Any idea how to archive this?

Upvotes: 0

Views: 1575

Answers (1)

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 28045

You could use Multimpap#asMap() view and serialize it with SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED to achieve the first out of your desired formats. Note that you'd need DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY to deserialize map-like object with this:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new GuavaModule());
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
objectMapper.enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);

Multimap<String, String> map = ArrayListMultimap.create();
map.put("Cheesecake", "mummy");
map.put("Cookie", "PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43");
map.put("Cookie", "yummy_cookie=choco; tasty_cookie=strawberry");

System.out.println(map);
System.out.println("---");

String json = objectMapper.writeValueAsString(map.asMap());
System.out.println("JSON:");
System.out.println(json);
System.out.println("---");

JsonNode node = objectMapper.readTree(json);
Multimap<String, String> multimap = objectMapper.readValue(
        objectMapper.treeAsTokens(node),
        objectMapper.getTypeFactory().constructMapLikeType(
                Multimap.class, String.class, String.class));
System.out.println(multimap);

Output:

{Cookie=[PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43, yummy_cookie=choco; tasty_cookie=strawberry], Cheesecake=[mummy]}
---
JSON:
{"Cookie":["PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43","yummy_cookie=choco; tasty_cookie=strawberry"],"Cheesecake":"mummy"}
---
{Cookie=[PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43, yummy_cookie=choco; tasty_cookie=strawberry], Cheesecake=[mummy]}

If you want to serialize and deserialize second desired format, you need to write custom Jackson serializer / deserializer.

Upvotes: 2

Related Questions