Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

General encapsulation based on Json serialization and deserialization

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Recently, the project has been launched, and after a few days of leisure, I want to repackage the serialization and deserialization of JSON and define it as JSONHelp, although Microsoft has done a good job. But I want to encapsulate a set of projects developed for my own use. It is convenient for later expansion and development.

two。 What is JSON?

JSON:JavaScript object representation (JavaScript Object Notation). JSON is the syntax for storing and exchanging text information. Similar to XML. JSON is smaller, faster and easier to parse than XML. Now it is essential to develop Web applications called JSON. JSON is a lightweight text data exchange case, JSON is language-independent, JSON is self-descriptive and easier to understand, JSON uses JavaScript syntax to describe data objects, but JSON is still independent of language and platform. JSON parsers and JSON libraries support many different programming languages.

3.JSON-convert to JavaScript object

The JSON text format is syntactically the same as the code that creates the JavaScript object. Because of this similarity, no parser is needed, and JavaScript programs can use the built-in eval () function to generate native JavaScript objects with JSON data.

4. Similar to XML

JSON is plain text JSON has the characteristic of self-description, JSON has a hierarchical structure (there are values in values) JSON can be parsed through JavaScript JSON data can be transmitted using AJAX compared to the difference of XML there is no closing tag shorter reading and writing speed can be parsed using the built-in, JavaScript eval () method, using arrays, without reserved words

5. Why use JSON?

For AJAX applications, JSON is faster and easier to use than XML: use XML to read XML documents, use XML DOM to loop through document read values and store them in variables

6. Use JSON

Read JSON strings use eval () to handle JSON string front-end JS interface We usually use JSON.parse () to serialize objects or strings into JSON and JSON.stringify () to serialize them into strings.

7. Serialization and deserialization of JSON

Serialization and deserialization of JSON I use JsonConvert.SerializeObject () in using Newtonsoft.Json and Jil.JSON.Serialize () in using System.Runtime.Serialization.Json;

Of course, the two serialization methods are the same. But Jil.JSON.Serialize () serialization is really much more efficient than this JsonConvert.SerializeObject (). It is obvious when a large amount of data is serialized. So

I strongly recommend using Jil.JSON.Serialize () to serialize and deserialize objects when the amount of serialization and deserialization data is very large and large.

The 9.JSONHelp.cs code is as follows:

1 using Newtonsoft.Json; 2 using System; 3 using System.IO; 4 using System.Runtime.Serialization.Json; 5 using System.Text 6 / * 7 ◇ author: LowKeyC 8 ◇ description: serialization and deserialization of JSON in General Management 9 ◇ version number: V1.0 10 ◇ creation date: Thursday, June 22, 2017 11 * * * / 12 namespace RapidDevelopmentFramework.Common 13 {14 / 15 / JSON Auxiliary Class 16 / 17 public static class JSONHelper 18 {19 20 / / 21 / normal version 22 of JSON serialization / / 23 / 24 / 25 / / 26 public static String ObjectToJSON (this T _ Object) where T: class 27 {28 return JsonConvert.SerializeObject (_ Object) 29} 30 31 / / 32 / / Standard version of JSON serialization 33 / / 34 / / 35 / / 36 public static String ObjectToJSON (this Object _ Object) 37 {38 return JsonConvert.SerializeObject (_ Object) 39} 40 41 42 / / 43 / / official version of JSON serialization 44 / / 45 / / 46 / / 47 / / 48 public static String ObjectToJSONOfficial (this T _ Object) where T: class 49 {50 using (var TempMemoryStream = new MemoryStream ()) 51 { 52 var MyDataContractJsonSerializer = new DataContractJsonSerializer (typeof (T)) 53 MyDataContractJsonSerializer.WriteObject (TempMemoryStream, _ Object); 54 var MySerializationString = Encoding.UTF8.GetString (TempMemoryStream.ToArray ()); 55 return MySerializationString 56} 57} 58 59 60 / 61 / / JSON serialized Jil version 62 / / 63 / 64 / / 65 / / 66 public static String ObjectToJSONJil (this T _ Object) where T: class 67 {68 return Jil.JSON.Serialize (_ Object, Jil.Options.ExcludeNullsIncludeInherited) 69} 70 71 72 / / 73 / / Jil version of JSON serialization can contain null 74 / / 75 / / 76 / / 77 / / 78 public static String ObjectToJSONJilIncludeNulls (this T _ Object) where T:class 79 {80 return Jil.JSON.Serialize (_ Object, Jil.Options.IncludeInherited) 81} 82 83 84 / / 85 / JSON deserialization 86 / / 87 / / 88 / / 89 / / 90 public static T JSONToObjectJSON (this String _ JSONString) where T:class 91 {92 return JsonConvert.DeserializeObject (_ JSONString) 93} 94 95 96 / / 97 / / official version of JSON deserialization 98 / / 99 / / 100 / / 101 / / 102public static T JSONToObjectOfficial (this String _ JSONString) where T: class103 {104 using (var TempMemoryStream = new MemoryStream (Encoding.UTF8.GetBytes (_) JSONString)) 105 {106 var MyDataContractJsonSerializer = new DataContractJsonSerializer (typeof (T)) 107 return (T) MyDataContractJsonSerializer.ReadObject (TempMemoryStream) Public static T JSONToObjectJil (this String _ JSONString) where T: class119 {120 using (var InputString = new StringReader (_) JSONString)) 121 {122 var ObjectResult = Jil.JSON.Deserialize (InputString) 123 return ObjectResult 126127128 / / 129129 / JSON deserialized Jil version which can contain null 130 / / 131131/ / 132 / / 133C / / 134public static T JSONToObjectJilIncludeNulls (this String _ JSONString) where T: class135 {136 using (var) InputString = new StringReader (_ JSONString) 137 {138 var ObjectResult = Jil.JSON.Deserialize (InputString) Jil.Options.IncludeInherited) 139 return ObjectResult;140} 141} 142 143} 144}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report