Skip to content

Commit

Permalink
Do not treat ignored field as missing member when deserializing from …
Browse files Browse the repository at this point in the history
…overriden json constructor (#2224)
  • Loading branch information
everfor authored and JamesNK committed Nov 29, 2019
1 parent 7c3d7f8 commit 9be95e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
19 changes: 17 additions & 2 deletions Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4530,7 +4530,22 @@ public void DeserializeIgnoredPropertyInConstructor()
{
string json = @"{""First"":""First"",""Second"":2,""Ignored"":{""Name"":""James""},""AdditionalContent"":{""LOL"":true}}";

ConstructorCompexIgnoredProperty cc = JsonConvert.DeserializeObject<ConstructorCompexIgnoredProperty>(json);
var cc = JsonConvert.DeserializeObject<ConstructorCompexIgnoredProperty>(json);
Assert.AreEqual("First", cc.First);
Assert.AreEqual(2, cc.Second);
Assert.AreEqual(null, cc.Ignored);
}

[Test]
public void DeserializeIgnoredPropertyInConstructorWithoutThrowingMissingMemberError()
{
string json = @"{""First"":""First"",""Second"":2,""Ignored"":{""Name"":""James""}}";

var cc = JsonConvert.DeserializeObject<ConstructorCompexIgnoredProperty>(
json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Error
});
Assert.AreEqual("First", cc.First);
Assert.AreEqual(2, cc.Second);
Assert.AreEqual(null, cc.Ignored);
Expand Down Expand Up @@ -7965,4 +7980,4 @@ public void NullableDoubleEmptyValue()
"Unexpected character encountered while parsing value: ,. Path 'E', line 1, position 36.");
}
}
}
}
39 changes: 21 additions & 18 deletions Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2201,30 +2201,33 @@ private List<CreatorPropertyContext> ResolvePropertyAndCreatorValues(JsonObjectC
propertyValues.Add(creatorPropertyContext);

JsonProperty? property = creatorPropertyContext.ConstructorProperty ?? creatorPropertyContext.Property;
if (property != null && !property.Ignored)
if (property != null)
{
if (property.PropertyContract == null)
if (!property.Ignored)
{
property.PropertyContract = GetContractSafe(property.PropertyType);
}
if (property.PropertyContract == null)
{
property.PropertyContract = GetContractSafe(property.PropertyType);
}

JsonConverter? propertyConverter = GetConverter(property.PropertyContract, property.Converter, contract, containerProperty);
JsonConverter? propertyConverter = GetConverter(property.PropertyContract, property.Converter, contract, containerProperty);

if (!reader.ReadForType(property.PropertyContract, propertyConverter != null))
{
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
}
if (!reader.ReadForType(property.PropertyContract, propertyConverter != null))
{
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
}

if (propertyConverter != null && propertyConverter.CanRead)
{
creatorPropertyContext.Value = DeserializeConvertable(propertyConverter, reader, property.PropertyType!, null);
}
else
{
creatorPropertyContext.Value = CreateValueInternal(reader, property.PropertyType, property.PropertyContract, property, contract, containerProperty, null);
if (propertyConverter != null && propertyConverter.CanRead)
{
creatorPropertyContext.Value = DeserializeConvertable(propertyConverter, reader, property.PropertyType!, null);
}
else
{
creatorPropertyContext.Value = CreateValueInternal(reader, property.PropertyType, property.PropertyContract, property, contract, containerProperty, null);
}

continue;
}

continue;
}
else
{
Expand Down

0 comments on commit 9be95e0

Please sign in to comment.