From 7398fe6b55eaf0e63f1298c4868eb9c439b5328d Mon Sep 17 00:00:00 2001 From: Samuel Sousa Date: Fri, 18 Oct 2024 00:00:00 -0300 Subject: [PATCH 1/3] Deserializing with NullValueHandling.Ignore ignore null values --- .../Serialization/JsonSerializerInternalReader.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 9fa364663..221e80848 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -975,6 +975,11 @@ private bool HasNoDefinedType(JsonContract? contract) return null; } + if (value == null && Serializer.NullValueHandling == NullValueHandling.Ignore) + { + return Activator.CreateInstance(targetType); + } + try { if (contract.IsConvertable) From d73da92a75b0be32766bc67c33930b4a7db31fee Mon Sep 17 00:00:00 2001 From: Samuel Sousa Date: Fri, 18 Oct 2024 17:09:13 -0300 Subject: [PATCH 2/3] Related tests added --- Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs new file mode 100644 index 000000000..c27ebb33f --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs @@ -0,0 +1,49 @@ +using System.Linq; +using Newtonsoft.Json.Linq; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +using TestCase = Xunit.InlineDataAttribute; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests.Issues +{ + public class Issue2954 + { + private const string JsonToTest = "{\"arg\": 1, \"field\": null, \"fieldEnum\": null}"; + [Test] + //https://github.com/JamesNK/Newtonsoft.Json/issues/2954 + public void Test_Ignore_Null_Values() + { + var result = JsonConvert.DeserializeObject(JsonToTest, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + + Assert.AreEqual(result.field, 0); + Assert.AreEqual(result.fieldEnum, B.Option1); + } + + [Test] + public void Test_Throw_JsonSerializationException() + { + ExceptionAssert.Throws(() => JsonConvert.DeserializeObject(JsonToTest)); + } + + class A + { + public A(int arg) + { + } + + public int field; + public B fieldEnum; + } + + enum B + { + Option1, + Option2, + } + } +} From 20d48d8c8b85071825aaee163e864cbcbf7f0bc4 Mon Sep 17 00:00:00 2001 From: Samuel Sousa Date: Fri, 18 Oct 2024 17:43:01 -0300 Subject: [PATCH 3/3] Refactoring the test --- Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs index c27ebb33f..0aa60cebe 100644 --- a/Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue2954.cs @@ -14,14 +14,15 @@ namespace Newtonsoft.Json.Tests.Issues public class Issue2954 { private const string JsonToTest = "{\"arg\": 1, \"field\": null, \"fieldEnum\": null}"; + [Test] //https://github.com/JamesNK/Newtonsoft.Json/issues/2954 public void Test_Ignore_Null_Values() { var result = JsonConvert.DeserializeObject(JsonToTest, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); - Assert.AreEqual(result.field, 0); - Assert.AreEqual(result.fieldEnum, B.Option1); + Assert.AreEqual(result.Field, 0); + Assert.AreEqual(result.FieldEnum, B.Option1); } [Test] @@ -36,8 +37,8 @@ public A(int arg) { } - public int field; - public B fieldEnum; + public int Field { get; set; } + public B FieldEnum { get; set; } } enum B