Json.net dynamic serialization and processing of time formats, json.net serialization

Source: Internet
Author: User

Json.net dynamic serialization and processing of time formats, json.net serialization

About Json processing in my work

1. Dynamic serialization

Second: Time Format Processing

Generally, a class may have 10 to more attributes, but we only need to serialize three to five of them so that there will be extra data.

 

 

 

What if I only want to serialize id and name?

This is my online method:

Using Newtonsoft. json; using Newtonsoft. json. converters; using Newtonsoft. json. serialization; 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 sorted return list listed in the list is retained. where (p => props. contains (p. propertyName )). toList ();}}}

 

 

 

 

Certificate certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Looking at the first figure of Json.net processing date format, we can see that Json.net processing date format. Nima is not a normal human can understand it.

 

You may find that Json.net contains something about the processing date.

 

 

 

However, at this time, you will find the Json.net reload

     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 formats of dynamic serialization and time processing cannot coexist. This problem really makes my little headache .....

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[] 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);            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 Json.net Processing

With a helpful attitude

About Josn.net http://www.codeplex.com/------------- Make by night, light flowers


What is JSON serialization? I learned json under net. What is serialization? What is the role?

The json string itself is organized in a certain format, just like the tag pairs used in xml, json needs to use:
"Key": "value"; format to organize;
In this way, it becomes a string sequence. The advantage of serialization is that Parsing is convenient and easy to understand. For example, javascript can directly convert json into an object using eval to call the value Attribute using the key;
I believe. net also has functions or classes for processing json strings. You can find them.

When JSONNET serializes an object as a JSON string, it can be specified. An attribute is not converted.

Mark the property under serialization:
[JsonProperty (Name = "password")]
Public string Password {get; set ;}

You can try it out without marking it ~

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.