Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON + Java Generics

I am trying to deserialize/map the below JSON to List<Bill> java object using Jackson json library. (this json was generated by jackson, Iam omitting that piece for brevity)

{"bills":[{"amount":"13","billId":"billid3"}]}

Here is my conversion method:

private static void convert(){
   String jsonBill =  "{\"bills\":[{\"amount\":\"13\",\"billId\":\"billid3\"}]}";

   ObjectMapper mapper = new ObjectMapper();
   List<Bill> bills = null;
   try {
       bills = mapper.readValue(jsonBill, new TypeReference<List<Bill>>() { });
   } catch (Exception e) {
       e.printStackTrace();
   }
   System.out.println("bills = " + bills.size());
}

The Bill entity is below:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS)
public class Bill { 
   private String amount;
   private String billId;

   public String getBillId() {
       return billId;
   }
   public void setBillId(String billId) {
       this.billId = billId;
   }
   public String getAmount() {
       return amount;
   }
   public void setAmount(String amount) {
       this.amount = amount;
   } 
}

and I get this error:

**org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token
 at [Source: java.io.StringReader@7a84e4; line: 1, column: 1]**
 at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
 at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:194)
 at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:103)
 at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:93)
 at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1980)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1278)

Here is my simplified spring3 controller which returns the i/p json (with Jackson mapping configured as default view):

@ModelAttribute("bills")
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<Bill> fetchBills() throws IOException {
    Bill bill = new Bill();
    bill.setAmount("13");
    bill.setBillId("billid3");

    List<Bill> bills = new ArrayList<Bill>();
    bills.add(bill);
    return bills;
}

I guess I am missing something obvious.. but not sure what it is.. Any ideas?

like image 532
Ravi Avatar asked Dec 15 '10 03:12

Ravi


2 Answers

The problem lies not in your code, but your example input. What you're actually trying to deserialize is an object with a field named "bills", not a list! What you should be using as input is:

[{"billId":"billid3","amount":"13"}]

This is an array of objects, which is converted to a list.

like image 78
Jason Nichols Avatar answered Sep 28 '22 17:09

Jason Nichols


Try using ObjectWriter instead of ObjectMapper

Writer writer=new StringWriter();

        ObjectWriter oWriter=om.writerWithType(new TypeReference<List<Bill>>() {
        });
        oWriter.writeValue(writer, result);

I'm using jackson 1.9.2

like image 20
neoreeprast Avatar answered Sep 28 '22 17:09

neoreeprast