我遇到一种情况,需要自定义某些JSON的序列化/反序列化。我已将其简化为可读的示例。我有一个Container类,其中包含实现MyInterface的对象。在我的示例中,ClassA,ClassB,IntegerHolder和StringHolder实现了该接口。通过将@JsonTypeInfo批注添加到我的界面(和容器)中:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
并为每个类注册类型名称,我可以成功地将其读取/写入此JSON:
{"type":"Container","items":
[ {"type":"classA","aValue":"AAA"},
{"type":"classB","bValue":"BBB"},
{"type":"intHolder","value":123},
{"type":"stringHolder","value":"abc"} ] }
一切都非常好:)我的问题是我想自定义intHolder和stringHolder的序列化,因为它们只是本机类型的包装。我的JSON将经常被手工编辑,原始类型将被大量使用。所以我想将JSON简化为:
{"type":"Container","items":
[ {"type":"classA","aValue":"AAA"},
{"type":"classB","bValue":"BBB"},
123,
"abc" ] }
我已经编写了一个序列化器和反序列化器(扩展了StdSeralizer和StdDeserializer),将它们放在SimpleModule中,并在映射器中注册了它(如here on SO所示),并且隔离地运行良好。那样的话,我的意思是,如果IntegerHolder和StringHolder是容器中唯一的对象,并且只有从接口中删除@JsonTypeInfo批注,我才能对它们进行序列化/反序列化。如果不这样做,则在写入JSON时遇到此错误:
[main] ERROR MyTests - can't write the Container
com.fasterxml.jackson.databind.JsonMappingException: Type id handling not implemented for type MyInterface (by serializer of type MyTests$MyInterfaceSerializer) (through reference chain: Container["items"])
at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1047)
at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:142)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serializeTypedContents(ObjectArraySerializer.java:316)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serializeContents(ObjectArraySerializer.java:217)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serialize(ObjectArraySerializer.java:201)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serialize(ObjectArraySerializer.java:25)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:552)
at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3387)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2747)
at MyTests.testItemSerializationDeserializationEquality(MyTests.java:51)
at MyTests.testSerialization(MyTests.java:41)
但是,当然,在删除@JsonTypeInfo的情况下,Jackson不知道如何反序列化ClassA和ClassB ...因此在读取JSON时失败:
[main] INFO MyTests - {"type":"Container","items":[{"aValue":"AAA"},{"bValue":"BBB"},123,"abc"]}
[main] ERROR MyTests - can't read the Container
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of MyInterface, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
at [Source: java.io.ByteArrayInputStream@37883b97; line: 1, column: 45] (through reference chain: Container["items"]->Object[][0])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:857)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:139)
at MyTests$MyInterfaceDeserializer.deserialize(MyTests.java:163)
at MyTests$MyInterfaceDeserializer.deserialize(MyTests.java:139)
我觉得杰克逊可以做到,而且我已经接近让杰克逊配置为对这两组类进行序列化/反序列化,但是到目前为止,我的尝试还没有取得成果。
任何使我朝正确方向前进的指针将不胜感激...谢谢!
这是我的测试示例中的7个类:
MyInterface.java
import com.fasterxml.jackson.annotation.*;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface MyInterface
{
}
容器.java
import com.fasterxml.jackson.annotation.*;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public class Container
{
public Container()
{
}
public Container(MyInterface... items)
{
this.items = items;
}
public MyInterface[] getItems()
{
return items;
}
public void setItems(MyInterface[] items)
{
this.items = items;
}
@Override
public boolean equals(Object obj)
{
for (int i = 0; i < items.length; i++)
if (!(items[i].equals(((Container)obj).items[i])))
return false;
return true;
}
private MyInterface[] items;
}
MyTests.java
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.std.*;
import com.fasterxml.jackson.databind.jsontype.*;
import com.fasterxml.jackson.databind.module.*;
import com.fasterxml.jackson.databind.node.*;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.std.*;
import org.junit.*;
import org.slf4j.*;
import java.io.*;
public class MyTests
{
@Test
public void testSerialization()
{
ClassA a = new ClassA();
a.setaValue("AAA");
ClassB b = new ClassB();
b.setbValue("BBB");
IntegerHolderClass int_holder = new IntegerHolderClass();
int_holder.setValue(123);
StringHolderClass string_holder = new StringHolderClass();
string_holder.setValue("abc");
// Testing with ONLY the non-customized classes works fine with the @JsonTypeInfo annotation on MyInterface
// if the custom de/serializers are not registered via the module
// testItemSerializationDeserializationEquality(new Container(a, b), Container.class);
// Testing with ONLY the customized classes works fine with the custom de/serializers registered via the module
// if the @JsonTypeInfo annotation on MyInterface is removed
// testItemSerializationDeserializationEquality(new Container(int_holder, string_holder), Container.class);
// This variation tests them all together...doesn't work under either scenario
testItemSerializationDeserializationEquality(new Container(a, b, int_holder, string_holder), Container.class);
}
private void testItemSerializationDeserializationEquality(Object original, Class expected_super_type)
{
ObjectMapper mapper = createMapper();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
try
{
mapper.writeValue(outstream, original);
outstream.flush();
}
catch (IOException e)
{
LOG.error("can't write the " + original.getClass().getSimpleName(), e);
}
LOG.info(outstream.toString());
Object copy = null;
try
{
copy = mapper.readValue(new ByteArrayInputStream(outstream.toByteArray()), expected_super_type);
}
catch (Exception e)
{
LOG.error("can't read the " + original.getClass().getSimpleName(), e);
}
Assert.assertNotNull(copy);
Assert.assertTrue(copy.equals(original));
}
private ObjectMapper createMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.registerSubtypes(new NamedType(ClassA.class, "classA"));
mapper.registerSubtypes(new NamedType(ClassB.class, "classB"));
mapper.registerSubtypes(new NamedType(IntegerHolderClass.class, "intHolder"));
mapper.registerSubtypes(new NamedType(StringHolderClass.class, "stringHolder"));
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier()
{
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer)
{
if (MyInterface.class.isAssignableFrom(beanDesc.getBeanClass()))
return new MyInterfaceDeserializer(deserializer);
return deserializer;
}
});
module.setSerializerModifier(new BeanSerializerModifier()
{
@Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer)
{
if (MyInterface.class.isAssignableFrom(beanDesc.getBeanClass()))
return new MyInterfaceSerializer(serializer);
return serializer;
}
});
mapper.registerModule(module);
return mapper;
}
static class MyInterfaceSerializer extends StdSerializer<MyInterface> implements ResolvableSerializer
{
public MyInterfaceSerializer(JsonSerializer<?> def)
{
super(MyInterface.class);
_default = (JsonSerializer<MyInterface>) def;
}
@Override
public void serialize(MyInterface value, JsonGenerator jgen, SerializerProvider provider) throws IOException
{
if (value instanceof IntegerHolderClass)
jgen.writeNumber(((IntegerHolderClass) value).getValue());
else if (value instanceof StringHolderClass)
jgen.writeString(((StringHolderClass) value).getValue());
else
_default.serialize(value, jgen, provider);
}
@Override
public void resolve(SerializerProvider provider) throws JsonMappingException
{
}
private final JsonSerializer<MyInterface> _default;
}
static class MyInterfaceDeserializer extends StdDeserializer<MyInterface> implements ResolvableDeserializer
{
public MyInterfaceDeserializer(JsonDeserializer<?> def)
{
super(MyInterface.class);
_default = def;
}
@Override
public MyInterface deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
TreeNode node = parser.getCodec().readTree(parser);
if (node instanceof TextNode)
{
StringHolderClass holder = new StringHolderClass();
holder.setValue(((TextNode) node).textValue());
return holder;
}
else if (node instanceof IntNode)
{
IntegerHolderClass holder = new IntegerHolderClass();
holder.setValue(((IntNode) node).intValue());
return holder;
}
return (MyInterface) _default.deserialize(parser, context);
}
@Override
public void resolve(DeserializationContext context) throws JsonMappingException
{
// ((ResolvableDeserializer)_default).resolve(context);
}
private final JsonDeserializer<?> _default;
}
final static Logger LOG = LoggerFactory.getLogger(MyTests.class);
}
ClassA.java
public class ClassA implements MyInterface
{
public String getaValue()
{
return _aValue;
}
public void setaValue(String aValue)
{
_aValue = aValue;
}
@Override
public boolean equals(Object obj)
{
return obj instanceof ClassA && _aValue.equals(((ClassA)obj)._aValue);
}
private String _aValue;
}
ClassB.java
public class ClassB implements MyInterface
{
public String getbValue()
{
return _bValue;
}
public void setbValue(String bValue)
{
_bValue = bValue;
}
@Override
public boolean equals(Object obj)
{
return obj instanceof ClassB && _bValue.equals(((ClassB)obj)._bValue);
}
private String _bValue;
}
StringHolderClass.java
public class StringHolderClass implements MyInterface
{
public String getValue()
{
return _value;
}
public void setValue(String value)
{
_value = value;
}
@Override
public boolean equals(Object obj)
{
return obj instanceof StringHolderClass && _value.equals(((StringHolderClass)obj)._value);
}
private String _value;
}
IntegerHolderClass.java
public class IntegerHolderClass implements MyInterface
{
public int getValue()
{
return _value;
}
public void setValue(int value)
{
_value = value;
}
@Override
public boolean equals(Object obj)
{
return obj instanceof IntegerHolderClass && _value.equals(((IntegerHolderClass)obj)._value);
}
private Integer _value;
}
最佳答案
两种选择:
为MyInterface
自定义解串器,然后就不需要JsonTypeInfo-所有逻辑都将在解串器中。
您可以尝试让IntegerHolder
和StringHolder
实现另一个接口,假设Holder
并将JsonTypeInfo
批注更改为:@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl=Holder.class)
并为Holder.class指定一个反序列化器。