Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing & Unwrap the Map object in POJO using Jackson annotation

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"
        }
    }
}
like image 810
Arun Prakash Avatar asked Jul 01 '15 11:07

Arun Prakash


People also ask

What does serialization mean?

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.

What is serializing in Python?

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.

What is serializing of objects?

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.

What is serializing a binary tree?

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.


1 Answers

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

like image 165
Arun Prakash Avatar answered Sep 28 '22 05:09

Arun Prakash