序列化的时候使用SerializerFeature.WriteClassName, 类上使用了@JSONType, 能成功序列化, 但是反序列化时失败, 使用版本为1.2.60 ;
异常信息为:
Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error
无法识别"@type"
示例如下:
` public class TestJSon {
@Data
@JSONType(typeName = "BigDog")
static class Dog {
private Map<String, Food> foodMap;
}
@Data
@JSONType(typeName = "BigFood")
static class Food {
private String name;
}
@Data
@JSONType(typeName = "iPhone")
static class Apple extends Food {
private String colour;
}
public static void main(String[] args){
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
TestJSon.Dog dog = new TestJSon.Dog();
Map<String, TestJSon.Food> foodMap = new HashMap<>();
TestJSon.Apple a = new TestJSon.Apple();
a.setColour("red");
foodMap.put("a",a);
dog.setFoodMap(foodMap);
String jsonString = JSON.toJSONString(dog, SerializerFeature.WriteClassName);
System.out.println(jsonString);
System.out.println(JSON.parseObject(jsonString,Dog.class));
}
} `