How to convert a JSON string to a Map<String, Set<String>> with Jackson JSON How to convert a JSON string to a Map<String, Set<String>> with Jackson JSON json json

How to convert a JSON string to a Map<String, Set<String>> with Jackson JSON


Try this:

JavaType setType = mapper.getTypeFactory().constructCollectionType(Set.class, CustomClass.class);JavaType stringType = mapper.getTypeFactory().constructType(String.class);JavaType mapType = mapper.getTypeFactory().constructMapType(Map.class, stringType, setType);String outputJson = mapper.readValue(json, mapType)


Unfortunately Class really can not express generic types; so if your value type is generic (like Set<String>), you need to pass JavaType instead. And that can be used to construct structured JavaType instances as well.