Search code examples
serializationjacksonstack-overflowrecursive-datastructures

Serializing a Recursive Class with Jackson (JSON)


I've defined a tree of HashMaps like so:

public class HashTree<K, V> extends HashMap<K, HashTree<K, V>> {

    private static final long serialVersionUID = 1L;
    boolean isLeaf = true;
    private HashMap<K, V> value = new HashMap<K, V>();

    public HashMap<K, V> getValues() {
        return value;
    }

    public void setValues(HashMap<K, V> value) {
        this.value = value;
    }

    public void putValue(K key, V value) {
        this.value.put(key, value);
    }

    public void removeValue(K key) {
        this.value.remove(key);
    }

    public void clearValues() {
        this.value.clear();
    }

}

Note the type is recursive: HashMap<K, HashTree<K, V>>. In Java this works fine, but when I serialize with Jackson I get a stack overflow:

java.lang.StackOverflowError
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:232)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:269)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:309)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:309)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:309)
    com.fasterxml.jackson.databind.type.TypeBindings._resolve(TypeBindings.java:203)
    com.fasterxml.jackson.databind.type.TypeBindings.findType(TypeBindings.java:121)
    com.fasterxml.jackson.databind.type.TypeFactory._fromVariable(TypeFactory.java:798)
    com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:343)
    com.fasterxml.jackson.databind.type.TypeFactory.findTypeParameters(TypeFactory.java:279)
    com.fasterxml.jackson.databind.type.TypeFactory.findTypeParameters(TypeFactory.java:251)
    com.fasterxml.jackson.databind.type.TypeFactory._fromParamType(TypeFactory.java:759)
    com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:337)

...

I've tried to intersept the serialisation with this, which should just return a string "123" when it encounters a HashTree.

public class HashTreeSerializer<K,V> extends StdSerializer<HashTree<K,V>> {

    public HashTreeSerializer() {
        super(HashTree.class, false);
    }

    @Override
    public void serialize(HashTree<K, V> tree, JsonGenerator json,
            SerializerProvider provider) throws IOException,
            JsonGenerationException {
        json.writeString("123");
    }

}

I've even extended HashTree to StringHashTree to remove those generic types, but I still get a stack overflow. Any ideas?


Solution

  • This is a bug; type resolver should be able to resolve this to an acceptable variable-free type.