Spring - Java HashMap for layered Pojo

I have a hashmap Map<String, String>

that has the following meaning, note that the dot indicates a hierarchy:

+--------------------+-----------+
|    Key             |  Value    |
+--------------------+-----------+
| car.color          | blue      |
| car.engine.make    | mitsubishi|
| car.engine.power   | 120       |
+--------------------+-----------+

      

I have pojo classes:

public class Vehicle {

    private Car car;
   **Setters and Getters Below*  
}    

public class Car {
    private String color;

    private Engine engine;
    **Setters and Getters Below*      
}

public class Engine {
    private String make;

    private Integer power;
    **Setters and Getters Below**
}

      

Is there a way to map a HashMap in a POJO class based on a hierarchy? I tried using jackson ObjectMapper mapper = new ObjectMapper();

but it seems it was able to display 1 level of the object.

+3


source to share


4 answers


You can use @JsonCreator

annotation
in your class constructor Vehicle

:

@JsonCreator
public Vehicle(Map<String, String> map) {
    String color = map.get("car.color");
    String make = map.get("car.engine.make");
    Integer power = Integer.valueOf(map.get("car.engine.power"));
    Engine engine = new Engine();
    engine.setMake(make);
    engine.setPower(power);
    Car car = new Car();
    car.setColor(color);
    car.setEngine(engine);
    this.car = car;
}

      



Using:

Map<String, String> map = new HashMap<>();
map.put("car.color", "blue");
map.put("car.engine.make", "mitsubishi");
map.put("car.engine.power", "120");

ObjectMapper mapper = new ObjectMapper();

Vehicle vehicle = mapper.convertValue(map, Vehicle.class);

      

+1


source


If you want to map a hashmap in a POJO with jackson

, you need to refactor the structure:

{
  "car": {
    "color": "blue",
    "engine": {
      "make": "mitsubishi",
      "power": 20
    }
  }
}

      



Or you can analyze the hashmap and build each one class

by hand.

+3


source


One way to use your classes:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class CarMap {

    Map<String, String> maap = new HashMap<String, String>();

    public CarMap() {
        Car c = new Car("blue", new Engine("mitsubishi",120));

        maap.put("color", c.color);
        maap.put("make", c.engine.make);
        maap.put("power",Integer.toString(c.engine.power));

        Set<String> keys = maap.keySet();
        for (String el : keys) {
            System.out.println(el+"\t"+maap.get(el).toString());
        }

        JSONObject obj = new JSONObject();

        JSONObject obj1 = new JSONObject();
        obj1.put("color",maap.get("color").toString());

        JSONArray engList = new JSONArray();
        JSONObject obj2 = new JSONObject();
        obj2.put("make",maap.get("make").toString());
        engList.add(obj2);

        JSONObject obj3 = new JSONObject();
        obj3.put("power",maap.get("power").toString());
        engList.add(obj3);

        obj.put("car", obj1);
        obj.put("engine", engList);
        System.out.print(obj);
    }

    public static void main(String[] args) {
        new CarMap();
    }


    class Car {

        private String color;
        private Engine engine;

        Car(String c, Engine e) {
            this.color = c;
            this.engine = e;
        }
    }

    class Engine {

        private String make;
        private Integer power;

        Engine(String m, Integer p) {
            this.make = m;
            this.power = p;
        }
    }
}

      

OUTPUT:

color   blue
power   120
make    mitsubishi
{"car":{"color":"blue"},"engine":[{"make":"mitsubishi"}, "power":"120"}]}

      

+1


source


The easiest way to just do it and avoid it while keeping it simple is to map them manually as follows. You might want to consider null checking optional values ​​as you go along.

Map<String, String> map = *population here*

Engine engine = new Engine()
engine.setMake(map.get("car.engine.make"))
engine.setPower(map.get("car.engine.power"))

Car car = new Car();
car.setColor(map.get("car.color");
car.setEngine(engine);

Vehicle vehicle = new Vehicle();
vehicle.setCar(car);

      

+1


source







All Articles