Ramanuj
Ramanuj

Reputation: 123

How can I ignore the JSON root element while deserialising Pojo

==== Root Object ====

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class RoomInventoryResponse {
    private Map<String, InventoryDetail> inventoryDetail = new HashMap<>();

    public Map<String, InventoryDetail> getInventoryDetail() {
        return inventoryDetail;
    }
    public void setInventoryDetail(Map<String, InventoryDetail> inventoryDetail) {
        this.inventoryDetail = inventoryDetail;
    }
   // To String overridden
}

==== InventoryDetail obj ====

import java.util.Objects;
public class InventoryDetail {
    private Integer booked;
    private Integer available;
    public Integer getBooked() {
        return booked;
    }
    public void setBooked(Integer booked) {
        this.booked = booked;
    }
    public Integer getAvailable() {
        return available;
    }
    public void setAvailable(Integer available) {
        this.available = available;
    }
   // To String overridden
}

====== Run mentod ====

public class Test{
    public static void main(String[] args) throws JsonProcessingException
     ObjectWriter ow = new  ObjectMapper().writer().withDefaultPrettyPrinter();
            String json = ow.writeValueAsString(request);
            RoomInventoryResponse response = new RoomInventoryResponse();

            Map<String, InventoryDetail> map = new HashMap<>();
            InventoryDetail inventoryDetail = new InventoryDetail();
            inventoryDetail.setAvailable(10);
            inventoryDetail.setBooked(10);
            map.put("2019-02-21", inventoryDetail);

            InventoryDetail inventoryDetail2 = new InventoryDetail();
            inventoryDetail2.setAvailable(15);
            inventoryDetail2.setBooked(10);
            map.put("2019-02-22", inventoryDetail2);

            InventoryDetail inventoryDetail3 = new InventoryDetail();
            inventoryDetail3.setAvailable(15);
            inventoryDetail3.setBooked(10);
            map.put("2019-02-23", inventoryDetail2);
            response.setInventoryDetail(map);
            String json2 = ow.writeValueAsString(response);

       }
    }

Current output :

Json Format: 
{
  "inventoryDetail" : {
    "2019-02-23" : {
      "booked" : 10,
      "available" : 15
    },
    "2019-02-22" : {
      "booked" : 10,
      "available" : 15
    },
    "2019-02-21" : {
      "booked" : 10,
      "available" : 10
    }
  }
}

Expected Result : (I wan't to remove inventoryDetail from response as below output result)

{
    "2019-09-30": {
        "booked": 0,
        "available": 100
    },
    "2019-09-20": {
        "booked": 0,
        "available": 16
    },
    "2019-09-12": {
        "booked": 0,
        "available": 10
    }
}

Another requirement is how can change the root element tags in caps or small as inventoryDetail make like InventoryDetail in response I should starts with as caps or small

Upvotes: 1

Views: 1118

Answers (1)

Kishan Maurya
Kishan Maurya

Reputation: 3394

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class RoomInventoryResponse implements JsonSerializable {


    private Map<String, InventoryDetail> inventoryDetail = new HashMap<>();

    public Map<String, InventoryDetail> getInventoryDetail() {
        return inventoryDetail;
    }

    public void setInventoryDetail(Map<String, InventoryDetail> inventoryDetail) {
        this.inventoryDetail = inventoryDetail;
    }

    @Override
    public void serialize(JsonGenerator arg0, SerializerProvider arg1) throws IOException {
        arg0.writeObject(inventoryDetail);
    }

    @Override
    public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {

    }
}

For your last question:

@JsonTypeName("InventoryDetail") // any name
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT ,use = JsonTypeInfo.Id.NAME) 
    `

Upvotes: 6

Related Questions