<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 6947字,需瀏覽 14分鐘

           ·

          2022-02-27 12:55

          1支持忽略循環(huán)引用

          在 .NET 6 之前,如果 System.Text.Json 檢測到循環(huán)引用,就會拋出 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 Children { get; set; } = new();
          }

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

          2(反)序列化事件通知

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

          有四個新的接口可以根據(jù)你的需要來實現(xiàn):

          • IJsonOnDeserialized

          • IJsonOnDeserializing

          • IJsonOnSerialized

          • IJsonOnSerializing

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

          string invalidJson = "{}";
          JsonSerializer.Deserialize(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 中包含現(xiàn)有的 JSON

          • 以不同于默認(rèn)格式的方式對數(shù)值進(jìn)行格式化

          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 支持 IAsyncEnumerable。IAsyncEnumerable 的序列化將其轉(zhuǎn)化為一個數(shù)組。對于根級 JSON 數(shù)組的反序列化,增加了 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 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

          下面的 GIF 展示了 IAsyncEnumerable 如何被序列化:

          6JSON 和 Stream 互轉(zhuǎn)

          在 .NET 6 中,添加了同步方法 Serialize/DeserializeStream 重載。

          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(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(outputStream, exampleToSerialize, options);
          // Output:
          // {
          // "Value": "Serialized from stream"
          // }

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

          7像 DOM 一樣操作 JSON

          在 .NET 6 中,提供了處理 in-memory 可寫文檔對象模型(DOM)的類型,用于隨機(jī)訪問結(jié)構(gòu)化數(shù)據(jù)視圖中的 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

          原文:bit.ly/3zma38X
          作者:Oleg Kyrylchuk
          翻譯:精致碼農(nóng)


          瀏覽 48
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产免费AV在线观看 | 欧美熟女BB | 国产爱搞视频网 | 性高潮视频在线观看 | 成人网站72 |