System.Text.Json should support deserializing more collection types #107787
Labels
area-System.Text.Json
enhancement
Product code improvement that does NOT require public API changes/additions
Milestone
STJ will today happily serialize any collection type that implements
IEnumerable<T>
, however things are less than ideal when it comes to deserialization support. While the serializer does include baked-in support for a number of common collection types, we still have a number of unsupported types such as frozen collections,ArraySegment<T>
orIReadOnlySet<T>
. Things become even more difficult when working with user-defined types: unless the type implements the conventions used by collection initializers users need to write their own custom converters.This issue proposes that we extend generic support for collection type deserialization by detecting the following patterns (in order of preference):
CollectionBuilderAttribute
or,IEnumerable<T>
parameter (where theT
must match theT
of the implementedIEnumerable<T>
).I have prototyped an implementation of this approach both for reflection and source gen in the typeshape-csharp project. This should add OOTB support for multiple built-in types that aren't supported today, or should make it relatively straightforward to make changes to collections such that they are supported (e.g. by adding
CollectinBuilderAttribute
annotations). For popular types not conforming to any of these patterns, we should consider adding baked-in converters to support them.Related Issues
cc @stephentoub @CyrusNajmabadi
The text was updated successfully, but these errors were encountered: