Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewire polymorphism metadata to the contract model #71346

Merged
merged 3 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
392 changes: 188 additions & 204 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/libraries/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,7 @@
<data name="JsonTypeInfoUsedButTypeInfoResolverNotSet" xml:space="preserve">
<value>JsonTypeInfo metadata references a JsonSerializerOptions instance that doesn't specify a TypeInfoResolver.</value>
</data>
<data name="JsonPolymorphismOptionsAssociatedWithDifferentJsonTypeInfo" xml:space="preserve">
<value>Parameter already associated with a different JsonTypeInfo instance.</value>
</data>
</root>
9 changes: 4 additions & 5 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -123,12 +123,13 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="System\Text\Json\Serialization\JsonUnknownDerivedTypeHandling.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\DefaultJsonTypeInfoResolver.Converters.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\DefaultJsonTypeInfoResolver.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoResolver.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\IJsonTypeInfoResolver.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonDerivedType.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonPolymorphismOptions.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoResolver.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoKind.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\CustomJsonTypeInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\PolymorphicSerializationState.cs" />
<Compile Include="System\Text\Json\Serialization\JsonPolymorphicTypeConfiguration.cs" />
<Compile Include="System\Text\Json\Serialization\ReferenceEqualsWrapper.cs" />
<Compile Include="System\Text\Json\Serialization\ConverterStrategy.cs" />
<Compile Include="System\Text\Json\Serialization\ConfigurationList.cs" />
Expand Down Expand Up @@ -237,10 +238,8 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.cs" />
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonUnknownTypeHandling.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\AttributePolymorphicTypeConfiguration.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\FSharpCoreReflectionProxy.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\DefaultValueHolder.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\IJsonPolymorphicTypeConfiguration.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonCollectionInfoValuesOfTCollection.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Collections.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Converters.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public ConfigurationList(IList<TItem>? source = null)

protected abstract bool IsLockedInstance { get; }
protected abstract void VerifyMutable();
protected virtual void OnItemAdded(TItem item) { }

public TItem this[int index]
{
Expand All @@ -39,7 +38,6 @@ public TItem this[int index]

VerifyMutable();
_list[index] = value;
OnItemAdded(value);
}
}

Expand All @@ -56,7 +54,6 @@ public void Add(TItem item)

VerifyMutable();
_list.Add(item);
OnItemAdded(item);
}

public void Clear()
Expand Down Expand Up @@ -94,7 +91,6 @@ public void Insert(int index, TItem item)

VerifyMutable();
_list.Insert(index, item);
OnItemAdded(item);
}

public bool Remove(TItem item)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ namespace System.Text.Json
{
public static partial class JsonSerializer
{
internal static readonly byte[] s_idPropertyName = "$id"u8.ToArray();
internal static readonly byte[] s_refPropertyName = "$ref"u8.ToArray();
internal static readonly byte[] s_typePropertyName = "$type"u8.ToArray();
internal static readonly byte[] s_valuesPropertyName = "$values"u8.ToArray();
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
internal const string IdPropertyName = "$id";
internal const string RefPropertyName = "$ref";
internal const string TypePropertyName = "$type";
internal const string ValuesPropertyName = "$values";

internal static readonly byte[] s_idPropertyName = Encoding.UTF8.GetBytes(IdPropertyName);
internal static readonly byte[] s_refPropertyName = Encoding.UTF8.GetBytes(RefPropertyName);
internal static readonly byte[] s_typePropertyName = Encoding.UTF8.GetBytes(TypePropertyName);
internal static readonly byte[] s_valuesPropertyName = Encoding.UTF8.GetBytes(ValuesPropertyName);

internal static bool TryReadMetadata(JsonConverter converter, JsonTypeInfo jsonTypeInfo, ref Utf8JsonReader reader, ref ReadStack state)
{
Expand Down Expand Up @@ -97,7 +102,7 @@ internal static bool TryReadMetadata(JsonConverter converter, JsonTypeInfo jsonT
break;

case MetadataPropertyName.Type:
state.Current.JsonPropertyName = jsonTypeInfo.PolymorphicTypeResolver?.CustomTypeDiscriminatorPropertyNameUtf8 ?? s_typePropertyName;
state.Current.JsonPropertyName = jsonTypeInfo.PolymorphicTypeResolver?.TypeDiscriminatorPropertyNameUtf8 ?? s_typePropertyName;

if (jsonTypeInfo.PolymorphicTypeResolver is null)
{
Expand Down Expand Up @@ -219,7 +224,7 @@ internal static bool IsMetadataPropertyName(ReadOnlySpan<byte> propertyName, Pol
{
return
(propertyName.Length > 0 && propertyName[0] == '$') ||
(resolver?.CustomTypeDiscriminatorPropertyNameUtf8?.AsSpan().SequenceEqual(propertyName) == true);
(resolver?.TypeDiscriminatorPropertyNameUtf8?.AsSpan().SequenceEqual(propertyName) == true);
}

internal static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan<byte> propertyName, PolymorphicTypeResolver? resolver)
Expand All @@ -245,7 +250,7 @@ internal static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan<byte>
}
break;

case 5 when resolver?.CustomTypeDiscriminatorPropertyNameUtf8 is null:
case 5 when resolver?.TypeDiscriminatorPropertyNameUtf8 is null:
if (propertyName[1] == 't' &&
propertyName[2] == 'y' &&
propertyName[3] == 'p' &&
Expand All @@ -269,7 +274,7 @@ internal static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan<byte>
}
}

if (resolver?.CustomTypeDiscriminatorPropertyNameUtf8 is byte[] customTypeDiscriminator &&
if (resolver?.TypeDiscriminatorPropertyNameUtf8 is byte[] customTypeDiscriminator &&
propertyName.SequenceEqual(customTypeDiscriminator))
{
return MetadataPropertyName.Type;
Expand Down
Loading