首页 > 其他分享 >.NET 6 中 System.Text.Json 的新特性

.NET 6 中 System.Text.Json 的新特性

时间:2023-06-23 10:56:09浏览次数:56  
标签:Console string Text System new NET public Name

1 支持忽略循环引用

在 .NET 6 之前,如果 System.Text.Json 检测到循环引用,就会抛出 JsonException 异常。在 .NET 6 中,你可以忽略它们。

Category dotnet = new()
{
    Name = ".NET 6",
};
Category systemTextJson = new()
{
    Name = "System.Text.Json",
    Parent = dotnet
};
dotnet.Children.Add(systemTextJson);

JsonSerializerOptions options = new()
{
    ReferenceHandler = ReferenceHandler.IgnoreCycles,
    WriteIndented = true
};

string dotnetJson = JsonSerializer.Serialize(dotnet, options);
Console.WriteLine($"{dotnetJson}");

public class Category
{
    public string Name { get; set; }
    public Category Parent { get; set; }
    public List<Category> Children { get; set; } = new();
}

// Output:
// {
//   "Name": ".NET 6",
//   "Parent": null,
//   "Children": [
//     {
//       "Name": "System.Text.Json",
//       "Parent": null,
//       "Children": []
//     }
//   ]
// }

  

2 (反)序列化事件通知

在 .NET 6 中,System.Text.Json 暴露了(反)序列化的事件通知接口。

有四个新的接口可以根据你的需要来实现:

  • IJsonOnDeserialized
  • IJsonOnDeserializing
  • IJsonOnSerialized
  • IJsonOnSerializing
Product invalidProduct = new() { Name = "Name", Test = "Test" };
JsonSerializer.Serialize(invalidProduct);
// The InvalidOperationException is thrown

string invalidJson = "{}";
JsonSerializer.Deserialize<Product>(invalidJson);
// The InvalidOperationException is thrown

class Product : IJsonOnDeserialized, IJsonOnSerializing, IJsonOnSerialized
{
    public string Name { get; set; }

    public string Test { get; set; }

    public void OnSerialized()
    {
        throw new NotImplementedException();
    }

    void IJsonOnDeserialized.OnDeserialized() => Validate(); // Call after deserialization
    void IJsonOnSerializing.OnSerializing() => Validate();   // Call before serialization

    private void Validate()
    {
        if (Name is null)
        {
            throw new InvalidOperationException("The 'Name' property cannot be 'null'.");
        }
    }
}

3 支持 Order 属性

以前,序列化顺序是由反射顺序决定的。在 .NET 6 中,System.Text.Json 添加了 JsonPropertyOrderAttribute 特性,它允许控制属性的序列化顺序。

Product product = new()
{
    Id = 1,
    Name = "Surface Pro 7",
    Price = 550,
    Category = "Laptops"
};

JsonSerializerOptions options = new() { WriteIndented = true };
string json = JsonSerializer.Serialize(product, options);
Console.WriteLine(json);

class Product : A
{
    [JsonPropertyOrder(2)] // Serialize after Price
    public string Category { get; set; }

    [JsonPropertyOrder(1)] // Serialize after other properties that have default ordering
    public decimal Price { get; set; }

    public string Name { get; set; } // Has default ordering value of 0

    [JsonPropertyOrder(-1)] // Serialize before other properties that have default ordering
    public int Id { get; set; }
}

class A
{
    public int Test { get; set; }
}

// Output:
// {
//   "Id": 1,
//   "Name": "Surface Pro 7",
//   "Price": 550,
//   "Category": "Laptops"
// }

  

4 用 Utf8JsonWriter 写原始 JSON

.NET 6 引入了 System.Text.Json.Utf8JsonWriter,用它可以写原始 JSON。

在你有下面需要的时候很有用:

  • 在新的 JSON 中包含现有的 JSON
  • 以不同于默认格式的方式对数值进行格式化
JsonWriterOptions writerOptions = new() { Indented = true, };

using MemoryStream stream = new();
using Utf8JsonWriter writer = new(stream, writerOptions);

writer.WriteStartObject();
writer.WriteStartArray("customJsonFormatting");
foreach (double result in new double[] { 10.2, 10 })
{
    writer.WriteStartObject();
    writer.WritePropertyName("value");
    writer.WriteRawValue(FormatNumberValue(result), skipInputValidation: true);
    writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
writer.Flush();

string json = Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine(json);

static string FormatNumberValue(double numberValue)
{
    return numberValue == Convert.ToInt32(numberValue)
        ? numberValue.ToString() + ".0"
        : numberValue.ToString();
}

// Output:
// {
//    "customJsonFormatting": [
//      {
//        "value": 10.2
//      },
//      {
//        "value": 10.0
//      }
//  ]
// }

  

5 支持 IAsyncEnumerable

在 .NET 6 中,System.Text.Json 支持 IAsyncEnumerableIAsyncEnumerable 的序列化将其转化为一个数组。对于根级 JSON 数组的反序列化,增加了 DeserializeAsyncEnumerable 方法。

static async IAsyncEnumerable<int> GetNumbersAsync(int n)
{
    for (int i = 0; i < n; i++)
    {
        await Task.Delay(1000);
        yield return i;
    }
}
// Serialization using IAsyncEnumerable
JsonSerializerOptions options = new() { WriteIndented = true };
using Stream outputStream = Console.OpenStandardOutput();
var data = new { Data = GetNumbersAsync(5) };
await JsonSerializer.SerializeAsync(outputStream, data, options);
// Output:
// {
//    "Data": [
//      0,
//      1,
//      2,
//      3,
//      4
//  ]
// }

// Deserialization using IAsyncEnumerable
using MemoryStream memoryStream = new(Encoding.UTF8.GetBytes("[0,1,2,3,4]"));
// Wraps the UTF-8 encoded text into an IAsyncEnumerable<T> that can be used to deserialize root-level JSON arrays in a streaming manner.
await foreach (int item in JsonSerializer.DeserializeAsyncEnumerable<int>(memoryStream))
{
    Console.WriteLine(item);
}
// Output:
// 0
// 1
// 2
// 3
// 4

  

6 JSON 和 Stream 互转

在 .NET 6 中,添加了同步方法 Serialize/Deserialize 的 Stream 重载。

string json = "{\"Value\":\"Deserialized from stream\"}";
byte[] bytes = Encoding.UTF8.GetBytes(json);

// Deserialize from stream
using MemoryStream ms = new MemoryStream(bytes);
Example desializedExample = JsonSerializer.Deserialize<Example>(ms);
Console.WriteLine(desializedExample.Value);
// Output: Deserialized from stream

// ==================================================================

// Serialize to stream
JsonSerializerOptions options = new() { WriteIndented = true };
using Stream outputStream = Console.OpenStandardOutput();
Example exampleToSerialize = new() { Value = "Serialized from stream" };
JsonSerializer.Serialize<Example>(outputStream, exampleToSerialize, options);
// Output:
// {
//    "Value": "Serialized from stream"
// }

class Example
{
    public string Value { get; set; }
}

 

7 像 DOM 一样操作 JSON

在 .NET 6 中,提供了处理 in-memory 可写文档对象模型(DOM)的类型,用于随机访问结构化数据视图中的 JSON 元素。

新类型:

  • JsonArray
  • JsonNode
  • JsonObject
  • JsonValue
// Parse a JSON object
JsonNode jNode = JsonNode.Parse("{\"Value\":\"Text\",\"Array\":[1,5,13,17,2]}");
string value = (string)jNode["Value"];
Console.WriteLine(value); // Text
                          // or
value = jNode["Value"].GetValue<string>();
Console.WriteLine(value); // Text

int arrayItem = jNode["Array"][1].GetValue<int>();
Console.WriteLine(arrayItem); // 5
                              // or
arrayItem = jNode["Array"][1].GetValue<int>();
Console.WriteLine(arrayItem); // 5

// Create a new JsonObject
var jObject = new JsonObject
{
    ["Value"] = "Text",
    ["Array"] = new JsonArray(1, 5, 13, 17, 2)
};
Console.WriteLine(jObject["Value"].GetValue<string>());  // Text
Console.WriteLine(jObject["Array"][1].GetValue<int>());  // 5

// Converts the current instance to string in JSON format
string json = jObject.ToJsonString();
Console.WriteLine(json); // {"Value":"Text","Array":[1,5,13,17,2]} 

8 收尾

所有的代码示例你都可以在我的 GitHub 上找到:

https://github.com/okyrylchuk/dotnet6_features/tree/main/System.Text.Json%20features

  

标签:Console,string,Text,System,new,NET,public,Name
From: https://www.cnblogs.com/friend/p/17498834.html

相关文章

  • 宏基笔记本安装固态硬盘,重装系统后,无法进入系统,operating system not found
    1开机显示operatingsystemnotfound 2开机按F2(根据自己笔记本设置)然后点到boot选项,找到BootListoption[UEFI]然后按enter键进去,把UEFI改为Legacy,然后按F10保存,遇到警告的话,按yes......
  • SpringCloud依赖问题:spring-cloud-starter-eureka-server 和 spring-cloud-starter-ne
    学习SpringCloud微服务时,很多资料上都写的是spring-cloud-starter-eureka-server,结果问题无法正常启动,这是因为与当前的SpringBoot版本不匹配。其实较新的版本应该使用spring-cloud-starter-netflix-eureka-server依赖。PS:SpringCloud的版本不兼容好坑。......
  • VB.NET开发Autocad,拿来就用的常用语句
    一,导入命名空间                             一、导入命名空间ImportsAcApps=Autodesk.AutoCAD.ApplicationServicesImportsAutodesk.AutoCAD.DatabaseServicesImportsAutodesk.AutoCAD.EditorInputImpor......
  • .Net 查询日志的几个位置
    日志类型存放位置实现方式说明请求日志一般以结构化数据存储在数据库中,字段是开发者自己定义,一般有关键字段例如请求地址、时间、耗时、参数、返回值等等通过Filter过滤器实现,手动的记录手动Log日志网站目录或者其它存储位置,根据开发者配置来就是.netcore的I......
  • STelnet远程登录设备
    1、应用场景Stelnet提供安全的认证方式,用户可以通过STelnet安全地登录远程设备,对设备进行管理和维护。举个“栗子”用户需要安全地登录远程设备,并对其进行方便地管理和维护:设备作为SSHServer,IP地址为10.137.217.203/24。2、前提条件用户本地PC安装SSH服务器软件。本例中以Pu......
  • 在sql中使用函数,遇到net.sf.jsqlparser.parser.ParseException异常
    异常详情如下Causedby:net.sf.jsqlparser.parser.ParseException:Encountered""->""->""atline1,column31.Wasexpectingoneof:"AS"..."DO"..."ANY"..."KEY"...……(中间省略很多符号)atnet.......
  • Android,两个互相影响的EditText如何避免死循环
    简单来说,是一个类似如下的需求:两个EditText,假设名字分别是et1和et2;et1的值*一个数字,假设是4500=et2的值;当et1的值发生变化时,et2的值也发生变化,et2的值发生变化时,et1的值也发生变化,用过用简单的 TextWatcher就会发生死循环,如何避免,下面是这个例子代码的最核心部分,简单来说就......
  • kubernetes安装和调试(1-20-9-docker19-03-0-weave)
    kubernetes安装和调试(1.20.9+docker19.03.0+weave)学习kubernetes来对项目进行容器化部署。这里使用了VMware创建虚拟机来搭建k8s集群。VMware:16.1.2,CentOS7,docker:19.03.0,kubeadm:1.20.9,kubectl:1.20.9,kubelet:1.20.9,weave。CentOS安装镜像下载CentOS7下载地址:CentOS7.9......
  • C#/.Net的多播委托到底是啥?彻底剖析下
    前言委托在.Net里面被托管代码封装了之后,看起来似乎有些复杂。但是实际上委托即是函数指针,而多播委托,即是函数指针链。本篇来只涉及底层的逻辑,慎入。概括1.示例代码publicdelegatevoidABC();//委托写在类的外面publicclassTest{publicABCAAA;publicvoidA()......
  • 什么是 ABAP 的 Message Class,Message Number 和 Message Text 试读版
    ABAP编程语言里的Message(消息)是SAP产品里及其重要的一个概念,因为Message是SAP应用在运行过程中,向终端用户提供运行反馈的最重要的交互渠道之一。当用户使用SAP产品过程中,如果遇到各种错误或者提示消息,会根据这些消息,查询文档或者咨询SAP支持人员,以获得下一步的操作......