Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing array with Jackson

I'm serializing the following model:

class Foo {

    private List<String> fooElements;
}

If fooElements contains the strings 'one', 'two' and 'three. The JSON contains one string:

{
    "fooElements":[
         "one, two, three"
     ]
}

How can I get it to look like this:

{
    "fooElements":[
         "one", "two", "three"
     ]
}
like image 289
user86834 Avatar asked Sep 04 '13 20:09

user86834


People also ask

Does Jackson preserve array order?

Jackson mapper fill the ArrayList maintaining the order of JSON. If you want a different order you can use the annotation @JsonPropertyOrder.

Does Jackson use Java serialization?

Note that Jackson does not use java.

What is Jackson object serialization?

Jackson is a solid and mature JSON serialization/deserialization library for Java. The ObjectMapper API provides a straightforward way to parse and generate JSON response objects with a lot of flexibility.

What is Jackson deserializer?

Jackson is a powerful and efficient Java library that handles the serialization and deserialization of Java objects and their JSON representations. It's one of the most widely used libraries for this task, and runs under the hood of many other frameworks.


2 Answers

I got it working by adding a custom serializer:

class Foo {
    @JsonSerialize(using = MySerializer.class)
    private List<String> fooElements;
}

public class MySerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        List<String> fooList = (List<String>) value;

        if (fooList.isEmpty()) {
            return;
        }

        String fooValue = fooList.get(0);
        String[] fooElements = fooValue.split(",");

        jgen.writeStartArray();
        for (String fooValue : fooElements) {
            jgen.writeString(fooValue);
        }
        jgen.writeEndArray();
    }
}
like image 145
user86834 Avatar answered Oct 04 '22 22:10

user86834


If you are using Jackson, then the following simple example worked for me.

Define the Foo class:

public class Foo {
    private List<String> fooElements = Arrays.asList("one", "two", "three");

    public Foo() {
    }

    public List<String> getFooElements() {
        return fooElements;
    }
}

Then using a standalone Java app:

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonExample {

    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {

        Foo foo = new Foo();

        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(foo));

    }

}

Output:

{"fooElements":["one","two","three"]}

like image 24
nickebbitt Avatar answered Oct 04 '22 23:10

nickebbitt