JAVA平台下的一款,序列化反序列化工具,重点在于
1.可以直接把JSON反序列化为对象.
2.可反序列化深层复杂的对象.
3.可序列化循环引用等复杂对象.
4.可序列化静态类和内部类.
json-io canbeuseddirectlyonJSONStringsorwithJava'sStreams.Example1:StringtoJavaobject Objectobj=JsonReader.jsonToJava("[\"Hello,World\"]");ThiswillconverttheJSONStringtoaJavaObjectgraph.Inthiscase,itwouldconsistofan Object[] ofone String element.Example2:JavaobjecttoJSONString Employeeemp; //Empfetchedfromdatabase Stringjson=JsonWriter.objectToJson(emp);Thisexamplewillconvertthe Employee instancetoaJSONString.Ifthe JsonReader wereusedonthis String,itwouldreconstituteaJavaEmployee instance.Example3: InputStream toJavaobject JsonReaderjr=newJsonReader(inputStream); Employeeemp=(Employee)jr.readObject();Inthisexample,an InputStream (couldbefromaFile,theNetwork,etc.)issupplyinganunknownamountofJSON.The JsonReader isusedtowrapthestreamtoparseit,andreturntheJavaobjectgraphitrepresents.Example4:JavaObjectto OutputStream Employeeemp; //empobtainedfromdatabase JsonWriterjw=newJsonWriter(outputStream); jw.write(emp); jw.close();
评论