中支援的集合類型 System.Text.Json

本文概述序列化和還原序列化支援哪些集合。 System.Text.Json.JsonSerializer 如果序列化支援集合類型:

序列化程式會呼叫 方法, GetEnumerator() 並寫入 專案。

還原序列化比較複雜,而且某些集合類型不支援。

下列各節會依命名空間組織,並顯示序列化和還原序列化支援哪些類型。

System.Array 命名空間

類型 序列化 還原序列化
單一維度陣列 ✔️ ✔️
多維度陣列
不規則陣列 ✔️ ✔️

System.Collections 命名空間

類型 序列化 還原序列化
ArrayList ✔️ ✔️
BitArray ✔️
DictionaryEntry ✔️ ✔️
Hashtable ✔️ ✔️
ICollection ✔️ ✔️
IDictionary ✔️ ✔️
IEnumerable ✔️ ✔️
IList ✔️ ✔️
Queue ✔️ ✔️
SortedList ✔️ ✔️
Stack * ✔️ ✔️

* 如需類型,請參閱 支援往返 Stack

System.Collections.Generic 命名空間

類型 序列化 還原序列化
Dictionary<TKey,TValue> * ✔️ ✔️
HashSet<T> ✔️ ✔️
IAsyncEnumerable<T> ✔️ ✔️
ICollection<T> ✔️ ✔️
IDictionary<TKey,TValue> * ✔️ ✔️
IEnumerable<T> ✔️ ✔️
IList<T> ✔️ ✔️
IReadOnlyCollection<T> ✔️ ✔️
IReadOnlyDictionary<TKey,TValue> * ✔️ ✔️
IReadOnlyList<T> ✔️ ✔️
ISet<T> ✔️ ✔️
KeyValuePair<TKey,TValue> ✔️ ✔️
LinkedList<T> ✔️ ✔️
LinkedListNode<T> ✔️
List<T> ✔️ ✔️
Queue<T> ✔️ ✔️
SortedDictionary<TKey,TValue> * ✔️ ✔️
SortedList<TKey,TValue> * ✔️ ✔️
SortedSet<T> ✔️ ✔️
Stack<T> ✔️ ✔️

* 請參閱 支援的金鑰類型

† 請參閱關於 的下一節 IAsyncEnumerable<T>

≦ 如需類型 ,請參閱 支援來回 Stack 行程。

IAsyncEnumerable < T>

下列範例使用資料流程做為任何非同步資料來源的表示。 來源可以是本機電腦上的檔案,或是資料庫查詢或 Web 服務 API 呼叫的結果。

資料流程序列化

System.Text.Json 支援將值序列化 IAsyncEnumerable<T> 為 JSON 陣列,如下列範例所示:

using System.Text.Json;

namespace IAsyncEnumerableSerialize;

public class Program
{
    public static async Task Main()
    {
        using Stream stream = Console.OpenStandardOutput();
        var data = new { Data = PrintNumbers(3) };
        await JsonSerializer.SerializeAsync(stream, data);
    }

    static async IAsyncEnumerable<int> PrintNumbers(int n)
    {
        for (int i = 0; i < n; i++)
        {
            await Task.Delay(1000);
            yield return i;
        }
    }
}
// output:
//  {"Data":[0,1,2]}

IAsyncEnumerable<T> 只有非同步序列化方法才支援值,例如 JsonSerializer.SerializeAsync

資料流程還原序列化

DeserializeAsyncEnumerable方法支援串流還原序列化,如下列範例所示:

using System.Text;
using System.Text.Json;

namespace IAsyncEnumerableDeserialize;

public class Program
{
    public static async Task Main()
    {
        using var stream = new MemoryStream(Encoding.UTF8.GetBytes("[0,1,2,3,4]"));
        await foreach (int item in JsonSerializer.DeserializeAsyncEnumerable<int>(stream))
        {
            Console.WriteLine(item);
        }
    }
}
// output:
//0
//1
//2
//3
//4

方法 DeserializeAsyncEnumerable 僅支援從根層級 JSON 陣列讀取。

DeserializeAsync方法支援 IAsyncEnumerable<T> ,但其簽章不允許串流。 它會以單一值傳回最終結果,如下列範例所示。

using System.Text;
using System.Text.Json;

namespace IAsyncEnumerableDeserializeNonStreaming;

public class MyPoco
{
    public IAsyncEnumerable<int>? Data { get; set; }
}

public class Program
{
    public static async Task Main()
    {
        using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@"{""Data"":[0,1,2,3,4]}"));
        MyPoco? result = await JsonSerializer.DeserializeAsync<MyPoco>(stream)!;
        await foreach (int item in result!.Data!)
        {
            Console.WriteLine(item);
        }
    }
}
// output:
//0
//1
//2
//3
//4

在此範例中,還原序列化程式會在傳回還原序列化物件之前,先緩衝 IAsyncEnumerable<T> 處理記憶體中的所有內容。 這是必要的行為,因為還原序列化程式必須在傳回結果之前讀取整個 JSON 承載。

System.Collections.Immutable 命名空間

類型 序列化 還原序列化
IImmutableDictionary<TKey,TValue> ✔️ ✔️
IImmutableList<T> ✔️ ✔️
IImmutableQueue<T> ✔️ ✔️
IImmutableSet<T> ✔️ ✔️
IImmutableStack<T> * ✔️ ✔️
ImmutableArray<T> ✔️ ✔️
ImmutableDictionary<TKey,TValue> ✔️ ✔️
ImmutableHashSet<T> ✔️ ✔️
ImmutableQueue<T> ✔️ ✔️
ImmutableSortedDictionary<TKey,TValue> ✔️ ✔️
ImmutableSortedSet<T> ✔️ ✔️
ImmutableStack<T> * ✔️ ✔️

* 如需類型,請參閱 支援往返 Stack

†請參閱 支援的金鑰類型

System.Collections.Specialized 命名空間

類型 序列化 還原序列化
BitVector32 ✔️ ❌*
HybridDictionary ✔️ ✔️
IOrderedDictionary ✔️
ListDictionary ✔️ ✔️
NameValueCollection ✔️
StringCollection ✔️
StringDictionary ✔️

* 還原序列化時 BitVector32Data 會略過 屬性,因為它沒有公用 setter。 不會擲回任何例外狀況。

System.Collections.Concurrent 命名空間

類型 序列化 還原序列化
BlockingCollection<T> ✔️
ConcurrentBag<T> ✔️
ConcurrentDictionary<TKey,TValue> ✔️ ✔️
ConcurrentQueue<T> ✔️ ✔️
ConcurrentStack<T> * ✔️ ✔️

* 如需類型,請參閱 支援往返 Stack

†請參閱 支援的金鑰類型

System.Collections.ObjectModel 命名空間

類型 序列化 還原序列化
Collection<T> ✔️ ✔️
KeyedCollection < 字串,TValue> * ✔️
ObservableCollection<T> ✔️ ✔️
ReadOnlyCollection<T> ✔️
ReadOnlyDictionary<TKey,TValue> ✔️
ReadOnlyObservableCollection<T> ✔️

* 不支援非 string 索引鍵。

自訂集合

任何不在上述命名空間中的集合類型都會被視為自訂集合。 這類類型包括 ASP.NET Core 所定義的使用者定義型別和型別。 例如, Microsoft.Extensions.Primitives 在這個群組中。

所有自訂集合(衍生自 IEnumerable 的所有專案)都支援序列化,只要支援其專案類型。

具有還原序列化支援的自訂集合

如果還原序列化,則支援自訂集合:

已知問題的自訂集合

下列自訂集合有已知問題:

如需已知問題的詳細資訊,請參閱 中的 System.Text.Json 開啟問題。

支援的金鑰類型

SortedList 類型的索引鍵 Dictionary 支援類型包括下列專案:

  • Boolean
  • Byte
  • DateTime
  • DateTimeOffset
  • Decimal
  • Double
  • Enum
  • Guid
  • Int16
  • Int32
  • Int64
  • Object (只有在序列化上,且執行時間類型在此清單中是其中一個支援的型別時。
  • SByte
  • Single
  • String
  • UInt16
  • UInt32
  • UInt64

System.Data 命名空間

命名空間中 System.Data 沒有 、 DataTable 和 相關類型的內建轉換器 DataSet 。 從不受信任的輸入還原序列化這些類型並不安全,如安全性指引 中所述 。 不過,您可以撰寫自訂轉換器來支援這些類型。 如需序列化和還原序列化 的 DataTable 範例自訂轉換程式碼,請參閱 往返DataTable.cs

另請參閱