Arun Prakash
Arun Prakash

Reputation: 1727

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"
        }
    }
}

Upvotes: 5

Views: 4125

Answers (1)

Arun Prakash
Arun Prakash

Reputation: 1727

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

Upvotes: 6

Related Questions