Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize a Map into an array of couple using Jackson in Spring MVC

First of all, I know what I am trying to do can be done using a custom JsonSerializer, but I'd like to know whether there is a less boilerplate code solution for this.

In Spring MVC, I'd like to serialize a Map into a list of couples. Let's say I'd like to return such a Map:

Map<String, String> res = new HashMap<>();
res.put("key1", "value1");
res.put("key2", "value2");

The default serialization result will give a JSON like this:

{key1: value1, key2: value2}

Is there a way to have instead something like this, without using a custom JsonSerializer?

[{key: "key1", value: "value1"}, {key: "key2", value: "value2"}]

I'm using Spring-Boot 1.3 with default versions of Spring MVC and Jackson.

like image 442
Rémi Doolaeghe Avatar asked Dec 03 '15 10:12

Rémi Doolaeghe


People also ask

Does Jackson use serializable?

Note that Jackson does not use java. io. Serializable for anything: there is no real value for adding that. It gets ignored.

What is serialization in Jackson?

We use the Jackson library to serialize an Object such as List, Map, Java object, etc. We can serialize an Object into JSON and put it into a file. In order to serialize an object, we use more than one class and method. We create the ObjectMapper class to use the writeValue() method.

How do you make a map Object serializable?

Now to serialize anything, you have to implement the java. io. Serializable interface and HashMap also implements the Serializable interface. Then after serializing the HashMap, we will learn how to deserialize the hashmap in Java.

Can we serialize map in Java?

Serialization converts a Java object into a stream of bytes, which can be persisted or shared as needed. Java Maps are collections that map a key Object to a value Object, and are often the least intuitive objects to serialize.


2 Answers

As I prefered a reusable solution, and could not find a standard solution, I implemented it with a custom JsonSerializer, as follows:

public class MapToCoupleArraySerializer extends JsonSerializer<Map<?, ?>>{

    @Override
    public void serialize(Map<?, ?> value, JsonGenerator generator,
            SerializerProvider serializers) throws IOException,
            JsonProcessingException {
        generator.writeStartArray();
        for (Entry<?, ?> entry : value.entrySet()){
            generator.writeStartObject();
            generator.writeObjectField("key", entry.getKey());
            generator.writeObjectField("value", entry.getValue());
            generator.writeEndObject();
        }       
        generator.writeEndArray();
    }    
}

and use it in the traditionnal Spring way:

public class MyClassToSerialize{

    @JsonSerialize(using = MapToCoupleArraySerializer .class)
    private Map<Key, Value> recipes;

    // ...
}
like image 57
Rémi Doolaeghe Avatar answered Nov 14 '22 23:11

Rémi Doolaeghe


try to serialize entries instead of the map itself :

Map.Entry[] entries = myMap.entrySet().toArray(new Map.Entry[]{});

I didn't try it, but the result should be similar enough to what you want.

like image 44
francesco foresti Avatar answered Nov 14 '22 23:11

francesco foresti