Newtonsoft. Json JsonHelper, newtonsoft. json

Source: Internet
Author: User
Tags list of attributes

Newtonsoft. Json JsonHelper, newtonsoft. json

Json.net simple Encapsulation

1 using System; 2 using System. linq; 3 using Newtonsoft. json; 4 using Newtonsoft. json. serialization; 5 using System. collections. generic; 6 7 namespace TT. utilities. json 8 {9 public static class JsonHelper 10 {11 // <summary> 12 // only the field required by the serial number 13 /// </summary> 14 // <param name = "obj"> </param> 15 // <param name = "arrFiled"> </param> 16 /// <returns> </returns> 17 public static string GetPartModelJs On (object obj, string [] arrFiled) 18 {19 JsonSerializerSettings jsetting = new JsonSerializerSettings (); 20 jsetting. contractResolver = new LimitPropsContractResolver (arrFiled); 21 return JsonConvert. serializeObject (obj, Formatting. indented, jsetting ); 22} 23 24 // <summary> 25 // convert to json format 26 // </summary> 27 // <param name = "obj"> Object </param> 28 // <returns> </returns> 29 public static string GetJson (object obj) 30 {31 return JsonConvert. serializeObject (obj ); 32} 33 34 // <summary> 35 // string in json format is converted to a T-type object 36 /// </summary> 37 /// <typeparam name = "T "> type </typeparam> 38 // <param name =" json "> json string </param> 39 /// <returns> </returns> 40 public static T getModel <T> (string json) 41 {42 return JsonConvert. deserializeObject <T> (json); 43} 44 45 // <summary> 46 // deserialize the object to json attributes 47 /// </Summary> 48 // <param name = "json"> json string </param> 49 // <returns> </returns> 50 public static Newtonsoft. json. linq. JProperty DeserialJson (string json) 51 {52 return JsonConvert. deserializeObject <Newtonsoft. json. linq. JProperty> (json); 53} 54 55 // <summary> 56 // deserialize JSON to the specified anonymous object. 57 // </summary> 58 // <typeparam name = "T"> anonymous object type </typeparam> 59 // <param name = "json"> json string </param> 60 /// <Param name = "anonymousTypeObject"> anonymous object </param> 61 // call: var objClass = JsonClass. deserializeAnonymousType (obj. data. toString (), nClass [anonymous object]); 62 // <returns> anonymous object </returns> 63 public static T DeserializeAnonymousType <T> (string json, T anonymousTypeObject) 64 {65 T = JsonConvert. deserializeAnonymousType (json, anonymousTypeObject); 66 return t; 67} 68 69 public class LimitPropsContractResolver: DefaultContractResolver 70 {71 string [] props = null; 72 73 bool retain; 74 75 // <summary> 76 // constructor 77 // </summary> 78 // <param name = "props"> INPUT attribute array </ param> 79 // <param name = "retain"> true: false indicates that props is the field to be retained: indicates that props is the field to be excluded </param> 80 public LimitPropsContractResolver (string [] props, bool retain = true) 81 {82 // list of attributes to be serialized 83 this. props = props; 84 85 this. retain = retain; 86} 87 88 protected override IList <JsonProperty> CreateProperties (Type type, 89 90 MemberSerialization memberSerialization) 91 {92 IList <JsonProperty> list = 93 base. createProperties (type, memberSerialization); 94 // only the 95 return list attributes listed in the list are retained. where (p => 96 {97 if (retain) 98 {99 return props. contains (p. propertyName); 100} 101 else102 {103 return! Props. Contains (p. PropertyName); 104} 105}). ToList (); 106} 107} 108}View Code

 

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.