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

Example Analysis of Swifter.Json Serialization Library on .net platform

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Net platform Swifter.Json serialization library [open source] example analysis, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Introduction to Json

Json (JavaScript Object Notation) is a lightweight data exchange format. As the most popular data exchange format at present, it is also a must for the major open source contributors, such as Ali's father's fastjson (java), Tencent's rapidjson (C++) and so on. But .net was not favored by big manufacturers. Before Swifter.Json, the Json parsing library of .net was not perfect.

Introduction to Swifter.Json

Swifter.Json is a powerful, easy-to-use, stable and high-performance Json serialization and deserialization tool on the .net platform.

Github: https://github.com/Dogwei/Swifter.Json

Ma Yun: https://gitee.com/eway0/Swifter

The strength of Swifter.Json

1: supports all known object structures of .net, including deep loop structure, reference type properties, etc.

2: support almost all commonly used data types, and multiple solutions can easily customize the serialization behavior of types, which will continue to increase in the future.

3: support indentation of Json, ignore Null value 0, value "", etc., support using {"$Ref": "# / Target"} to indicate duplicate references, or serialize circular references to Null, etc., and set the maximum depth.

4: supports .Net Core 2.0memorial.net Framework 2.0memorial.net Standard 2.0recording recordXamarinLaw UnifymemeMono and other platforms.

5:Swifter.Json is almost BUG-free, because it already has several online projects running, and each release will go through our own unit tests and Newtonsoft.Json and Spanjson unit tests to ensure its stability, if you encounter problems, you can release an issue on Github, we will try our best to help you.

6: complete and comprehensive API Chinese documentation, each public type and method has a Chinese description, as well as Wiki documents on Github (now mainly maintenance documents).

Why repeat the wheel?

At present, the Json libraries on the .net platform have some obvious shortcomings: some are easy to use, but the performance is not high, some are high but unstable, and the stable ones are low performance and too complex.

We urgently need a powerful, stable, high-performance and easy-to-use Json serialization tool!

So Swifter.Json was made.

Library reference of Swifter.Json

Swifter.Core: this is a toolkit that can be of great help to class library developers, and Swifter.Json is based on it. It provides efficient data reading and writing API, efficient mathematical algorithms and. Net CLR numerous data information, as well as to solve the differences between the versions of different platforms, and so on. Later, we will develop a Swifter.MessagePack based on this library (already developed, to be released). This library 300~400KB (varies from platform to platform).

Swifter.Unsafe: this is a library written by IL code. Because the operation provided by System.Runtime.ComplierServices.Unsafe is similar to that provided by System.Runtime.ComplierServices.Unsafe, it is named Swifter.Unsafe, which is a big 5KB.

Swifter.Json: IValueReader that implements Swifter.Json, IValueWriter implements serialization and deserialization of arbitrary objects. The main code is in JsonSerializer (implementing IValueWriter) and JsonDeserializer (implementing IValueReader). JsonFormatter provides API overloading and now supports full asynchronous API. This library is about 86KB.

Performance of Swifter.Json

Test results for .net Core 3.0 Preview 7

Compared with Newtonsoft.Json, it is about 5 to 10 times higher. Although there are many Json libraries that claim high performance, the only one I agree with except Swifter.Json is Spanjson, but its disadvantages are too obvious, it only supports .net Core 2.1 + and is not stable. If you don't want to use Swifter.Json, I suggest you use Newtonsoft.Json. After all, it has all the advantages except performance.

There is another feature that is closely related to performance: small allocation. As the name implies, the amount of memory allocated, for example: when a program is deserializing an array, most Json tools repeatedly create larger arrays in the process, which is time-consuming and resource-consuming, because the length of the array is unknown. Swifter.Json has solved this problem perfectly with pooling technology. Spanjson also uses the ArrayPool pool on Core, so I clicked a little star for Spanjson.

After reading the above, have you mastered the method of sample analysis of the Swifter.Json serialization library [open source] on the .net platform? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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: 250

*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

Internet Technology

Wechat

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

12
Report