Reputation: 1479
I'm trying to deserialize this Json
element in my code Json array to a custom class .. but i couldn't :
ObjectMapper OMapper = new ObjectMapper();
OMapper.configure(Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
OMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
OMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
OMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
OMapper.enable(SerializationFeature.INDENT_OUTPUT);
JsonNode node = OMapper.readTree("{\"headers\":[\"st1\", \"ddf2\", \"sdsd\"]}");
headers h = OMapper.treeToValue(node.get("headers"), headers.class);
and this is the class :
public class headers{
public headers(){
System.out.println("dssdsd");
}
@JsonSetter
public void set(){
System.out.println("ASAASAAS");
}
@JsonGetter()
public JsonNode get(int index){
System.out.println("XXXXXXXXXx");
return null;
}
}
but i'm getting this error :
com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of headers out of START_ARRAY token at [Source: UNKNOWN; line: -1, column: -1] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:62) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1307) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1116) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1070) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1447) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:173) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:149)
Upvotes: 0
Views: 1185
Reputation: 5025
You should specify headers
as an array.
headers[] h = OMapper.treeToValue(node.get("headers"), headers[].class);
Upvotes: 1