Suren Raju
Suren Raju

Reputation: 3060

How to unwrap and serialize java map in java using jackson?

I have a bean like this

class Foo {
    private Map<String, Data> dataMap;
    private String fooFieldOne;
    private String fooFieldTwo;
}

class Data {
    private fieldOne;
    private fieldTwo;
}

I want to serialize as Json as like this

{
    "key1": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key2": {
        "fieldOne": "some other value",
        "fieldTwo": "some other value"
    },
    "fooFieldOne":"valueone", 
    "fooFieldTwo":"valuetwo" 
}

But i am getting result like

{
    "dataMap": {
        "key1": {
            "fieldOne": "some value",
            "fieldTwo": "some value"
        },
        "key2": {
            "fieldOne": "some other value",
            "fieldTwo": "some other value"
        }
    },
    "fooFieldOne":"valueone", 
    "fooFieldTwo":"valuetwo" 
}

How to ignore dataMap layer in the above json? I'm using java jackson library for this.

Code i tried is

ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(myFOOObject)

Upvotes: 17

Views: 29719

Answers (4)

T.Loner
T.Loner

Reputation: 59

I found answer - using @JsonAnyGetter annotation with getter for map. To avoid conflicts during deserialization should be used @JsonAnySetter annotation with setter for this map.

Upvotes: 5

Bart
Bart

Reputation: 17361

You could create a getter for dataMap and serialize the dataMap instead of the entire Foo instance.

mapper.writeValueAsString(myFOOObject.getDataMap());

Another method is using the @JsonUnwrapped annotation. This annotation is available in Jackson 1.9+.

http://jackson.codehaus.org/1.9.9/javadoc/org/codehaus/jackson/annotate/JsonUnwrapped.html

The downside of using this annotation is the inability to use maps as stated in the answer to your other question

Upvotes: 11

sanbhat
sanbhat

Reputation: 17622

You are getting

{ "dataMap": ... } 

because Foo has a field called dataMap within it. Here the name of the field cannot be ignored because it will create conflicts if Foo had multiple maps, something like

class Foo {
private Map<String, Data> dataMap1;
private Map<String, Data> dataMap2;
}

now without having the fieldName : in JSON, the JSON doesn't know how to deserialize when it gets some JSON like

{
    //belongs to dataMap1
    "key1": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key2": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    //belongs to dataMap2
    "key3": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key4": {
        "fieldOne": "some other value",
        "fieldTwo": "some other value"
    }
}

Now having the fieldName makes it very clear while deserializing

{
    "dataMap1" : {
    "key1": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key2": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    }
    },
    "dataMap2" : {
    "key3": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key4": {
        "fieldOne": "some other value",
        "fieldTwo": "some other value"
    }
  }
}

Having said that

How to ignore dataMap layer in the json?

You can just serialize the map

ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(myFOOObject.getDataMap());

Upvotes: 2

Harmeet Singh Taara
Harmeet Singh Taara

Reputation: 6611

In map there is two Objects String and Data . JSON convert the java objects into JSON key value pair , but if you already have a Map that contain key value pair, and convert that map into JSON document , then map key is also convert into JSON key value pair and Map value also have a object that convert into key value pair.

Upvotes: 0

Related Questions