There was an error while loading. Please reload this page.
可以通过Annotation JSONType支持seeAlso配置。用法如下:
@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);