@JsonSubTypes
实现多态解析假如有以下格式json
json1.json 文件内容
[
{
"id": "1",
"type": "startEvent",
"startEvent": "begin",
"startInfo": "hello world"
},
{
"id": "2",
"type": "endEvent",
"endEvent": "end",
"endInfo": "hello jackson"
}
]
json2.json 文件内容
{
"id": "1",
"type": "startEvent",
"startEvent": "begin",
"startInfo": "hello world"
}
特点:
id&type
)Event&Info
是每个子类特定的字段BaseElement
@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible =
true)
@JsonSubTypes({@JsonSubTypes.Type(value = StartEvent.class, name = "startEvent"),
@JsonSubTypes.Type(value = EndEvent.class, name = "endEvent")})
public class BaseElement {
String id;
String type;
}
StartEvent
@ToString(callSuper = true)
@Data
public class StartEvent extends BaseElement{
private String startEvent;
private String startInfo;
}
EndEvent
@ToString(callSuper = true)
@Data
public class EndEvent extends BaseElement{
private String endEvent;
private String endInfo;
}
@Test
public void version1Json2Test() {
String json = IoUtil.readFileAsString("json2.json");
ObjectMapper mapper = new ObjectMapper();
try {
StartEvent startEvent = (StartEvent) mapper.readValue(json, BaseElement.class);
System.out.println(startEvent);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void version1Json1Test() {
String jsonString = IoUtil.readFileAsString("json1.json");
System.out.println(jsonString);
ObjectMapper objectMapper = new ObjectMapper();
try {
List<BaseElement> baseElements = new ArrayList<>();
baseElements = objectMapper.readValue(jsonString,
new TypeReference<List<BaseElement>>() {
});
baseElements.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
JsonTypeInfo
借助JsonSubTypes
注解来感知抽象类的有哪些实现类,并且是如何匹配的。在大型工程中抽象类的子类很多(接口的实现很多),那么JsonSubTypes注解就十分臃肿了。而且这种写法是 违反开闭原则(OCP) 的。借助以下方式可以将JsonSubTypes剔除掉,达到相同的效果。
下面使用一种GitHub上的开源框架 https://github.com/ronmamo/reflections
BaseElement
@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property =
"type", visible =
true)
public class BaseElement {
String id;
String type;
}
EndEvent
@ToString(callSuper = true)
@Data
@JsonTypeName(value = "endEvent")
public class EndEvent extends BaseElement {
private String endEvent;
private String endInfo;
}
StartEvent
@ToString(callSuper = true)
@Data
@JsonTypeName(value = "startEvent")
public class StartEvent extends BaseElement {
private String startEvent;
private String startInfo;
}
@Test
public void version2Json2Test() {
Reflections reflections = new Reflections("org.ctc.googol.jackson.version2");
Set<Class<? extends BaseElement>> classSet = reflections.getSubTypesOf(BaseElement.class);
ObjectMapper mapper = new ObjectMapper();
classSet.stream().forEach(clazz -> mapper.registerSubtypes(clazz));
String json = IoUtil.readFileAsString("json2.json");
try {
StartEvent startEvent = (StartEvent) mapper.readValue(json, BaseElement.class);
System.out.println(startEvent);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void version2Json1Test() {
Reflections reflections = new Reflections("org.ctc.googol.jackson.version2");
Set<Class<? extends BaseElement>> classSet = reflections.getSubTypesOf(BaseElement.class);
ObjectMapper mapper = new ObjectMapper();
classSet.stream().forEach(clazz -> mapper.registerSubtypes(clazz));
String json = IoUtil.readFileAsString("json1.json");
try {
List<BaseElement> result = mapper.readValue(json,
new TypeReference<List<BaseElement>>() {
});
result.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
具体Demo源码请见 https://github.com/googalAmbition/googol
Jackson 其他黑科技 https://github.com/FasterXML/jackson