Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization of key/value pairs in Jackson?

Tags:

java

rest

jackson

I have a class

class Foo {
  String key;
  String value;
}

and want to serialize this into "<content of key>":"<content of value>" How can I achieve this (and how to deserialize "myKey":"myVal" into a Fooobject?

I was trying to use

@JsonValue
public String toString() {
   return "\"" + key + "\":\"" + value + "\"";
}

But clearly end up with too many quotes.

@JsonValue
public String toString() {
    return key + ":" + value;
}

also does not work, as it does not create enough quotes.

like image 854
Heiko Rupp Avatar asked Mar 25 '11 09:03

Heiko Rupp


People also ask

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.

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.

Which methods does Jackson rely on to serialize a Java object *?

Overview. Jackson is a simple java based library to serialize java objects to JSON and vice versa.

How does Jackson serialize date?

Here, Jackson serializes birthDate field as "2008-02-03". This is because we've set the pattern attribute as "yyyy-MM-dd".


1 Answers

I found one way, which is using a JsonSerializer like this:

public class PropertyValueSerializer extends JsonSerializer<Foo> {

    @Override
    public void serialize(Foo property_value, JsonGenerator jsonGenerator,
                          SerializerProvider serializerProvider) throws IOException, JsonProcessingException {

        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName(property_value.getKey());
        jsonGenerator.writeString(property_value.getValue());
        jsonGenerator.writeEndObject();
    }

The Foo class needs to know about this:

@JsonSerialize(using = PropertyValueSerializer.class)
public class Foo {

Deserializing is very similar:

public class PropertyValueDeserializer extends JsonDeserializer<PROPERTY_VALUE> {    

    @Override
    public Foo deserialize(JsonParser jsonParser,
                                      DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

        String tmp = jsonParser.getText(); // {
        jsonParser.nextToken();
        String key = jsonParser.getText();
        jsonParser.nextToken();
        String value = jsonParser.getText();
        jsonParser.nextToken();
        tmp = jsonParser.getText(); // }

        Foo pv = new Foo(key,value);
        return pv;
    }

And this also needs to be annotated on the Foo class:

@JsonSerialize(using = PropertyValueSerializer.class)
@JsonDeserialize(using = PropertyValueDeserializer.class)
public class Foo implements Serializable{
like image 152
Heiko Rupp Avatar answered Sep 27 '22 20:09

Heiko Rupp