Редактиране

Споделяне чрез


Supported collection types in System.Text.Json

This article gives an overview of which collections are supported for serialization and deserialization. System.Text.Json.JsonSerializer supports a collection type for serialization if it:

The serializer calls the GetEnumerator() method and writes the elements.

Deserialization is more complicated and is not supported for some collection types.

The following sections are organized by namespace and show which types are supported for serialization and deserialization.

System.Array namespace

Type Serialization Deserialization
Single-dimensional arrays ✔️ ✔️
Multi-dimensional arrays
Jagged arrays ✔️ ✔️

System.Collections namespace

Type Serialization Deserialization
ArrayList ✔️ ✔️
BitArray ✔️
DictionaryEntry ✔️ ✔️
Hashtable ✔️ ✔️
ICollection ✔️ ✔️
IDictionary ✔️ ✔️
IEnumerable ✔️ ✔️
IList ✔️ ✔️
Queue ✔️ ✔️
SortedList ✔️ ✔️
Stack * ✔️ ✔️

* See Support round trip for Stack types.

System.Collections.Generic namespace

Type Serialization Deserialization
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> ✔️ ✔️

* See Supported key types.

† See the following section on IAsyncEnumerable<T>.

‡ See Support round trip for Stack types.

IAsyncEnumerable<T>

The following examples use streams as a representation of any async source of data. The source could be files on a local machine, or results from a database query or web service API call.

Stream serialization

System.Text.Json supports serializing IAsyncEnumerable<T> values as JSON arrays, as shown in the following example:

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> values are only supported by the asynchronous serialization methods, such as JsonSerializer.SerializeAsync.

Stream deserialization

The DeserializeAsyncEnumerable method supports streaming deserialization, as shown in the following example:

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

The DeserializeAsyncEnumerable method only supports reading from root-level JSON arrays.

The DeserializeAsync method supports IAsyncEnumerable<T>, but its signature doesn't allow streaming. It returns the final result as a single value, as shown in the following example.

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

In this example, the deserializer buffers all IAsyncEnumerable<T> contents in memory before returning the deserialized object. This behavior is necessary because the deserializer needs to read the entire JSON payload before returning a result.

System.Collections.Immutable namespace

Type Serialization Deserialization
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> * ✔️ ✔️

* See Support round trip for Stack types.

† See Supported key types.

System.Collections.Specialized namespace

Type Serialization Deserialization
BitVector32 ✔️ ❌*
HybridDictionary ✔️ ✔️
IOrderedDictionary ✔️
ListDictionary ✔️ ✔️
NameValueCollection ✔️
StringCollection ✔️
StringDictionary ✔️

* When BitVector32 is deserialized, the Data property is skipped because it doesn't have a public setter. No exception is thrown.

System.Collections.Concurrent namespace

Type Serialization Deserialization
BlockingCollection<T> ✔️
ConcurrentBag<T> ✔️
ConcurrentDictionary<TKey,TValue> ✔️ ✔️
ConcurrentQueue<T> ✔️ ✔️
ConcurrentStack<T> * ✔️ ✔️

* See Support round trip for Stack types.

† See Supported key types.

System.Collections.ObjectModel namespace

Type Serialization Deserialization
Collection<T> ✔️ ✔️
KeyedCollection<string, TValue> * ✔️
ObservableCollection<T> ✔️ ✔️
ReadOnlyCollection<T> ✔️
ReadOnlyDictionary<TKey,TValue> ✔️
ReadOnlyObservableCollection<T> ✔️

* Non-string keys are not supported.

Custom collections

Any collection type that isn't in one of the preceding namespaces is considered a custom collection. Such types include user-defined types and types defined by ASP.NET Core. For example, Microsoft.Extensions.Primitives is in this group.

All custom collections (everything that derives from IEnumerable) are supported for serialization, as long as their element types are supported.

Custom collections with deserialization support

A custom collection is supported for deserialization if it:

Custom collections with known issues

There are known issues with the following custom collections:

For more information about known issues, see the open issues in System.Text.Json.

Supported key types

Supported types for the keys of Dictionary and SortedList types include the following:

  • Boolean
  • Byte
  • DateTime
  • DateTimeOffset
  • Decimal
  • Double
  • Enum
  • Guid
  • Int16
  • Int32
  • Int64
  • Object (Only on serialization and if the runtime type is one of the supported types in this list.)
  • SByte
  • Single
  • String
  • UInt16
  • UInt32
  • UInt64

System.Data namespace

There are no built-in converters for DataSet, DataTable, and related types in the System.Data namespace. Deserializing these types from untrusted input is not safe, as explained in the security guidance. However, you can write a custom converter to support these types. For sample custom converter code that serializes and deserializes a DataTable, see RoundtripDataTable.cs.

See also