Json. NET, fastJSON, ServiceStack. Text simple serialization operation performance test, json. netfastjson
As JSON serialization operations are required in recent projects, we have compared the JSON serialization framework.
Json. NET (Newtonsoft. json) is the most commonly used framework. It is now one of the default reference libraries for Visual Studio Web project templates. fastJSON claims to be the smallest and fastest serialization tool; ServiceStack. text is an open-source project of the famous Stack Exchange team. Let's start the test:
Test environment: Operating System Windows 8.1, CPU: Intel Core i7-3610QM, Visual Studio 2013.
Create a console application and add the reference of the latest three libraries from NuGet.
Create a serializable type:
1 [Serializable] 2 public class Person { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public int Age { get; set; } 6 public bool Gender { get; set; } 7 public string Address { get; set; } 8 public string IDCard { get; set; } 9 }
Add a public method to execute the serialization operation cyclically:
1 /// <summary> 2 /// perform the serialization operation cyclically 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5 /// <param name = "entity"> entity that requires serialization </param> 6 // <param name = "testName"> name of the database </param> 7 // <param name = "count"> cyclic test </param> 8 /// <param name = "func"> delegate the serialization method </param> 9 private static void PerformanceTest <T> (T entity, string testName, int count, Action <T> func) where T: class {10 var stopwatch = new Stopwatch (); 11 stopwatch. start (); 12 for (var I = 0; I <count; I ++) {13 func. invoke (entity); 14} 15 stopwatch. stop (); 16 Console. writeLine ("{0 }:{ 1}", testName, stopwatch. elapsedMilliseconds); 17}
Next we can call it in the Main method:
1 static void Main () {2 var person = new Person {3 Id = 1, 4 Name = "Vision Ding", 5 Age = 28, 6 Gender = true, 7 Address = "Tianhe District, Guangzhou City, Guangdong Province", 8 IDCard = "42XXXX198XXXXXXXXX" 9}; 10 for (var I = 0; I <10; I ++) {11 Console. writeLine ("{0} execution:", I + 1); 12 PerformanceTest (person, "Json. NET ", 100000, p => JsonConvert. serializeObject (p); 13 PerformanceTest (person, "fastJSON", 100000, p => JSON. toJSON (p); 14 PerformanceTest (person, "ServiceStack", 100000, p => p. toJson (); 15} 16}
We cyclically run the command 10 times and the result is as follows (unit: milliseconds ):
|
Json. NET |
FastJSON |
ServiceStack. Text |
1 |
480 |
342 |
406 |
2 |
218 |
308 |
251 |
3 |
216 |
307 |
235 |
4 |
219 |
326 |
235 |
5 |
220 |
310 |
251 |
6 |
215 |
308 |
232 |
7 |
217 |
309 |
233 |
8 |
216 |
305 |
233 |
9 |
216 |
306 |
233 |
10 |
218 |
306 |
269 |
From the preceding execution results, fastJSON is the fastest and Json. NET is the slowest during the first execution, but the last nine times are the opposite. Since each loop is executed for 10 million serialized operations, the difference is only several dozen milliseconds, so this performance gap is almost negligible, and Json. NET is very reliable.
The official website of the above three class libraries:
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 ~
How does fastjson read and write json files?
Publicstaticfinal <TTparseObject (Stringtext, Class <Tclazz); // set the JSON Text parse to JavaBean
PublicstaticfinalJSONArrayparseArray (Stringtext); // convert the JSON Text parse into a JSONArray
Publicstaticfinal <TList <TparseArray (Stringtext, Class <Tclazz); // converts the JSON Text parse into a JavaBean set.
PublicstaticfinalStringtoJSONString (Objectobject); // serialize A JavaBean to JSON text
PublicstaticfinalStringtoJSONString (Objectobject, booleanprettyFormat); // serialize A JavaBean to a JSON text with a format
PublicstaticfinalObjecttoJSON (ObjectjavaObject); Convert JavaBean to JSONObject or JSONArray.
You can try the online search api. It is always very powerful in fastJSON!