Prabhakar D Prabhakar D - 1 year ago 95
Java Question

Custom serialization of a POJO using Jackson

I have a pojo like below , When I serialize this Pojo I want the
Elements of the map to be appear as root elements of the json instead of nested element under testMap

public class ProxyConfig implements Serializable {

private String test;

private Integer intValue;

Map<String, String> testMap;

Example lets say testMap has two elements in it then the serialized ProxyConfig should look like this:

"test" : "testValue",
"intValue" : 20,
"testMapkey1" : "value1",
"testMapkey1" : "value1",

Not sure How to implement the CustomSerializer to achieve this any inputs would be helpful

Answer Source

You can use @JsonAnyGetter to get the key-values in a Map as plain properties when serialize the map in an instance:

public Map<String, String> getTestMap() {
    return testMap;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download