28

I have a Map<String, Object> which contains a deserialized form of JSON. I would like to deserialize this into the fields of a POJO.

I can perform this using Gson by serializing the Map into a JSON string and then deserializing the JSON string into the POJO, but this is inefficient (see example below). How can I perform this without the middle step?

The solution should preferably use either Gson or Jackson, as they're already in use by the project.

Example code:

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;

public class Test {
    public static void main(String[] args) {
        Map<String, Object> innermap = new HashMap<String, Object>();
        innermap.put("number", 234);
        innermap.put("string", "bar");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("number", 123);
        map.put("string", "foo");
        map.put("pojo2", innermap);

        Gson gson = new Gson();

        // How to perform this without JSON serialization?
        String json = gson.toJson(map);
        MyPojo pojo = gson.fromJson(json, MyPojo.class);

        System.out.println(pojo);
    }
}


class MyPojo {
    private int number;
    private String string;
    private MyPojo pojo2;

    @Override
    public String toString() {
        return "MyPojo[number=" + number + ", string=" + string + ", pojo2=" + pojo2 + "]";
    }
}
Sampo
  • 4,308
  • 6
  • 35
  • 51

4 Answers4

37

Using Gson, you can turn your Map into a JsonElement, which you can then parse in exactly the same way using fromJson:

JsonElement jsonElement = gson.toJsonTree(map);
MyPojo pojo = gson.fromJson(jsonElement, MyPojo.class);

This is similar to the toJson method that you are already using, except that it avoids writing the JSON String representation and then parsing that JSON String back into a JsonElement before converting it to your class.

andersschuller
  • 13,509
  • 2
  • 42
  • 33
  • Just what I was looking for. The Jackson answer by Michal works as well, but we're using Gson more, so it fits our needs better. – Sampo Aug 24 '14 at 15:58
26

In Jackson you can use convertValue method. See below example:

mapper.convertValue(map, MyPojo.class);
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
10

You can use jackson library for convert map object to direct POJO.

Map<String, String> map = new HashMap<>();
map.put("id", "5");
map.put("name", "Bob");
map.put("age", "23");
map.put("savings", "2500.39");

ObjectMapper mapper = new ObjectMapper();
MyPojo pojo = mapper.convertValue(map, MyPojo.class);
beaver
  • 523
  • 1
  • 9
  • 20
-1

You can directly construct MyPojo by giving it the map. Something like

MyPojo pojo = new MyPojo(map);

And declaring an according constructor :

public MyPojo(Map<String, Object> map){
  this.number=map.get("number");
  this.string=map.get("string");
  this.pojo2=new MyPojo(); // I don't know if this is possible
  this.pojo2.string=map.get("pojo2").get("string");
  this.pojo2.number=map.get("pojo2").get("number");
}
  • 3
    I want to avoid explicit mapping, since I have many different classes I want to deserialize from. – Sampo Aug 22 '14 at 13:58