I have a POJO with a property named paramMap as Map type.
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
@JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)
public class Pojo {
@JsonUnwrapped
private Map<String, Object> paramMap = new HashMap<String, Object>();
public Map<String, Object> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, Object> paramMap) {
this.paramMap = paramMap;
}
consider i have populated some values in the map, now i want to serialize this and unwrap the property name paramMap
.
Expected Output:
{
"Pojo": {
"name": "value1",
"age": 12,
"date": "12/02/2015"
}
}
Actual Output
{
"Pojo": {
"paramMap": {
"name": "value1",
"age": 12,
"date": "12/02/2015"
}
}
}
Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.
Serialization refers to the process of converting a data object (e.g., Python objects, Tensorflow models) into a format that allows us to store or transmit the data and then recreate the object when needed using the reverse process of deserialization.
To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.
Serialize and Deserialize Binary Tree. Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
The answer is here, Using the Jackson annotation @JsonAnyGetter
in the getter method of getParamMap()
we can get the expected out put.
@JsonAnyGetter
public Map<String, Object> getParamMap() {
return paramMap;
}
Note: This is is still open in Jackson project Issue #171 Thanks Tatu Saloranta author of the post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With