7

I am getting the below error message, can someone please help or suggest how best to debug this.

Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 37610] (through reference chain: com.model.ProductList["products"]->java.util.ArrayList[23]->com.model.Product["price"]->com.Price["now"])

I am trying to deserialise a Products object from a REst API call. The code has been working fine until I added code to deserialise a Price sub-class. This looks as follows,

"price": {
        "was": "",
        "then1": "",
        "then2": "",
        "now": "59.00",
        "uom": "",
        "currency": "GBP"
      },

My Price pojo looks as follows,

public class Price {

     @JsonProperty("was")
     String was;
     @JsonProperty("then1")
     String then1;
     @JsonProperty("then2")
     String then2;
     @JsonProperty("now")
     String now;
     @JsonProperty("uom")
     String uom;
     @JsonProperty("currency")
     String currency;

     public Price() {
         //blank constructor for JSON
     }

     @Override
     public String toString() {
         return "Price{" +
                 "was='" + was + '\'' +
                 ", then1='" + then1 + '\'' +
                 ", then2='" + then2 + '\'' +
                 ", now='" + now + '\'' +
                 ", uom='" + uom + '\'' +
                 ", currency='" + currency + '\'' +
                 '}';
     }
}

I have written a Junit test to try and simulate the error but it works in my test,

    @Test
    public void shouldConvertJsonProductListIntoPrice() {
        ObjectMapper objectMapper = new ObjectMapper();
        String content3 = "{\"products\": [{\"productId\": \"3525085\",\"title\": \"hush Tasha Vest Dress\", " +
                "\"price\": {\"was\": \"\",\"then1\": \"\",\"then2\": \"\",\"now\": \"59.00\",\"uom\": \"\",\"currency\": \"GBP\"}, " +
                "\"colorSwatches\": [{\"basicColor\": \"Red\",\"skuId\": \"237494589\"},{\"basicColor\": \"Blue\",\"skuId\": \"237494562\"}] " +
                "}]}";
        JavaType valueType = objectMapper.constructType(ProductList.class);
        ProductList readValue;
        try {
            readValue = objectMapper.readValue(content3, valueType);
            assertEquals("3525085", readValue.getProductList().get(0).productId);
            assertEquals("hush Tasha Vest Dress", readValue.getProductList().get(0).title);
            assertEquals("", readValue.getProductList().get(0).price.then1);
            assertEquals("59.00", readValue.getProductList().get(0).price.now);
            assertEquals("Blue", readValue.getProductList().get(0).colorSwatches[1].basicColor);
            assertEquals("237494562", readValue.getProductList().get(0).colorSwatches[1].skuId);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

If I comment out the "now" field then my RestAPI call works perfectly and I dont see the exception. So it seems that there is a problem with the "now" field and here I notice that its trying to convert "59.00" into a String. Could this be a problem for the Fasterxml converter ? do I need to help it maybe ?

The Product class is as follows (although this is much reduced list of fields that I am receiving off the API call).

public class Product {

     @JsonProperty("productId")
     String productId;

     @JsonProperty("title")
     String title;

     @JsonProperty("colorSwatches")
     ColorSwatch [] colorSwatches;

     @JsonProperty("price")
     Price price;

     public Product(){
         // blank required for Jackson
     }

     public Product(String productId, String title, ColorSwatch[] colorSwatches, Price price){
         this.productId = productId;
         this.title = title;
         this.colorSwatches = colorSwatches;
         this.price = price;
  }
11
  • Why are you parsing and constructing JSON Manually? Why not let Spring/Jackson do this for you?
    – Todd
    Jan 6, 2019 at 14:29
  • Hi Todd, the error I reported above is coming from Spring/Jackson. I wrote the Unit Test to try and see if I could reproduce the error but I wasnt able to. Unless I have misunderstood you. Have I added code which I should be leaving to Spring/Jackson to do for me ?
    – robbie70
    Jan 6, 2019 at 14:37
  • I tried using your code and it works well! Show us your product class as well if you can.
    – ScanQR
    Jan 6, 2019 at 14:37
  • thanks ScanQR - how did you test it ? I seem to get it when call the full RestAPI and the Spring/Jackson processes are handling the unmarshalling.
    – robbie70
    Jan 6, 2019 at 14:42
  • 1
    @robbie70 I think you need to test it from ProductList class which you have. Please try to replicate the same values that you are doing. And can you add code where you can you are calling to the deserializer?thank you Jan 6, 2019 at 16:49

1 Answer 1

15

The error states it expects a VALUE (VALUE_STRING preferably), whereas it gets a START_OBJECT, so your issue is probably coming from a json of the form

"price": {
    "was": "",
    "then1": "",
    "then2": "",
    "now":  {
        ...
    }
    "uom": "",
    "currency": "GBP"
},

Instead of the "now": "some value" form expected by the code.

6
  • wow well done I think that might be it. I just searched the record set and found at record 25 of 50 an entry similar to how you've described,
    – robbie70
    Jan 8, 2019 at 22:22
  • { "productId": "3341058", "type": "product", "title": "Jolie Moi Bonded Lace Prom Dress", "code": "69251713", "averageRating": 0, "reviews": 0, "price": { "was": "85.00", "then1": "68.00", "then2": "", "now": { "from": "59.00", "to": "68.00" }, "uom": "", "currency": "GBP" },
    – robbie70
    Jan 8, 2019 at 22:24
  • its a pity I cant format the record as nicely as you have in your example. Tomorrow I will check it in my app but I am pretty sure thats it.
    – robbie70
    Jan 8, 2019 at 22:24
  • If that is the case then do you know how I should reprensent the data type - it looks like it can either be sent as a String or an Object Type - how should I define that in my pojo ?
    – robbie70
    Jan 8, 2019 at 22:25
  • 1
    You are welcome. For deserializing a node that can be either a String or an Object, you could give a look to @JsonSerialize giving a custom JsonDeserializer Jan 9, 2019 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.