Is there a way to use annotation on a List property in a class to use ACCEPT_SINGLE_VALUE_AS_ARRAY
in Jackson
? I'm using Spring and getting the below exception
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
Assume I have a class as below:
public class MyClass {
private List < String > value;
}
And my JSON structures are as below:
case 1:
[{"operator": "in", "value": ["Active"], "property": "status"}]
case 2:
[{"operator": "like", "value": "aba", "property": "desc"}]
What annotation should I use to let the framework know I want these 2 cases to be treated the same when deserializing.
UPDATE: I moved the updates to an answer in this post for more clarity.
I'm just answering my own question for clarity. One of the answers was to upgrade to the higher version so that I can use annotations. I cannot do it due to dependency restrictions of my project.
As a result, based on Michal Foksa answer I solved my problem by creating a custom deserializer. Its as below:
On my property:
@JsonDeserialize(using = CustomStringDeserializer.class)
private List<String> value;
And my Deserializer:
public class CustomStringDeserializer extends JsonDeserializer<List<String>>{
@Override
public List<String> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature. ACCEPT_SINGLE_VALUE_AS_ARRAY);
return mapper.readValue(p, List.class);
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments