Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json custom deserializer stuck in infinite recursion

Implemented a custom deserializer to deserialize a JSON by the following way. but mapper.treeToValue is causing to call the code infinite number of times.

public class MyDeserializer extends StdDeserializer<MyResource>  {
       @Override
    public myResourcedeserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
         MyResource resource = null;
        Class<? extends MyResource > clazz = null;
        ObjectMapper mapper = (ObjectMapper) parser.getCodec();
        ObjectNode node = (ObjectNode) mapper.readTree(parser);

        Iterator<Map.Entry<String, JsonNode>> elementsIterator = node.fields();

        while (elementsIterator.hasNext()) {
            Map.Entry<String, JsonNode> element = elementsIterator.next();
            if(element.getKey().equals("typeId"))
            {
                if(element.getValue().asInt() == 1)
                {
                    clazz = SpecificResource.class;
                    break;
                }
            }
        }
        return  mapper.treeToValue(node,clazz);
}

After executing mapper.treeToValue, control again goes back myResourcedeserialize method and executing it infinite number of times and causing stackOverFlowError.

Any suggestions please?

like image 331
Venky Avatar asked Dec 01 '16 17:12

Venky


People also ask

How to implement a custom deserializer using jsondeserialize?

We can implement a custom deserializer by extending the StdDeserializer class with a generic type Employee and need to override the deserialize () method of StdDeserializer class. In the below program, we can implement a custom deserializer using @JsonDeserialize annotation

What is @jsondeserialize annotation in Java?

Java JSON Object Oriented Programming Programming The @JsonDeserialize annotation is used to declare custom deserializer while deserializing JSON to Java object. We can implement a custom deserializer by extending the StdDeserializer class with a generic type Employee and need to override the deserialize () method of StdDeserializer class.

How to control which fields get serialized/deserialized by Jackson?

How to control which fields get serialized/deserialized by Jackson and which fields get ignored. Control your JSON output with Jackson 2 by using a Custom Serializer. 2. Standard Deserialization Let's start by defining 2 entities and see how Jackson will deserialize a JSON representation to these entities without any customization:

What is the difference between jsonparser and deserializationcontext in Java?

JsonParser: Reads JSON content. DeserializationContext: Context for the process of deserialization a single root-level value. We can iterate JSON property as following. Here p is the instance of JsonParser in deserialize () method.


1 Answers

We have faced similar situation and this solution is based on that: The custom de-serializer is used to parse JSON polymorphically without using type attribute

Lets call the polymorphic classes as BaseResource (base class), SubResource1, SubResource2.

BaseResource has annotation to specify which CustomSerializer class

@JsonDeserialze(using=CustomDeserializer.class)
public class BaseResource{
......
}

So on SubResource1 and SubResource2, we add annotations to not use the CustomDeserialzer.class by using JsonDeserialer.None.class

@JsonDeserialze(using=JsonDeserialer.None.class)
public class SubResource1 extends BaseResource{
......
}

Reference: http://www.robinhowlett.com/blog/2015/03/19/custom-jackson-polymorphic-deserialization-without-type-metadata/

like image 186
Narasimha Avatar answered Oct 18 '22 11:10

Narasimha