Json.NET dynamic serialization and processing of time formats

Source: Internet
Author: User

about what I do with JSON in my work.

First: Dynamic serialization classes

Second: Time format processing

Usually we have 10 or more properties in a class, but we usually only need to serialize three to five of them so there will be extra data.

If I just want to serialize ID with name how to handle

This is the method I am looking for online:

usingNewtonsoft.json;usingNewtonsoft.Json.Converters;usingNewtonsoft.Json.Serialization;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;namespacecyp.new.wcf.common.common{ Public classLimitpropscontractresolver:defaultcontractresolver {Private string[] props =NULL;  PublicLimitpropscontractresolver (string[] props) {             This. props =props; }        protected OverrideIlist<jsonproperty>createproperties (Type type, Memberserialization memberserialization) {IList<JsonProperty> list =Base.            Createproperties (type, memberserialization); //only the list of the listed sex is retained            returnList. Where (p =props. Contains (P.propertyname)).        ToList (); }    }}

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ----------------------------------------

About json.net processing date format look at the first picture. Json.NET Processing Date format This is not what normal human beings can see.

You might find that json.net has something to do with the date.

But this time you'll find that the json.net of the heavy load inside

      Public Static stringSerializeObject (Objectvalue);  Public Static stringSerializeObject (Objectvalue, formatting formatting);  Public Static stringSerializeObject (Objectvalue, jsonserializersettings settings);  Public Static stringSerializeObject (ObjectValueparamsjsonconverter[] converters);  Public Static stringSerializeObject (Objectvalue, formatting formatting, jsonserializersettings settings);  Public Static stringSerializeObject (ObjectValue, formatting formatting,paramsJsonconverter[] converters);

You will find that the dynamic serialization and the format of the time processing cannot coexist this problem really makes my little egg ache a bit ....

Solution:

usingNewtonsoft.json;usingNewtonsoft.Json.Converters;usingNewtonsoft.Json.Serialization;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;namespacecyp.new.wcf.common.common{ Public classLimitpropscontractresolver:defaultcontractresolver {Private string[] props =NULL;  PublicLimitpropscontractresolver (string[] props) {             This. props =props; }        protected OverrideIlist<jsonproperty>createproperties (Type type, Memberserialization memberserialization) {IList<JsonProperty> list =Base.            Createproperties (type, memberserialization); Isodatetimeconverter ISO=NewIsodatetimeconverter () {DateTimeFormat ="YYYY-MM-DD HH:mm:ss" }; IList<JsonProperty> Listwithconver =NewList<jsonproperty>(); foreach(varIteminchlist) {                if(props. Contains (item. PropertyName)) {if(item. Propertytype.tostring (). Contains ("System.DateTime") {item. Converter=ISO;                } listwithconver.add (item); }            }            returnListwithconver; }    }}

Some handling of json.net

In a manner that is helpful to all

About Josn.net's http://www.codeplex.com/                                                                                                   -------------make  by  night, flowers clear light

Json.NET dynamic serialization and processing of time formats

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.