mu_sa
mu_sa

Reputation: 2725

Can not deserialize instance of out of START_OBJECT token

My Json looks something like (and its unmodifiable)

{
    ....
    "Sale": [
        "SaleLines": {
                    "SaleLine": [
                        {
                            "Item": {
                                "Prices": {
                                    "ItemPrice": [
                                        {
                                            "amount": "100",
                                            "useType": "Default"
                                        },
                                        {
                                            "amount": "100",
                                            "useType": "MSRP"
                                        }
                                    ]
                                },
                            }
                                ......
                                ......
                        } 
                ] 
            "calcDiscount": "0",
            "calcSubtotal": "500",
        }
    ]
}

The java POJO code looks like

public static class SaleLines {

    @JsonProperty("SaleLine")
    private SaleLineObject[] saleLineObject;

    public SaleLineObject[] getSaleLineObject() { return saleLineObject; }

    public void setSaleLineObject(SaleLineObject[] saleLineObject) { this.saleLineObject = saleLineObject; }
}

public static class SaleLineObject {
    private SaleLine saleLine;

    public SaleLine getSaleLine() {
        return saleLine;
    }

    public void setSaleLine(SaleLine saleLine) {
        this.saleLine = saleLine;
    }

}

public static class SaleLine {
    @JsonProperty("itemID")
    private String itemId;                  //line_item_nk
    @JsonProperty("unitPrice")
    private String unitPrice;
    ....
}

@JsonPropertyOrder({"total", "calcSubTotal", "calcDiscount"})
public static class Sale {

    private String saleTotal, calcSubtotal, calcDiscount; 
    private int salesValueWOVat;

    @JsonProperty("SaleLines")
    SaleLines saleLine;

    @JsonCreator
    public Sale (@JsonProperty("total")String saleTotal,
            @JsonProperty("calcSubtotal")String calcSubtotal,
            @JsonProperty("calcDiscount")String calcDiscount,
            @JsonProperty("SaleLines")SaleLines saleLine,
    ) {
        this.saleTotal = saleTotal;
        this.calcSubtotal = calcSubtotal;
        this.calcDiscount = calcDiscount;
        this.saleLine = saleLine;
        setSalesValueWOVat();
    }

    // getter and setters 

}

@SuppressWarnings({ "rawtypes" })
public static <E, T extends Collection> T readFromJsonAndFillType (
        String json, 
        Modules module,
        Class <T> collectionType,
        Class <E> elementType) 
        throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper objMapper = new ObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    TypeFactory tf = objMapper.getTypeFactory();
    JsonNode node = objMapper.readTree(json).get(module.jsonFieldName); 
    return objMapper.readValue(node.toString(),
            tf.constructCollectionType(collectionType, elementType));

}

In main

ArrayList<Sale> saleList = readFromJsonAndFillType(
                saleJSON, 
                Modules.SALE, 
                ArrayList.class,
                Sale.class);

for (Sale sale: saleList) {
    System.out.println(sale.toString());
}

I know this question has been asked multiple times and even I took help from for eg Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

But still I cannot get through this error

Upvotes: 0

Views: 35796

Answers (1)

K_Bist
K_Bist

Reputation: 91

I know this question has been asked multiple times & everyone getting resolved there problems with different ways. Whenever you find "Can not deserialized instance of out of START_OBJECT token". it's generally occur when you trying to get object which is not actually same in json format (means json starting object is different not as you guys are converting).
For Ex:- Json returning first object is Boolean but unfortunately you are converting is to List<Object> then you will having this error.
I would suggest to have a look to read format using below code than convert it as per the object returning.

        ObjectMapper objectMapper = new ObjectMapper();
        Map<?,?> empMap = objectMapper.readValue(new FileInputStream("employee.json"),Map.class);

        for (Map.Entry<?,?> entry : empMap.entrySet())
        {
          System.out.println("\n----------------------------\n"+entry.getKey() + "=" + entry.getValue()+"\n");
        }

Get the key & convert the value as per the object returning.
For reference:- https://dzone.com/articles/processing-json-with-jackson

Upvotes: 7

Related Questions