TagUtils { @Nullable static Map<String, Set<String>> convertToTagsMap(@Nullable JsonValue jsonValue) { if (jsonValue == null || jsonValue.isNull()) { return null; } Map<String, Set<String>> tagGroups = new HashMap<>(); if (jsonValue.isJsonMap()) { for (Map.Entry<String, JsonValue> groupEntry : jsonValue.optMap()) { Set<String> tags = new HashSet<>(); for (JsonValue tag : groupEntry.getValue().optList()) { if (tag.isString()) { tags.add(tag.getString()); } } tagGroups.put(groupEntry.getKey(), tags); } } if (tagGroups.isEmpty()) { return null; } return tagGroups; }    }
@Test public void testConvertToTagsMap() throws JsonException { Map<String, Set<String>> tagGroups = new HashMap<>(); Set<String> tags = new HashSet<>(); tags.add("tag1"); tags.add("tag2"); tags.add("tag3"); tagGroups.put("tagGroup", tags); JsonValue jsonValue = JsonValue.wrap(tagGroups); Map<String, Set<String>> map = TagUtils.convertToTagsMap(jsonValue); assertEquals("Map size mismatch", map.size(), tagGroups.size()); assertTrue("Value mismatch", map.containsValue(tags)); }
@Test public void testConvertNullToTagsMap() throws JsonException { assertNull(TagUtils.convertToTagsMap(null)); JsonValue jsonString = JsonValue.parseString("non-JsonMap"); assertNull(TagUtils.convertToTagsMap(jsonString)); }