闽公网安备 35020302035485号
JsonSerializer.Serialize(student, new JsonSerializerOptions()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true //不敏感大小写
});
反序列化:JsonSerializer.Deserialize<Student>("xxxx");
本身微软就宣称System.Text.Json.JsonSerializer性能是强于一个Newtonsoft,所以这两年一直使用微软自带的。当然话题扯远了,只是带大家稍微了解回顾下。我们来看看微软官网提供的反射和源生成两种方式在Json序列化中的优劣:



[JsonSerializable(typeof(bool))] [JsonSerializable(typeof(int))]以增加对于这些类型的支持,便于源生成提前生成相关类型代码。
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Student))]
[JsonSerializable(typeof(Teacher))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
分析器下会出现一些自动生成的代码:
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
{
}
注意点:如果不显示设置源生成模式,那么会同时应用元数据收集和序列化优化两种方式。using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DemoSourceGenerator
{
public class Student
{
public 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()
{
Id = 1,
StuName = "Bruce",
Birthday = DateTime.Parse("1996-08-24"),
Address = "上海市浦东新区"
};
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true
};
Stopwatch stopwatch1 = new Stopwatch();
stopwatch1.Start();
foreach (var index in Enumerable.Range(0, 100000))
{
JsonSerializer.Serialize(student, jsonOptions);
}
stopwatch1.Stop();
Console.WriteLine($"原始的序列化时间:{stopwatch1.ElapsedMilliseconds}");
Stopwatch stopwatch2 = new Stopwatch();
stopwatch2.Start();
foreach (var index in Enumerable.Range(0, 100000))
{
JsonSerializer.Serialize(student, SourceGenerationContext.Default.Student);
}
stopwatch2.Stop();
Console.WriteLine($"源码生成器的序列化时间:{stopwatch2.ElapsedMilliseconds}");
}
}
}
我们直接跑这个程序看看