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?