2

Jackson 的多态序列化/反序列化功能真的很酷,或者如果我能弄清楚如何将它应用到我手头的问题上。http://programmerbruce.blogspot.com/2011/05/deserialize-json-with-jackson-into.html上有一篇很好的文章,我无法适应我的简化问题。

简而言之,我能够让 Jackson 2.1.2 将类层次结构序列化为带有类型信息的 JSON 字符串。但是,我无法让 Jackson 2.1.2 将该 JSON 字符串反序列化回我的类层次结构中。下面是一个暴露这个问题的单元测试。

类层次结构很简单;只有两个直接子类的基类。此外,JSON 输出似乎尊重我的 Jackson @JsonTypeInfo 并从 mapper.writeValueAsString 生成一个可信的字符串

{"type":"dog","name":"King","breed":"Collie"}

但是我对 mapper.readValue(jsonOfKing, Animal.class) 堆栈跟踪的调用...

FAILED: testJacksonSerializeDeserialize
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.rekdev.fasterjacksonwtf.PolymorphismTests$Dog]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@32b3a5a0; line: 1, column: 14]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:400)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:289)
....

这是我的单元测试。

import org.testng.annotations.*;
import static org.testng.Assert.*;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.databind.*;

public class PolymorphismTests {
    @Test
    public void testJacksonSerializeDeserialize() throws Exception {
        ObjectMapper mapper = new ObjectMapper();

        Animal king = new Dog();
        king.name = "King";
        ( (Dog) king ).breed = "Collie";
        String jsonOfKing = mapper.writeValueAsString( king );
        // JsonMappingException right here!
        Animal actualKing = mapper.readValue( jsonOfKing, Animal.class );
        assertEquals( king, actualKing );

    }

    @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type" )
    @JsonSubTypes( { @Type( value = Cat.class, name = "cat" ), @Type( value = Dog.class, name = "dog" ) } )
    abstract class Animal {
        public String name;

        @Override
        public abstract boolean equals( Object obj );

        @Override
        public abstract int hashCode();
    }

    class Dog extends Animal {
        public String breed;

        @Override
        public boolean equals( Object obj ) {
            if ( this == obj ) {
                return true;
            }
            if ( obj == null ) {
                return false;
            }
            if ( getClass() != obj.getClass() ) {
                return false;
            }
            final Dog that = (Dog) obj;
            boolean equals = name.equals( that.name ) && breed.equals( that.breed );
            return equals;
        }

        @Override
        public int hashCode() {
            int hashCode = name.hashCode() + breed.hashCode();
            return hashCode;
        }
    }

    class Cat extends Animal {
        public String favoriteToy;

        @Override
        public boolean equals( Object obj ) {
            if ( this == obj ) {
                return true;
            }
            if ( obj == null ) {
                return false;
            }
            if ( getClass() != obj.getClass() ) {
                return false;
            }
            final Cat that = (Cat) obj;
            boolean equals = name.equals( that.name ) && favoriteToy.equals( that.favoriteToy );
            return equals;
        }

        @Override
        public int hashCode() {
            int hashCode = name.hashCode() + favoriteToy.hashCode();
            return hashCode;
        }
    }
}

为什么 ObjectMapper 不允许我对由 ObjectMapper.writeValue() 生成的 JSON 进行 readValue 处理?

4

1 回答 1

2

使您的内部类静态,例如:

static class Dog extends Animal { ... }

否则事情将无法正常工作(因为非静态内部类需要所谓的“隐式 this”参数来引用封闭类的实例)。

于 2013-01-11T03:53:51.853 回答