Android lightweight JSON operations

Source: Internet
Author: User
Package COM. google. test; import Java. lang. reflect. array; import Java. lang. reflect. field; import Java. lang. reflect. parameterizedtype; import Java. lang. reflect. type; import Java. util. collection; import Org. JSON. jsonarray; import Org. JSON. jsonexception; import Org. JSON. jsonobject; import Org. JSON. jsonstringer;/** JSON serialization helper class **/public class jsonhelper {/** converts an object to a JSON string **/public static string tojson (Object OBJ) {Jsonstringer JS = new jsonstringer (); serialize (JS, OBJ); Return Js. tostring ();}/** serialized as JSON **/Private Static void serialize (jsonstringer JS, object O) {If (isnull (O) {try {Js. value (null);} catch (jsonexception e) {e. printstacktrace ();} return;} class <?> Clazz = O. getclass (); If (isobject (clazz) {// object serializeobject (JS, O);} else if (isarray (clazz) {// array serializearray (JS, o);} else if (iscollection (clazz) {// collection <?> Collection = (collection <?>) O; serializecollect (JS, collection);} else {// a single value try {Js. value (o);} catch (jsonexception e) {e. printstacktrace () ;}}/ ** serialized array **/Private Static void serializearray (jsonstringer JS, object array) {try {Js. array (); For (INT I = 0; I <array. getlength (array); ++ I) {object o = array. get (array, I); serialize (JS, O);} Js. endarray ();} catch (exception e) {e. printstacktrace () ;}/ ** serialization set **/Private Static v Oid serializecollect (jsonstringer JS, collection <?> Collection) {try {Js. array (); For (Object O: Collection) {serialize (JS, O);} Js. endarray ();} catch (exception e) {e. printstacktrace () ;}/ ***** serialized object *****/Private Static void serializeobject (jsonstringer JS, object OBJ) {try {Js. object (); For (field F: obj. getclass (). getfields () {object o = f. get (OBJ); Js. key (F. getname (); serialize (JS, O);} Js. endobject ();} catch (exception e) {e. printstacktrace ();} }/*** Deserialization of a simple object ** @ throws **/public static <t> T parseobject (jsonobject Jo, class <t> clazz) {If (clazz = NULL | isnull (Jo) {return NULL;} t OBJ = createinstance (clazz); If (OBJ = NULL) {return NULL ;} for (field F: clazz. getfields () {setfield (OBJ, F, Jo);} return OBJ ;} /*** deserialize a simple object ** @ throws **/public static <t> T parseobject (string jsonstring, class <t> clazz) {If (clazz = NULL | jsonstrin G = NULL | jsonstring. length () = 0) {return NULL;} jsonobject Jo = NULL; try {Jo = new jsonobject (jsonstring);} catch (jsonexception e) {e. printstacktrace ();} If (isnull (Jo) {return NULL;} return parseobject (Jo, clazz );} /*** deserialization array object ** @ throws **/public static <t> T [] parsearray (jsonarray Ja, class <t> clazz) {If (clazz = NULL | isnull (JA) {return NULL;} int Len = JA. length (); @ suppresswarnings ("Unchecked") T [] array = (T []) array. newinstance (clazz, Len); For (INT I = 0; I <Len; ++ I) {try {jsonobject Jo = JA. getjsonobject (I); t o = parseobject (Jo, clazz); array [I] = O;} catch (jsonexception e) {e. printstacktrace () ;}} return array;}/*** deserialization array object ** @ throws **/public static <t> T [] parsearray (string jsonstring, class <t> clazz) {If (clazz = NULL | jsonstring. length () = 0) {return NULL;} jsonarray Jo = NULL; try {Jo = new jsonarray (jsonstring);} catch (jsonexception e) {e. printstacktrace ();} If (isnull (Jo) {return NULL;} return parsearray (Jo, clazz );} /*** deserialization generic set ** @ throws **/@ suppresswarnings ("unchecked") public static <t> collection <t> parsecollection (jsonarray Ja, class <?> Collectionclazz, class <t> generictype) {If (collectionclazz = NULL | generictype = NULL | isnull (JA) {return NULL ;} collection <t> collection = (collection <t>) createinstance (collectionclazz); For (INT I = 0; I <Ja. length (); ++ I) {try {jsonobject Jo = JA. getjsonobject (I); t o = parseobject (Jo, generictype); Collection. add (o);} catch (jsonexception e) {e. printstacktrace () ;}} return collection ;}/*** Deserialization of generic collections ** @ throws **/public static <t> collection <t> parsecollection (string jsonstring, class <?> Collectionclazz, class <t> generictype) {If (collectionclazz = NULL | generictype = NULL | jsonstring. length () = 0) {return NULL;} jsonarray Jo = NULL; try {Jo = new jsonarray (jsonstring);} catch (jsonexception e) {e. printstacktrace ();} If (isnull (Jo) {return NULL;} return parsecollection (Jo, collectionclazz, generictype );} /** create object based on type **/Private Static <t> T createinstance (Cl Ass <t> clazz) {If (clazz = NULL) return NULL; t OBJ = NULL; try {OBJ = clazz. newinstance ();} catch (exception e) {e. printstacktrace ();} return OBJ;}/** set the field value **/Private Static void setfield (Object OBJ, field F, jsonobject Jo) {string name = f. getname (); Class <?> Clazz = f. GetType (); try {If (isarray (clazz) {// array class <?> C = clazz. getcomponenttype (); jsonarray ja = Jo. optjsonarray (name); If (! Isnull (JA) {object array = parsearray (JA, c); F. set (OBJ, array) ;}} else if (iscollection (clazz) {// generic set // obtain the defined generic class <?> C = NULL; Type gtype = f. getgenerictype (); If (gtype instanceof parameterizedtype) {parameterizedtype ptype = (parameterizedtype) gtype; Type [] targs = ptype. getactualtypearguments (); If (targs! = NULL & targs. length> 0) {type T = targs [0]; C = (class <?>) T ;}} jsonarray ja = Jo. optjsonarray (name); If (! Isnull (JA) {object o = parsecollection (JA, clazz, c); F. set (OBJ, O) ;}} else if (issingle (clazz) {// value type object o = Jo. opt (name); If (o! = NULL) {f. Set (OBJ, O) ;}} else if (isobject (clazz) {// object jsonobject J = Jo. optjsonobject (name); If (! Isnull (j) {object o = parseobject (J, clazz); F. Set (OBJ, O) ;}} else {Throw new exception ("unknow type! ") ;}} Catch (exception e) {e. printstacktrace () ;}/ ** determines whether the object is empty **/Private Static Boolean isnull (Object OBJ) {If (OBJ instanceof jsonobject) {return jsonobject. null. equals (OBJ);} return OBJ = NULL;}/** determine whether the value type is **/Private Static Boolean issingle (class <?> Clazz) {return isboolean (clazz) | isnumber (clazz) | isstring (clazz);}/** Boolean value **/public static Boolean isboolean (class <?> Clazz) {return (clazz! = NULL) & (Boolean. type. isassignablefrom (clazz) | (Boolean. class. isassignablefrom (clazz);}/** is it a numerical value **/public static Boolean isnumber (class <?> Clazz) {return (clazz! = NULL) & (byte. type. isassignablefrom (clazz) | (short. type. isassignablefrom (clazz) | (integer. type. isassignablefrom (clazz) | (Long. type. isassignablefrom (clazz) | (float. type. isassignablefrom (clazz) | (double. type. isassignablefrom (clazz) | (number. class. isassignablefrom (clazz);}/** determine whether it is a string **/public static Boolean isstring (class <?> Clazz) {return (clazz! = NULL) & (string. class. isassignablefrom (clazz) | (character. type. isassignablefrom (clazz) | (character. class. isassignablefrom (clazz);}/** determine whether the object is **/Private Static Boolean isobject (class <?> Clazz) {return clazz! = NULL &&! Issingle (clazz )&&! Isarray (clazz )&&! Iscollection (clazz);}/** determine whether it is an array **/public static Boolean isarray (class <?> Clazz) {return clazz! = NULL & clazz. isarray ();}/** determine whether it is a set **/public static Boolean iscollection (class <?> Clazz) {return clazz! = NULL & collection. Class. isassignablefrom (clazz );}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.