Jackson的Polymorphic Serialize / Deserialize功能真的很酷,或者如果我能弄明白如何将它应用到手头的问题上。在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不允许我readValue处理ObjectMapper.writeValue()生成的JSON?
答案 0 :(得分:2)
使你的内部类静态,如:
static class Dog extends Animal { ... }
否则事情将无法工作(因为非静态内部类需要所谓的“隐式this”参数来引用封闭类的实例)。