我想根据type反序列化成接口的对象,示例中必须把实现类写入接口中,但实际开发中接口类往往不知道有哪些实现类,或者实现类可以不断增加,接口应该不需要改变。
请问有没有其他的办法注册seeAlso这个参数?
/**
* 看这里:seeAlso能不能再其他的代码中追加???
* /
@JSONType(seeAlso = {FloorV2.class}, typeKey = "type")
public interface Area {
public static final String TYPE_SECTION = "section";
public static final String TYPE_FLOORV1 = "floorV1";
public static final String TYPE_FLOORV2 = "floorV2";
}
@JSONType(typeName = "floorV2")
public static class FloorV2 implements Area {
public String type;
public String templateId;
}
// testcase code
String json = "{\"type\":\"floorV2\",\"templateId\":\"x123\"}";
FloorV2 floorV2 = (FloorV2) JSON.parseObject(json, Area.class);
assertNotNull(floorV2);
assertNotNull(floorV2.templateId);
assertEquals("x123", floorV2.templateId);
assertEquals("floorV2", floorV2.type);
String json2 = JSON.toJSONString(floorV2, SerializerFeature.WriteClassName);
assertEquals("{\"type\":\"floorV2\",\"templateId\":\"x123\"}", json2);
1条答案
按热度按时间8yparm6h1#
同求解决方案,@JSONType 子类自己注解还好,但是父类同步要加 seeAlso 就坑大了,是否考虑支持显示注册类型Map,例如
typeMappingRegistry.register(someTypeName, SomeConcreteClass.class, SomeAbstractClass.class)