Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace System.Text.Json
/// or a type's converter, if the current instance is a <see cref="JsonClassInfo.PropertyInfoForClassInfo"/>.
internal sealed class JsonPropertyInfo<T> : JsonPropertyInfo
{
/// <summary>
/// Returns true if the property's converter is external (a user's custom converter)
/// and the type to convert is not the same as the declared property type (polymorphic).
/// Used to determine whether to perform additional validation on the value returned by the
/// converter on deserialization.
/// </summary>
private bool _converterIsExternalAndPolymorphic;

public Func<object, T>? Get { get; private set; }
public Action<object, T>? Set { get; private set; }

Expand Down Expand Up @@ -91,6 +99,7 @@ public override void Initialize(
}
}

_converterIsExternalAndPolymorphic = !converter.IsInternalConverter && DeclaredPropertyType != converter.TypeToConvert;
GetPolicies(ignoreCondition, parentTypeNumberHandling, defaultValueIsNull: Converter.CanBeNull);
}

Expand Down Expand Up @@ -230,7 +239,9 @@ public override bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref U
success = Converter.TryRead(ref reader, RuntimePropertyType!, Options, ref state, out T value);
if (success)
{
if (!Converter.IsInternalConverter)
#if !DEBUG
if (_converterIsExternalAndPolymorphic)
#endif
{
if (value != null)
{
Expand All @@ -245,6 +256,7 @@ public override bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref U
ThrowHelper.ThrowInvalidOperationException_DeserializeUnableToAssignNull(DeclaredPropertyType);
}
}

Set!(obj, value!);
}
}
Expand Down
20 changes: 0 additions & 20 deletions src/libraries/System.Text.Json/tests/AssertHelper.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Tests;
using Xunit;

namespace System.Text.Json.Serialization.Tests
Expand Down Expand Up @@ -396,15 +395,14 @@ private class ClassWithPrimitives
[Fact]
public static void ClassWithPrimitivesObjectConverter()
{
string[] expected = new[]
{
@"""MyIntProperty"":123",
@"""MyBoolProperty"":true",
@"""MyStringProperty"":""Hello""",
@"""MyIntField"":321",
@"""MyBoolField"":true",
@"""MyStringField"":""World"""
};
string expected = @"{
""MyIntProperty"":123,
""MyBoolProperty"":true,
""MyStringProperty"":""Hello"",
""MyIntField"":321,
""MyBoolField"":true,
""MyStringField"":""World""
}";

string json;
var converter = new PrimitiveConverter();
Expand All @@ -428,7 +426,7 @@ public static void ClassWithPrimitivesObjectConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(6, converter.WriteCallCount);
AssertHelper.ValidateJson(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}
{
var obj = JsonSerializer.Deserialize<ClassWithPrimitives>(json, options);
Expand Down Expand Up @@ -459,15 +457,14 @@ private class ClassWithNullablePrimitives
[Fact]
public static void ClassWithNullablePrimitivesObjectConverter()
{
string[] expected = new[]
{
@"""MyIntProperty"":123",
@"""MyBoolProperty"":true",
@"""MyStringProperty"":""Hello""",
@"""MyIntField"":321",
@"""MyBoolField"":true",
@"""MyStringField"":""World"""
};
string expected = @"{
""MyIntProperty"":123,
""MyBoolProperty"":true,
""MyStringProperty"":""Hello"",
""MyIntField"":321,
""MyBoolField"":true,
""MyStringField"":""World""
}";

string json;
var converter = new PrimitiveConverter();
Expand All @@ -491,7 +488,7 @@ public static void ClassWithNullablePrimitivesObjectConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(6, converter.WriteCallCount);
AssertHelper.ValidateJson(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}
{
var obj = JsonSerializer.Deserialize<ClassWithNullablePrimitives>(json, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static void ValueTypedMemberToInterfaceConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(4, converter.WriteCallCount);
Assert.Equal(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}

{
Expand Down Expand Up @@ -240,7 +240,7 @@ public static void ValueTypedMemberToObjectConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(4, converter.WriteCallCount);
Assert.Equal(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}

{
Expand Down Expand Up @@ -272,7 +272,7 @@ public static void NullableValueTypedMemberToInterfaceConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(4, converter.WriteCallCount);
Assert.Equal(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}

{
Expand Down Expand Up @@ -304,7 +304,7 @@ public static void NullableValueTypedMemberToObjectConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(4, converter.WriteCallCount);
Assert.Equal(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}

{
Expand Down Expand Up @@ -334,7 +334,7 @@ public static void NullableValueTypedMemberWithNullsToInterfaceConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(4, converter.WriteCallCount);
Assert.Equal(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}

{
Expand Down Expand Up @@ -367,7 +367,7 @@ public static void NullableValueTypedMemberWithNullsToObjectConverter()
json = JsonSerializer.Serialize(obj, options);

Assert.Equal(4, converter.WriteCallCount);
Assert.Equal(expected, json);
JsonTestHelper.AssertJsonEqual(expected, json);
}

{
Expand All @@ -380,6 +380,5 @@ public static void NullableValueTypedMemberWithNullsToObjectConverter()
Assert.Null(obj.MyRefTypedField);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace System.Text.Json.Serialization.Tests
Expand Down Expand Up @@ -105,5 +103,4 @@ public OtherRTMember(string value)
Value = value;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)System\IO\WrappedMemoryStream.cs" Link="CommonTest\System\IO\WrappedMemoryStream.cs" />
<Compile Include="AssertHelper.cs" />
<Compile Include="BitStackTests.cs" />
<Compile Include="BufferFactory.cs" />
<Compile Include="BufferSegment.cs" />
Expand Down