jackson JsonDeserializer stackoverflow

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


jackson JsonDeserializer stackoverflow



I am trying to extract attributes from a json sting in Java and I am writing a JsonDeseriizer to do it.
My attempt is shown below:


public class testJacksonSimple {
@JsonDeserialize(using = ItemDeserializerJson.class)
public static class Item {
public int id;
public String itemName;

public Item(){};
public Item(int id, String itemName) {
this.id = id;
this.itemName = itemName;
}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}
}

public static class ItemDeserializerJson extends JsonDeserializer<Item> {

ObjectReader reader = new ObjectMapper().reader();

@Override
public Item deserialize(JsonParser jp, DeserializationContext context) throws IOException {
if(jp.currentToken() != JsonToken.VALUE_NULL) {
return reader.readValue(jp, Item.class);
} else {
return null;
}
}
}
}



This is my test case:


public class test {
public static void main(String args){
ObjectMapper mapper = new ObjectMapper();
String json = "{n" +
" "id": 1,n" +
" "itemName": "theItem"n" +
"}";
System.out.println(json);
testJacksonSimple.Item itemWithOwner = null;
try {
itemWithOwner = new ObjectMapper().readValue(json, testJacksonSimple.Item.class);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(itemWithOwner.id);
System.out.println(itemWithOwner.itemName);
}
}



I got the error:


Exception in thread "main" java.lang.StackOverflowError
at java.lang.reflect.Method.getParameterTypes(Method.java:264)
at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:59)
at com.sun.proxy.$Proxy2.annotationType(Unknown Source)
at com.fasterxml.jackson.databind.introspect.AnnotationMap._add(AnnotationMap.java:135)
at com.fasterxml.jackson.databind.introspect.AnnotationMap.addIfNotPresent(AnnotationMap.java:101)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass._addAnnotationsIfNotPresent(AnnotatedClass.java:1061)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass._resolveClassAnnotations(AnnotatedClass.java:399)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass._classAnnotations(AnnotatedClass.java:376)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass.getAnnotation(AnnotatedClass.java:248)
at com.fasterxml.jackson.databind.AnnotationIntrospector._findAnnotation(AnnotationIntrospector.java:1436)
at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.findAutoDetectVisibility(JacksonAnnotationIntrospector.java:349)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.<init>(POJOPropertiesCollector.java:129)



Just do not understand why I got error stackoverflow. I can get correct result without using custom JSONDeserializer, but I want to test it. Any help is appreciated.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name