Do not treat ignored field as missing member when deserializing from overriden json constructor (#2224)

This commit is contained in:
everfor
2019-11-30 11:16:00 +13:00
committed by James Newton-King
parent 7c3d7f8da7
commit 9be95e0f6a
2 changed files with 38 additions and 20 deletions
@@ -4530,7 +4530,22 @@ Path '', line 1, position 1.");
{
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);
@@ -7965,4 +7980,4 @@ This is just junk, though.";
"Unexpected character encountered while parsing value: ,. Path 'E', line 1, position 36.");
}
}
}
}
@@ -2201,30 +2201,33 @@ namespace Newtonsoft.Json.Serialization
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);
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;
}
else
{
creatorPropertyContext.Value = CreateValueInternal(reader, property.PropertyType, property.PropertyContract, property, contract, containerProperty, null);
}
continue;
}
else
{