1

.net6&7中如何优雅且高性能的使用Json序列化 - BruceNeter

 1 year ago
source link: https://www.cnblogs.com/qwqwQAQ/p/16944672.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

.net6&7中如何优雅且高性能的使用Json序列化

.net中的SourceGenerator让开发者编可以写分析器,在项目代码编译时,分析器分析项目既有的静态代码,允许添加源代码到GeneratorExecutionContext中,一同与既有的代码参与编译。这种技术其实是把一些运行时才能去获取程序集相关资源的方式提前到编译前了。
.net6开始,微软为我们提供了System.Text.Json的SourceGenerator版本,接下来我们一起基于一个.net6的控制台项目学习了解System.Text.Json.SourceGenerator.
(SourceGenerator以下简称源生成)

反射 vs 源生成

目前基本所有的序列化和反序列化都是基于反射,反射是运行时的一些操作,一直以来性能差而被诟病。System.Text.Json中的JsonSerializer对象中的序列化操作也是基于反射的,我们常用的方法如下:
序列化:

JsonSerializer.Serialize(student, new JsonSerializerOptions()
{
    WriteIndented = true, 
    PropertyNameCaseInsensitive = true //不敏感大小写
});

反序列化:

JsonSerializer.Deserialize<Student>("xxxx");

本身微软就宣称System.Text.Json.JsonSerializer性能是强于一个Newtonsoft,所以这两年一直使用微软自带的。
当然话题扯远了,只是带大家稍微了解回顾下。
我们来看看微软官网提供的反射和源生成两种方式在Json序列化中的优劣:

1306612-20221202141407044-219687457.png


1.可以看到反射的易用性和开放程度是高于源生成的。
2.性能方面则是源生成完全碾压。

源生成注意点

1.源生成有两种模式:元数据收集和序列化优化,两者的区别会在下面的实践中给出自己的理解,官网并没有得到较为明确的两种的解释,两种生成模式可以同时存在。默认同时启用。
2.源生成不能够像反射一样可以使用JsonInclude标签将包含私有访问器的公共属性包含进来,会抛NotSupportedException异常

元数据收集&序列化优化

元数据收集

可以使用源生成将元数据收集进程从运行时移到编译时。 在编译期间,系统将收集元数据并生成源代码文件。 生成的源代码文件会自动编译为应用程序的一个整型部分。 使用此方法便无需进行运行时元数据集合,这可提高序列化和反序列化的性能.

序列化优化:

这个就比较好理解一点了,无非就是对于序列化的一些设置选项和特性做出一些优化,当然目前不是所有设置和特性都支持,官网也列出了受支持的设置和特性。

设置选项:

1306612-20221202144653569-725231072.png

特性:

1306612-20221202144729344-2099594683.png

好了说了这么多,大家对一些概念都有了基本了解,我也很讨厌这么多文字的概念往上贴,那么现在就进入实战!

一个.net6的控制台项目,可以观察到它的分析器里有一个System.Text.Json.SourceGenerator这个解析器

1306612-20221202150227672-547665613.png
创建一个序列化上下文

创建SourceGenerationContext派生自JsonSerializerContext

指定要序列化或反序列化的类型

通过向上下文类应用 JsonSerializableAttribute 来指定要序列化或反序列化的类型。
不需要为类型的字段类型做特殊处理,但是如果类型包含object类型的对象,并且你知道,在运行时,它可能有 boolean 和 int 对象
则需要添加

[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(int))]

以增加对于这些类型的支持,便于源生成提前生成相关类型代码。

序列化配置

JsonSourceGenerationOptions可以添加一些序列化的配置设置。

序列化上下文最后代码:

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Student))] 
[JsonSerializable(typeof(Teacher))]
internal partial class SourceGenerationContext : JsonSerializerContext
{

}

分析器下会出现一些自动生成的代码:

1306612-20221202152453075-292749258.png
序列化/反序列化
JsonSerializer.Serialize(student, SourceGenerationContext.Default.Student);

反序列化:

var obj = JsonSerializer.Deserialize<Student>(
    jsonString, SourceGenerationContext.Default.Student);
指定源生成方式
元数据收集模式

全部类型设置元数据收集模式

[JsonSourceGenerationOptions(WriteIndented = true,GenerationMode =JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(Student))] 
[JsonSerializable(typeof(Teacher))]
internal partial class SourceGenerationContext : JsonSerializerContext
{

}

单个类型设置元数据收集模式,只设置学生类型使用特定的元数据收集模式

[JsonSourceGenerationOptions(WriteIndented = true,GenerationMode =JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(Student,GenerationMode =JsonSourceGenerationMode.Metadata))]
[JsonSerializable(typeof(Teacher))]
internal partial class SourceGenerationContext : JsonSerializerContext
{

}
序列化优化模式

全部类型设置序列化优化模式

[JsonSourceGenerationOptions(WriteIndented = true,GenerationMode =JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(Student))]
[JsonSerializable(typeof(Teacher))]
internal partial class SourceGenerationContext : JsonSerializerContext
{

}

单个类型设置序列化优化模式,只设置学生类型使用特定的序列化优化模式

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Student), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(Teacher))]
internal partial class SourceGenerationContext : JsonSerializerContext
{

}

注意点:如果不显示设置源生成模式,那么会同时应用元数据收集和序列化优化两种方式。

说了这么多,你凭啥说服我们使用这玩意儿??
我们试试使用JsonSerializer和源生成的方式来跑10000次序列化试试,说试就试,完整代码如下:

using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace DemoSourceGenerator
{
    public class Student
    {
        private int Id { get; set; }
        public string StuName { get; set; }
        public DateTime Birthday { get; set; }
        public string Address { get; set; }
    }

    public class Teacher 
    {
        public int Id { get; set; }
        public string TeacherName { get; set; }
        public DateTime Birthday { get; set; }
        public string Address { get; set; }
    }

    [JsonSourceGenerationOptions(WriteIndented = true)]
    [JsonSerializable(typeof(Student))]
    [JsonSerializable(typeof(Teacher))]
    internal partial class SourceGenerationContext : JsonSerializerContext
    {

    }

    public class Program 
    {
        public static void Main(string[] args) 
        {
            Student student = new Student()
            {
                StuName = "Bruce",
                Birthday = DateTime.Parse("1996-08-24"),
                Address = "上海市浦东新区"
            };

            Stopwatch stopwatch1 = new Stopwatch();
            stopwatch1.Start();
            foreach (var index in Enumerable.Range(0, 10000))
            {
                JsonSerializer.Serialize(student, new JsonSerializerOptions()
                {
                    WriteIndented = true,
                    PropertyNameCaseInsensitive = true
                });
            }
            stopwatch1.Stop();
            Console.WriteLine($"原始的序列化时间:{stopwatch1.ElapsedMilliseconds}");

            Stopwatch stopwatch2 = new Stopwatch();
            stopwatch2.Start();
            foreach (var index in Enumerable.Range(0, 10000))
            {
                JsonSerializer.Serialize(student, SourceGenerationContext.Default.Student);
            }
            stopwatch2.Stop();
            Console.WriteLine($"源码生成器的序列化时间:{stopwatch2.ElapsedMilliseconds}");
        }
    }
}

我们直接跑这个程序看看
1306612-20221202153504541-2073167990.png
跑了几次下来,时间上差距了接近300倍!!!

1.首先肯定是.net 6及其之后的版本,因为我们公司在升级一些服务到.net6,所以可以使用微软提供的这个功能。
2.大量的使用到了序列化和反序列化,可以为建立一个上下文,将这这些类型通过JsonSerializable注册到上下文中,当然也可以根据领域划分多个上下文。

https://learn.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json/source-generation-modes?pivots=dotnet-7-0

https://learn.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json/source-generation-modes?pivots=dotnet-7-0

本文是本人按照官方文档和自己的一些实际使用作出,如存在误区,希望不吝赐教。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK