This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
JSONType_seeAlso_cn
wenshao edited this page May 12, 2016
·
2 revisions
在fastjson-1.2.11版本中,@JSONType支持seeAlso配置,类似于JAXB中的XmlSeeAlso。用法如下:
@JSONType(seeAlso={Dog.class, Cat.class})
public static class Animal {
}
@JSONType(typeName = "dog")
public static class Dog extends Animal {
public String dogName;
}
@JSONType(typeName = "cat")
public static class Cat extends Animal {
public String catName;
}
Dog dog = new Dog();
dog.dogName = "dog1001";
String text = JSON.toJSONString(dog, SerializerFeature.WriteClassName);
Assert.assertEquals("{\"@type\":\"dog\",\"dogName\":\"dog1001\"}", text);
Dog dog2 = (Dog) JSON.parseObject(text, Animal.class);
Assert.assertEquals(dog.dogName, dog2.dogName);
Cat cat = new Cat();
cat.catName = "cat2001";
String text = JSON.toJSONString(cat, SerializerFeature.WriteClassName);
Assert.assertEquals("{\"@type\":\"cat\",\"catName\":\"cat2001\"}", text);
Cat cat2 = (Cat) JSON.parseObject(text, Animal.class);
Assert.assertEquals(cat.catName, cat2.catName);
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner