How to deserialize a Map<String, Object> into POJO?

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 + "]";
    }
}

Answers


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.


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

mapper.convertValue(map, MyPojo.class);

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);

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

Need Your Help

Django Admin filter on Foreign Key property

python django filter admin

I want to add a filter in an admin changelist by a property of a foreign key, e.g.

Can't use chrome driver for Selenium

python google-chrome python-3.x selenium python-3.3

I'm having trouble using the Chrome driver for Selenium. I have the chromedriver downloaded and saved to C:\Chrome: