Jsonhelper class (C # conversion between objects and JSON)

Source: Internet
Author: User

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

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.