From 9be95e0f6aedf5cdc108b058bf71f0839911e0c9 Mon Sep 17 00:00:00 2001 From: everfor Date: Fri, 29 Nov 2019 14:16:00 -0800 Subject: [PATCH] Do not treat ignored field as missing member when deserializing from overriden json constructor (#2224) --- .../Serialization/JsonSerializerTest.cs | 19 ++++++++- .../JsonSerializerInternalReader.cs | 39 ++++++++++--------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 805bcfdf6..26190d2f7 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -4530,7 +4530,22 @@ public void DeserializeIgnoredPropertyInConstructor() { string json = @"{""First"":""First"",""Second"":2,""Ignored"":{""Name"":""James""},""AdditionalContent"":{""LOL"":true}}"; - ConstructorCompexIgnoredProperty cc = JsonConvert.DeserializeObject(json); + var cc = JsonConvert.DeserializeObject(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( + 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 @@ public void NullableDoubleEmptyValue() "Unexpected character encountered while parsing value: ,. Path 'E', line 1, position 36."); } } -} \ No newline at end of file +} diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 97229a1b0..64c921dca 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -2201,30 +2201,33 @@ private List 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 {