Search code examples
javaperformancejacksondata-processingjackson-databind

How to convert comma separated string in map to Set in object using Jackson in Java


I'm running the next program:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test {

    private static class Shape {
        private String name;
        private Set<String> colors;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Set<String> getColors() {
            return colors;
        }

        public void setColors(Set<String> colors) {
            this.colors = colors;
        }
    }

    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("name", "table");
        attributes.put("colors", "blue,green,red,black");
        Shape shape = objectMapper.convertValue(attributes, Shape.class);
    }
}

Here the dependencies in the pom.xml:

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9.3</version>
        </dependency>
    </dependencies>

I got the next error:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot deserialize instance of `java.util.HashSet` out of VALUE_STRING token
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.company.test.Test$Shape["colors"])
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3751)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3669)
    at com.company.test.Test.main(Test.java:36)

I have tried changing to:

    attributes.put("colors", "[blue,green,red,black]");
    AND
    attributes.put("colors", "[\"blue\",\"green\",\"red\",\"black\"]");

But it does not work. A workaround can be the next:

    ...
    Set<String> colors = new HashSet<>();
    colors.add("blue");
    colors.add("green");
    colors.add("red");
    colors.add("black");
    attributes.put("colors", colors);
    ...

However, that solution is not allowed for the current implementation. Do you imagine how to implemented using a different approach?


Solution

  • You can use CsvMapper from jackson-dataformats-text library. Also, you need to deserialise first String into Set<String>, build a Map and convert it to Shape at the end:

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.dataformat.csv.CsvMapper;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    public class CsvApp {
        public static void main(String[] args) throws JsonProcessingException {
            CsvMapper mapper = new CsvMapper();
    
            String array = "blue,green,red,black";
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("name", "table");
            attributes.put("colors", mapper.readValue(array, new TypeReference<Set<String>>() {}));
            Shape shape = mapper.convertValue(attributes, Shape.class);
    
            System.out.println(shape);
        }
    }