diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index 08ee8878..088c8140 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -2051,9 +2051,71 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.IsNotNull(obtainedDictionary); } + + [Test] + public void SerializeNullableStructProperty_Auto() + { + JsonSerializerSettings serializerSettings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Auto, + Formatting = Formatting.Indented + }; + + ObjectWithOptionalMessage objWithMessage = new ObjectWithOptionalMessage(new Message2("Hello!")); + + string json = JsonConvert.SerializeObject(objWithMessage, serializerSettings); + + StringAssert.AreEqual(@"{ + ""Message"": { + ""Value"": ""Hello!"" + } +}", json); + } + + [Test] + public void DeserializeNullableStructProperty_Auto() + { + JsonSerializerSettings serializerSettings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Auto, + Formatting = Formatting.Indented + }; + + string json = @"{ + ""Message"": { + ""Value"": ""Hello!"" + } +}"; + ObjectWithOptionalMessage objWithMessage = JsonConvert.DeserializeObject(json, serializerSettings); + + StringAssert.AreEqual("Hello!", objWithMessage.Message.Value.Value); + } #endif } + public struct Message2 + { + public string Value { get; } + + [JsonConstructor] + public Message2(string value) + { + if (value == null) throw new ArgumentNullException(nameof(value)); + + Value = value; + } + } + + public class ObjectWithOptionalMessage + { + public Message2? Message { get; } + + public ObjectWithOptionalMessage(Message2? message) + { + Message = message; + } + } + public class DataType { public DataType() diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index a45c1f01..c0d9f69c 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -1002,14 +1002,14 @@ namespace Newtonsoft.Json.Serialization { if (member != null) { - if (contract.UnderlyingType != member.PropertyContract.CreatedType) + if (contract.NonNullableUnderlyingType != member.PropertyContract.CreatedType) { return true; } } else if (containerContract != null) { - if (containerContract.ItemContract == null || contract.UnderlyingType != containerContract.ItemContract.CreatedType) + if (containerContract.ItemContract == null || contract.NonNullableUnderlyingType != containerContract.ItemContract.CreatedType) { return true; } @@ -1018,7 +1018,7 @@ namespace Newtonsoft.Json.Serialization { JsonContract rootContract = Serializer._contractResolver.ResolveContract(_rootType); - if (contract.UnderlyingType != rootContract.CreatedType) + if (contract.NonNullableUnderlyingType != rootContract.CreatedType) { return true; }