C#json Serialization and deserialization

Source: Internet
Author: User
Tags add time

1, dynamic determine whether the data to be serialized

My needs are like this, I used a database of components called Dos.orm, it is really convenient, but in use, I found a problem, such as my definition of the table has a field to add time, modify the time, these are the default, when added, do not need to assign value, but I passed from the front, it will be a datetime.minvalue. I don't want to change it. But when I query the fields from the database, I want to get the data. Therefore, it is not possible to simply add a serialized property to the model.

Here's my Jsonhelper class.

usingHelp.DataService.Business.Common;usingNewtonsoft.json;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacehelp.dataservice.business{ Public Static classJsonhelper { Public Static stringSerializeObject (Objectparam) {            if(param! =NULL)            {                returnjsonconvert.serializeobject (param); }            return string.        Empty; }         Public StaticT deserializeobject<t> (stringJsonwhereT:class        {            if(!string. IsNullOrEmpty (JSON)) {returnJsonconvert.deserializeobject<t>(JSON); }            return default(T); }         Public StaticRESULT Parsemodel<input, result>(INPUT data)whereINPUT:class            whereRESULT:class        {            if(Data = =NULL)            {                return NULL; }            varJsetting =Newjsonserializersettings {nullvaluehandling =Nullvaluehandling.ignore}; string[] Props = {"Modifytime", "Addtime"  }; Jsetting.contractresolver=NewLimitpropscontractresolver (Props,false); stringJSON =jsonconvert.serializeobject (data, jsetting); if(!string. IsNullOrEmpty (JSON)) {//null and default, there is no need to deserialize, dos.orm component if DateTime is 0001-01-01, operation database will not succeed                returnJsonconvert.deserializeobject<result>(JSON, jsetting); }            return default(RESULT); }         Public StaticR Parseviewmodel<i, r>(I data)whereI:class            whereR:class        {            stringJSON =serializeobject (data); returnDeserializeobject<r>(JSON); }    }}
The code for the Limitpropscontractresolver class is as follows:

usingNewtonsoft.Json.Serialization;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacehelp.dataservice.business.common{/// <summary>    ///dynamically determines whether a property is serialized/// </summary>     Public classLimitpropscontractresolver:defaultcontractresolver {/// <summary>        ///Property List/// </summary>        Private string[] props =NULL; /// <summary>        ///whether it contains/// </summary>        Private BOOLretain; /// <summary>        ///constructor Function/// </summary>        /// <param name= "props" >Props</param>        /// <param name= "Isretain" >Isretain (included or excluded)</param>         PublicLimitpropscontractresolver (string[] Props,BOOLIsretain =true)        {             This. props =props;  This. retain =Isretain; }        protected OverrideIlist<jsonproperty>createproperties (Type type, Newtonsoft.Json.MemberSerialization memberserialization) {IList<JsonProperty> list =Base.            Createproperties (type, memberserialization); if(Props = =NULL|| Props. Length = =0)            {                returnlist; }            //List only properties that are reserved            varret = list. Where (p =            {                if(retain) {return  This. Props.                Contains (P.propertyname); }                Else                {                    return! This. Props.                Contains (P.propertyname); }            }).            ToList (); returnret; }    }}
2. Use C # to tune Java services.

The result of the error is that the data is not found, do not know, later debugging Java code Discovery, but also the problem of deserialization. Because the code in C # is capitalized in the first letter, the Java code is the first lowercase letter, so that after deserialization, Java does not get any parameters at all, although C # has passed. Without changing the Java code, I can only change the C # code. Fortunately there are ready-made methods, but not very common, as follows:

Newnew= cal; string json = Jsonconvert.serializeobject (VO, setting);
3. Related Information

Newtonsoft.json Advanced Usage

C#json Serialization and deserialization

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.