gpt4 book ai didi

java - 如何使用 JsonTypeInfo 和反/序列化器来自定义处理多态子类型?

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:28 25 4
gpt4 key购买 nike

我遇到了一种情况,我需要自定义某些 JSON 的序列化/反序列化。我已将其简化为一个可读的示例。我有一个 Container 类,其中包含实现 MyInterface 的对象。在我的示例中,ClassA、ClassB、IntegerHolder 和 StringHolder 实现了该接口(interface)。通过将 @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 的序列化,因为它们只是 native 类型的包装器。我的 JSON 将经常手动编辑,并且将大量使用原始类型。所以我想将 JSON 简化为:

{"type":"Container","items":
[ {"type":"classA","aValue":"AAA"},
{"type":"classB","bValue":"BBB"},
123,
"abc" ] }

我编写了一个序列化器和反序列化器(扩展 StdSeralizer 和 StdDeserializer),将它们放入 SimpleModule 中并使用映射器注册它(如图 here on SO 所示),并且单独运行,效果很好。我的意思是,如果 IntegerHolder 和 StringHolder 是容器中唯一的对象,并且只有从接口(interface)中删除 @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)

我觉得 Jackson 可以做到这一点,并且我即将将 Jackson 配置为序列化/反序列化两组类,但到目前为止,我的尝试尚未取得成果。

任何能让我朝着正确方向前进的指示将不胜感激......提前致谢!

以下是我的测试示例中的 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;
}

B类.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;
}

最佳答案

两个选项:

  1. MyInterface 自定义反序列化器,然后您不需要 JsonTypeInfo - 所有逻辑都将在反序列化器中。
  2. 您可以尝试让 IntegerHolderStringHolder 实现另一个接口(interface)(例如 Holder),并将 JsonTypeInfo 注释更改为:
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl=Holder.class)
    并为 Holder.class 指定一个解串器。

关于java - 如何使用 JsonTypeInfo 和反/序列化器来自定义处理多态子类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30894157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com