Wilfred Clement
Wilfred Clement

Reputation: 2774

Serializing POJO to JSON - Input parameterization

I have 4 POJO's ( links )

and I have a class file that is going to map values based on these POJO's ( Link )

When i run the code

public static void main(String[] args) throws Exception {
    Input user = new Input();
    user.setId("0001");
    user.setType("donut");
    user.setName("cake");
    user.setPpu(0.55);

the below is my output

{
  "id" : "0001",
  "type" : "donut",
  "name" : "cake",
  "ppu" : 0.55,
  "batters" : null,
  "topping" : [ ]
}

However, I'd like my output to be

{ "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": 
{ "batter": [{ "id": "1001", "type": "Regular" }, 
{ "id": "1004", "type": "Devil's Food" } ] }, "topping": [{ "id": "5001", 
"type": "None" }, { "id": "5004", "type": "Maple" } ] }

I am not sure how i can improvise the code to print the output like how i expect, Thanks

Expected : Batters, Toppings values also

Upvotes: 1

Views: 78

Answers (2)

Slava Vedenin
Slava Vedenin

Reputation: 60104

You should use something like this:

public static void main(String[] args) throws Exception {
    Input user = new Input();
    user.setId("0001");
    user.setType("donut");
    user.setName("Cake");
    user.setPpu(0.55);

    // Add Regular batter
    Batter batter1 = new Batter();
    batter1.setId("0001");
    batter1.setType("Regular");

    // Add Devil's Food batter
    Batter batter2 = new Batter();
    batter2.setId("1004");
    batter2.setType("Devil's Food");

    Batters batters = new Batters();
    batters.setBatter(Arrays.asList(batter1, batter2));
    user.setBatters(batters); 

    // Add toppings
    Topping topping1 = new Topping();
    topping1.setId("5001");
    topping1.setId("None");

    Topping topping2 = new Topping();
    topping2.setId("5004");
    topping2.setId("Maple");

    users.setTopping(Arrays.asList(topping1, topping2));

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    String json = mapper.writeValueAsString(user);
    System.out.println(json); // expected string

Upvotes: 1

Konzern
Konzern

Reputation: 128

public static void main(String[] args) throws JsonProcessingException {
    Javabelazy user = new Javabelazy();
    user.setId("0001");
    user.setType("donut");
    user.setName("Cake");
    user.setPpu("0.55");
    Batter batter1 = new Batter();
    batter1.setId("0001");
    batter1.setType("Regular");
    Batter batter2 = new Batter();
    batter2.setId("1004");
    batter2.setType("Devil's Food");
    Batters batters = new Batters();
    Batter[] batter = new Batter[2];
    batter[1] = batter1;
    batter[0] = batter2;
    batters.setBatter(batter );
    user.setBatters(batters); 
    Topping topping1 = new Topping();
    topping1.setId("5001");
    topping1.setId("None");
    Topping topping2 = new Topping();
    topping2.setId("5004");
    topping2.setId("Maple");
    Topping[] topping = new Topping[2];
    topping[0] = topping1;
    topping[1] = topping2;
    user.setTopping(topping );
    ObjectMapper objectmapper = new ObjectMapper();
    String json = objectmapper.writeValueAsString(user);
    System.out.println(json);
}

Upvotes: 0

Related Questions