/CTO @Software Service and Innovation /JUGLviv leader /JDayLviv co-organiser


Tricky issue on jackson serialization/deserialization

Andriy AndrunevchynAndriy Andrunevchyn

Just few days ago I was implementing small feature on one of our minor projects .  The idea process some data on user interface pass it to backend and send it further to third party service.

So on UI we have code like this

$.post( "/backendService", JSON.stringify(idList); );

Obviously idList contains plain long type id values e.g. 54396 so that on backendService we have following piece of code

List<Long> idList= mapper.readValue(idListJson, List.class);

And later on I would like include that list as part of other structure

public class ThirdPartyServiceRequest {

public List<Long> idList = new ArrayList<Long>();

so  I did following

ThirdPartyServiceRequest request = new ThirdPartyServiceRequest();
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(request);
            executePOSTQuery("ThirdPartyServiceURL", json);

So everything pretty clear I wrote unit test where I manually created idList and so on, everything worked like a  charm, but when I started testing the whole solution following line

String json = mapper.writeValueAsString(request);

threw me exception

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: java.lang.String cannot be cast to java.lang.Long (through reference chain: com.pack.ThirdPartyServiceRequest ["idList"]->java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContentsUsing(
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(
    at com.pack.BackendService(
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
    at com.fasterxml.jackson.databind.ser.std.NumberSerializers$LongSerializer.serialize(
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContentsUsing(
    ... 10 more


So what a ClassCastException from String to Long is mentioned if we do exactly opposite and convert List<Long> to String ?

I’m sure many watchful readers already figured out issue. I did a mistake in following line

List<Long> idList = mapper.readValue(idListJson, List.class);

We cannot do it. If we check json we received from UI we will see following line


returns such json


so that when we use jackson to deserialize we should be careful and use proper generic for list

List<String> idList= mapper.readValue(idListJson, List.class);

or provide proper type

List<Long> idList = mapper.readValue(idListJson, new TypeReference<list<long>>() {})

It’s pure developer faults and it’s quite easy to do such mistake so remember about it



Thx to comments MVMn the proper fix would change not server side but UI part


So before writing to idList you have check if it’s really int and not a String and convert it to int on UI if needed with function parseInt

In my case I receive id as value of checkboxes and obviously it is String not int

/CTO @Software Service and Innovation /JUGLviv leader /JDayLviv co-organiser