Consider (head at dotnet/corefx@4fbef81)
using System;
using System.Text.Json;
using Microsoft.Extensions.Primitives;
namespace stringvaluestest
{
class Program
{
static void Main(string[] args)
{
string json = @"[""My Val""]";
JsonSerializer.Deserialize<StringValues>(json);
}
}
}
Output
Unhandled exception. System.NotSupportedException: Specified method is not supported.
at Microsoft.Extensions.Primitives.StringValues.System.Collections.Generic.ICollection<System.String>.Add(String item)
at System.Text.Json.JsonPropertyInfoCommon`4.CreateDerivedEnumerableInstance(JsonPropertyInfo collectionPropertyInfo, IList sourceList, String jsonPath, JsonSerializerOptions options)
at System.Text.Json.Serialization.Converters.DefaultDerivedEnumerableConverter.CreateFromList(ReadStack& state, IList sourceList, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.HandleEndArray(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at stringvaluestest.Program.Main(String[] args) in D:\console_apps\stringvaluestest\Program.cs:line 14
dotnet/corefx#39001 adds support for types that implement BCL enumerables that are natively supported in the serializer.
For cases like StringValue where we cannot invoke the add method of the implemented type (ICollection<string>.Add in this case), we should throw a JsonException instead of a NotSupportedException:
For enumerables that are not supported: readonly collections like StringValues (implements ICollection<string>), and collections that do not implement any of:
IList
ICollection<T>
Stack<T>
Queue<T>
IDictionary
IDictionary<string, TValue>,
the serializer should throw a NotSupportedException.
These collections can be supported in the future.
Consider (head at dotnet/corefx@4fbef81)
Output
dotnet/corefx#39001 adds support for types that implement BCL enumerables that are natively supported in the serializer.
For cases likeStringValuewhere we cannot invoke the add method of the implemented type (ICollection<string>.Addin this case), we should throw aJsonExceptioninstead of aNotSupportedException:For enumerables that are not supported: readonly collections like
StringValues(implementsICollection<string>), and collections that do not implement any of:IListICollection<T>Stack<T>Queue<T>IDictionaryIDictionary<string, TValue>,the serializer should throw a
NotSupportedException.These collections can be supported in the future.