Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add support for types derived from supported BCL collections (#39001)
Browse files Browse the repository at this point in the history
* Add support for types derived from supported BCL collections

* Fix merge issues

* Re-add commented-out tests

* Address review comments
  • Loading branch information
layomia authored Jul 11, 2019
1 parent ae79f49 commit 6005a1e
Show file tree
Hide file tree
Showing 29 changed files with 3,488 additions and 464 deletions.
11 changes: 7 additions & 4 deletions src/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
<Compile Include="System\Text\Json\JsonHelpers.cs" />
<Compile Include="System\Text\Json\JsonHelpers.Date.cs" />
<Compile Include="System\Text\Json\JsonTokenType.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\EnumConverterOptions.cs" />
<Compile Include="System\Text\Json\Serialization\ExtensionDataWriteStatus.cs" />
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonDefaultNamingPolicy.cs" />
<Compile Include="System\Text\Json\ThrowHelper.cs" />
<Compile Include="System\Text\Json\ThrowHelper.Serialization.cs" />
<Compile Include="System\Text\Json\Document\JsonDocument.cs" />
Expand Down Expand Up @@ -54,11 +50,14 @@
<Compile Include="System\Text\Json\Reader\Utf8JsonReader.TryGet.cs" />
<Compile Include="System\Text\Json\Serialization\ClassType.cs" />
<Compile Include="System\Text\Json\Serialization\ConverterList.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultDerivedDictionaryConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultDerivedEnumerableConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultArrayConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultICollectionConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultIDictionaryConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultImmutableEnumerableConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultImmutableDictionaryConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\EnumConverterOptions.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonKeyValuePairConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterBoolean.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterByte.cs" />
Expand All @@ -84,14 +83,17 @@
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt32.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt64.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUri.cs" />
<Compile Include="System\Text\Json\Serialization\ExtensionDataWriteStatus.cs" />
<Compile Include="System\Text\Json\Serialization\JsonAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\JsonCamelCaseNamingPolicy.cs" />
<Compile Include="System\Text\Json\Serialization\JsonClassInfo.cs" />
<Compile Include="System\Text\Json\Serialization\JsonClassInfo.AddProperty.cs" />
<Compile Include="System\Text\Json\Serialization\JsonClassInfo.Helpers.cs" />
<Compile Include="System\Text\Json\Serialization\JsonConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonConverterAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\JsonConverterFactory.cs" />
<Compile Include="System\Text\Json\Serialization\JsonConverterOfT.cs" />
<Compile Include="System\Text\Json\Serialization\JsonDefaultNamingPolicy.cs" />
<Compile Include="System\Text\Json\Serialization\JsonDictionaryConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonEnumerableConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonExtensionDataAttribute.cs" />
Expand Down Expand Up @@ -126,6 +128,7 @@
<Compile Include="System\Text\Json\Serialization\JsonSerializer.Write.Utf8JsonWriter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.Converters.cs" />
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\MemberAccessor.cs" />
<Compile Include="System\Text\Json\Serialization\PooledByteBufferWriter.cs" />
<Compile Include="System\Text\Json\Serialization\PropertyRef.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections;

namespace System.Text.Json.Serialization.Converters
{
internal sealed class DefaultDerivedDictionaryConverter : JsonDictionaryConverter
{
public override object CreateFromDictionary(ref ReadStack state, IDictionary sourceDictionary, JsonSerializerOptions options)
{
JsonPropertyInfo collectionPropertyInfo = state.Current.JsonPropertyInfo;
JsonPropertyInfo elementPropertyInfo = options.GetJsonPropertyInfoFromClassInfo(collectionPropertyInfo.ElementClassInfo, options);
return elementPropertyInfo.CreateDerivedDictionaryInstance(collectionPropertyInfo, sourceDictionary, state.JsonPath, options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections;

namespace System.Text.Json.Serialization.Converters
{
internal sealed class DefaultDerivedEnumerableConverter : JsonEnumerableConverter
{
public override IEnumerable CreateFromList(ref ReadStack state, IList sourceList, JsonSerializerOptions options)
{
JsonPropertyInfo collectionPropertyInfo = state.Current.JsonPropertyInfo;
JsonPropertyInfo elementPropertyInfo = options.GetJsonPropertyInfoFromClassInfo(collectionPropertyInfo.ElementClassInfo, options);
return elementPropertyInfo.CreateDerivedEnumerableInstance(collectionPropertyInfo, sourceList, state.JsonPath, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Text.Json.Serialization.Converters
{
internal sealed class DefaultIDictionaryConverter : JsonDictionaryConverter
{
public override IDictionary CreateFromDictionary(ref ReadStack state, IDictionary sourceDictionary, JsonSerializerOptions options)
public override object CreateFromDictionary(ref ReadStack state, IDictionary sourceDictionary, JsonSerializerOptions options)
{
Type dictionaryType = state.Current.JsonPropertyInfo.RuntimePropertyType;
JsonClassInfo elementClassInfo = state.Current.JsonPropertyInfo.ElementClassInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ namespace System.Text.Json.Serialization.Converters
{
internal sealed class DefaultImmutableDictionaryConverter : JsonDictionaryConverter
{
private const string ImmutableDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableDictionary`2";
private const string ImmutableDictionaryGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableDictionary`2";
private const string ImmutableSortedDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableSortedDictionary`2";
public const string ImmutableDictionaryTypeName = "System.Collections.Immutable.ImmutableDictionary";
public const string ImmutableDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableDictionary`2";
public const string ImmutableDictionaryGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableDictionary`2";

public const string ImmutableSortedDictionaryTypeName = "System.Collections.Immutable.ImmutableSortedDictionary";
public const string ImmutableSortedDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableSortedDictionary`2";

public static void RegisterImmutableDictionary(Type immutableCollectionType, Type elementType, JsonSerializerOptions options)
{
Expand Down Expand Up @@ -52,7 +55,7 @@ public static bool IsImmutableDictionary(Type type)
}
}

public override IDictionary CreateFromDictionary(ref ReadStack state, IDictionary sourceDictionary, JsonSerializerOptions options)
public override object CreateFromDictionary(ref ReadStack state, IDictionary sourceDictionary, JsonSerializerOptions options)
{
Type immutableCollectionType = state.Current.JsonPropertyInfo.RuntimePropertyType;
Type elementType = state.Current.GetElementType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,33 @@
// See the LICENSE file in the project root for more information.

using System.Collections;
using System.Diagnostics;

namespace System.Text.Json.Serialization.Converters
{
// This converter returns enumerables in the System.Collections.Immutable namespace.
internal sealed class DefaultImmutableEnumerableConverter : JsonEnumerableConverter
{
private const string ImmutableArrayTypeName = "System.Collections.Immutable.ImmutableArray";
public const string ImmutableArrayGenericTypeName = "System.Collections.Immutable.ImmutableArray`1";

private const string ImmutableListTypeName = "System.Collections.Immutable.ImmutableList";
private const string ImmutableListGenericTypeName = "System.Collections.Immutable.ImmutableList`1";
private const string ImmutableListGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableList`1";
public const string ImmutableListGenericTypeName = "System.Collections.Immutable.ImmutableList`1";
public const string ImmutableListGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableList`1";

private const string ImmutableStackTypeName = "System.Collections.Immutable.ImmutableStack";
private const string ImmutableStackGenericTypeName = "System.Collections.Immutable.ImmutableStack`1";
private const string ImmutableStackGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableStack`1";
public const string ImmutableStackGenericTypeName = "System.Collections.Immutable.ImmutableStack`1";
public const string ImmutableStackGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableStack`1";

private const string ImmutableQueueTypeName = "System.Collections.Immutable.ImmutableQueue";
private const string ImmutableQueueGenericTypeName = "System.Collections.Immutable.ImmutableQueue`1";
private const string ImmutableQueueGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableQueue`1";
public const string ImmutableQueueGenericTypeName = "System.Collections.Immutable.ImmutableQueue`1";
public const string ImmutableQueueGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableQueue`1";

private const string ImmutableSortedSetTypeName = "System.Collections.Immutable.ImmutableSortedSet";
private const string ImmutableSortedSetGenericTypeName = "System.Collections.Immutable.ImmutableSortedSet`1";
public const string ImmutableSortedSetTypeName = "System.Collections.Immutable.ImmutableSortedSet";
public const string ImmutableSortedSetGenericTypeName = "System.Collections.Immutable.ImmutableSortedSet`1";

private const string ImmutableHashSetTypeName = "System.Collections.Immutable.ImmutableHashSet";
private const string ImmutableHashSetGenericTypeName = "System.Collections.Immutable.ImmutableHashSet`1";
private const string ImmutableSetGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableSet`1";

private const string ImmutableDictionaryTypeName = "System.Collections.Immutable.ImmutableDictionary";
private const string ImmutableDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableDictionary`2";
private const string ImmutableDictionaryGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableDictionary`2";

private const string ImmutableSortedDictionaryTypeName = "System.Collections.Immutable.ImmutableSortedDictionary";
private const string ImmutableSortedDictionaryGenericTypeName = "System.Collections.Immutable.ImmutableSortedDictionary`2";
public const string ImmutableHashSetGenericTypeName = "System.Collections.Immutable.ImmutableHashSet`1";
public const string ImmutableSetGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableSet`1";

public static string GetDelegateKey(
Type immutableCollectionType,
Expand Down Expand Up @@ -67,12 +62,12 @@ public static string GetDelegateKey(
case ImmutableSetGenericInterfaceTypeName:
constructingTypeName = ImmutableHashSetTypeName;
break;
case ImmutableDictionaryGenericTypeName:
case ImmutableDictionaryGenericInterfaceTypeName:
constructingTypeName = ImmutableDictionaryTypeName;
case DefaultImmutableDictionaryConverter.ImmutableDictionaryGenericTypeName:
case DefaultImmutableDictionaryConverter.ImmutableDictionaryGenericInterfaceTypeName:
constructingTypeName = DefaultImmutableDictionaryConverter.ImmutableDictionaryTypeName;
break;
case ImmutableSortedDictionaryGenericTypeName:
constructingTypeName = ImmutableSortedDictionaryTypeName;
case DefaultImmutableDictionaryConverter.ImmutableSortedDictionaryGenericTypeName:
constructingTypeName = DefaultImmutableDictionaryConverter.ImmutableSortedDictionaryTypeName;
break;
default:
throw ThrowHelper.GetNotSupportedException_SerializationNotSupportedCollection(immutableCollectionType, null, null);
Expand Down
Loading

0 comments on commit 6005a1e

Please sign in to comment.