Cannot deserialize an Object that contains an ArrayList

4,564 views
Skip to first unread message

Meissa M'baye Sakho

unread,
Dec 18, 2020, 4:34:26 AM12/18/20
to jackson-user
Hello everyone,
I have marshalled  an object with jackson and I'm trying unmarshall the generated json back to java and it does not seem to work.
The object contains a single attribute which is a an ArrayList:

public class CacheStore implements Serializable {

private ArrayList<CacheItem> store;

}

The CacheItem has two attributes:

public class CacheItem implements Serializable {

  private PacsBean entry;

  private MessageKey key;

  }

When marshalled, the generated json output is bellow:

[ {

  "entry" : {"msgId" : "msg01","transactionId" : "001","instructionId" : "instr001"},

  "key" : {"id" : "id1","bicCode" : "code1"}

},

 {

"entry" : {"msgId" : "msg02","transactionId" : "002","instructionId" : "instr002"},

  "key" : {"id" : "id1","bicCode" : "code1"}

}

 ]

When I try to unmarshall the json above, I'm expected to get a CacheStore with a store attribute containing the ArrayList of CacheItem.

The instruction that I'm using to unmarshall the json is the following:

    ObjectMapper mapper = new ObjectMapper();

    String jsonInput = BACKUP_LOCATION_FOLDER +"cachestore.json";

    CacheStore cacheStore = mapper.readValue(new File(jsonInput),  CacheStore.class);

I'm getting he following exception when I execute the instruction:
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.redhat.consulting.jdg.model.CacheStore` from Array value (token `JsonToken.START_ARRAY`)
 at [Source: (File); line: 1, column: 1]
  at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
  at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1603)

Is there something I'm missing in the instruction?
Do I need to change the CacheStore object?
Any input is appre

Meissa M'baye Sakho

unread,
Dec 18, 2020, 1:04:27 PM12/18/20
to jackson-user
I've solved the issue.
The json above did not correspond to the one expected and the default constructor was missing from the MessageKey Bean.

Reply all
Reply to author
Forward
0 new messages