Using system; using system. collections. generic; using system. LINQ; using system. text; using newtonsoft. JSON; using newtonsoft. JSON. converters; namespace yy. szyd. shop. common. utils {public static class jsonhelper {Private Static jsonserializersettings _ jsonsettings; static jsonhelper () {isodatetimeconverter datetimeconverter = new isodatetimeconverter (); datetimeconverter. datetimeformat = "yyyy-mm-dd hh: mm: SS "; _ Jsonsettings = new jsonserializersettings (); _ jsonsettings. missingmemberhandling = newtonsoft. JSON. missingmemberhandling. ignore; _ jsonsettings. nullvaluehandling = newtonsoft. JSON. nullvaluehandling. ignore; _ jsonsettings. referenceloophandling = newtonsoft. JSON. referenceloophandling. ignore; _ jsonsettings. converters. add (datetimeconverter) ;}/// <summary> /// serialize the specified object to JSON data. /// </Summary> /// <Param name = "OBJ"> specifies the object to be serialized. </Param> // <returns> </returns> Public static string tojson (this object OBJ) {try {If (null = OBJ) return NULL; return jsonconvert. serializeobject (OBJ, formatting. none, _ jsonsettings);} catch (exception ex) {logging. logmanager. error (new logging. predictionloginfo () {predictionclassname = "YY. szyd. shop. common. utils. jsonhelper ", exceptionmethod =" tojson ", exceptionnote =" JSON serialization error ", requestinfo = obj. getType (). Fullname,}, ex); return NULL ;}/// <summary> // deserializes the specified JSON data into a specified object. /// </Summary> /// <typeparam name = "T"> object type. </Typeparam> // <Param name = "JSON"> JSON data. </Param> // <returns> </returns> Public static t fromjson <t> (this string JSON) {try {return jsonconvert. deserializeobject <t> (JSON, _ jsonsettings);} catch (exception ex) {logging. logmanager. error (new logging. predictionloginfo () {predictionclassname = "YY. szyd. shop. common. utils. jsonhelper ", exceptionmethod =" tojson ", exceptionnote =" JSON serialization error ", requestinfo = JSON,}, ex); Return default (t );}}}}
DLL download