日期:2014-05-16 浏览次数:20466 次
{"id":12,"name":"魏嘉留"}
String text = ...; // {"id":12,"name":"魏嘉留"} JSON.parseObject(text, Employee.class);
Employee x = ...; String text = JSON.toJSONString(x, SerializerFeature.WriteClassName);
{“@type”:"com.alibaba.demo.Employee", "id":12,"name":"魏嘉留"}
Employee x = (Employee) JSON.parse(text);
class A { B getB(); setB(B b); }
{"@type":"com.alibaba.json.demo.A","b":{"id":0}}
{"@type":"com.alibaba.json.demo.A","b":{"@type":"com.alibaba.json.demo.B","id":0}}
http://code.alibabatech.com/svn/fastjson/trunk/fastjson/src/test/java/com/alibaba/json/test/bvt/writeClassName/WriteClassNameTest_List2.java
public class Group { private Long id; private String name; private List<User> users = new ArrayList<User>(); private List<Class> clzes = new ArrayList<Class>(); /** setter && getter **/ }
public void serialize() { Group group = new Group(); group.setId(0L); group.setName("admin"); User guestUser = new User(); guestUser.setId(2L); guestUser.setName("guest"); User rootUser = new User(); rootUser.setId(3L); rootUser.setName("root"); group.getUsers().add(guestUser); group.getUsers().add(rootUser); group.getClzes().add(Integer.class); group.getClzes().add(int.class); String jsonString = JSON.toJSONString(group); System.out.println(jsonString); Group group2 = JSON.parseObject(jsonString, Group.class); }
com.alibaba.fastjson.JSONException: not support type : null at com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer.deserialze(DefaultObjectDeserializer.java:246) at Fastjson_ASM_Group_1.deserialze(Unknown Source) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:377) at com.alibaba.fastjson.JSON.parseObject(JSON.java:198) at com.alibaba.fastjson.JSON.parseObject(JSON.java:158) at com.alibaba.fastjson.JSON.parseObject(JSON.java:273) at org.netease.socketio.util.FastjsonTest.serialize(FastjsonTest.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAc