Dynamic serialization of reprinted self---json.net and processing of time format

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:

Using newtonsoft.json;using newtonsoft.json.converters;using newtonsoft.json.serialization;using System;using System.collections.generic;using system.linq;namespace CYP. new.wcf.common.common{public    class Limitpropscontractresolver:defaultcontractresolver    {        private string[] props = null;        Public Limitpropscontractresolver (string[] props)        {            this.props = props;        }        protected override ilist<jsonproperty> Createproperties (type type, memberserialization memberserialization)        {            ilist<jsonproperty> list = base. Createproperties (type, memberserialization);            Only the list of the listed            return list is reserved. 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 string SerializeObject (object value);        public static string SerializeObject (object value, formatting formatting);        public static string SerializeObject (object value, jsonserializersettings settings);        public static string SerializeObject (object value, params jsonconverter[] converters);        public static string SerializeObject (object value, formatting formatting, jsonserializersettings settings);        public static string SerializeObject (object value, formatting formatting, params jsonconverter[] 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:

Using newtonsoft.json;using newtonsoft.json.converters;using newtonsoft.json.serialization;using System;using System.collections.generic;using system.linq;namespace CYP. new.wcf.common.common{public class Limitpropscontractresolver:defaultcontractresolver {private string[] P        Rops = null;        Public Limitpropscontractresolver (string[] props) {this.props = props;         } protected override ilist<jsonproperty> Createproperties (type type, memberserialization memberserialization) {ilist<jsonproperty> list = base.            Createproperties (type, memberserialization);            Isodatetimeconverter ISO = new Isodatetimeconverter () {DateTimeFormat = "yyyy-mm-dd HH:mm:ss"};            ilist<jsonproperty> listwithconver = new list<jsonproperty> (); foreach (var item in list) {if (props. Contains (item. PropertyName)) {if (item). Propertytype.tostring(). Contains ("System.DateTime")) {item.                    Converter = ISO;                } listwithconver.add (item);        }} return listwithconver; }    }}

Some handling of json.net

In a manner that is helpful to all

About the http://www.codeplex.com/of josn.net

Reprinted from: http://www.cnblogs.com/yesehuaqingqian/p/3934532.html

Dynamic serialization of reprinted self---json.net and processing of time format

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.