I can easily convert (deserialize) Json string-> Java object
in Jackson, but I have a requirement to get some properties as JSON string.
The purpose is to have a magical property (let's call it payload here) that can hold anything, and want to restore it with the data type (FQCN) defined in another property.
Jackson has a @JsonValue
that treats JSON strings as they are, but this is only valid for serialization. Unfortunately, this is not the case for deserialization.
The workaround is to implement your own JsonDeserializer
and specify the deserialization process individually with @JsonDeserialize
. However, since the purpose is "I want to get the JSON string as it is", the implementation of JsonDeserializer
is very easy.
JsonRawValueDeserializer.java
package com.example.jacksondemo;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
//★ Point 1
public class JsonRawValueDeserializer extends JsonDeserializer<String> {
//★ Point 2
@Override
public String deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
return parser.readValueAsTree().toString();
}
}
MyModel.java
package com.example.jacksondemo;
import java.io.Serializable;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
public class MyModel implements Serializable {
private static final long serialVersionUID = 1L;
//★ Point 3
// I want json raw text!
@JsonDeserialize(using = JsonRawValueDeserializer.class)
private String payload;
// these property are auto mapped by jackson (without special operation)
private String fqcn;
private Integer priority;
private String messageId;
// constructor, setter, getter omitted
}
** ★ Point 1 **
Inherit the com.fasterxml.jackson.databind.JsonDeserializer
class and define your ownJsonDeserializer
.
For the type parameter, specify the type of the deserialization process result, that is, String
this time.
** ★ Point 2 **
Implement to override the deserialize
method and return a JSON string.
** ★ Point 3 **
In the Java class that maps JSON, add the @JsonDeserialize
annotation to the property whose value you want to map as it is a JSON string.
ʻUseattribute ★ Specify the original
JsonDeserializer` implemented in point 1.
Now, even if the JSON payload
property is an object, the JSON string will be stored as is in the Stringpayload
property.
To achieve this goal, look at the dqcn
property to identify the data type, and then deserialize the String value of the payload
property with the desired data type.
This time, I explained how to get some properties as JSON strings by deserializing Jackson.
Jackson has a function called JsonNode
that reads Json as tree-structured data, but I think that there are many ways to map Json and Java. At that time, if you find that only a specific property remains Json, please try this usage.
Recommended Posts