Stops including the typename for nullable structs using TypeNameHandling.Auto, fixes #1050
This commit is contained in:
@@ -1934,9 +1934,50 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
|
|
||||||
Assert.IsNotNull(obtainedDictionary);
|
Assert.IsNotNull(obtainedDictionary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeNullableStructProperty_Auto()
|
||||||
|
{
|
||||||
|
ObjectWithOptionalMessage objWithMessage = new ObjectWithOptionalMessage(new Message2("Hello!"));
|
||||||
|
|
||||||
|
JsonSerializerSettings serializerSettings = new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
TypeNameHandling = TypeNameHandling.Auto,
|
||||||
|
Formatting = Formatting.Indented
|
||||||
|
};
|
||||||
|
|
||||||
|
StringAssert.AreEqual(@"{
|
||||||
|
""Message"": {
|
||||||
|
""Value"": ""Hello!""
|
||||||
|
}
|
||||||
|
}", JsonConvert.SerializeObject(objWithMessage, serializerSettings));
|
||||||
|
}
|
||||||
#endif
|
#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 class DataType
|
||||||
{
|
{
|
||||||
public DataType()
|
public DataType()
|
||||||
|
|||||||
@@ -1002,14 +1002,14 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
if (member != null)
|
if (member != null)
|
||||||
{
|
{
|
||||||
if (contract.UnderlyingType != member.PropertyContract.CreatedType)
|
if (contract.NonNullableUnderlyingType != member.PropertyContract.CreatedType)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (containerContract != null)
|
else if (containerContract != null)
|
||||||
{
|
{
|
||||||
if (containerContract.ItemContract == null || contract.UnderlyingType != containerContract.ItemContract.CreatedType)
|
if (containerContract.ItemContract == null || contract.NonNullableUnderlyingType != containerContract.ItemContract.CreatedType)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1018,7 +1018,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
JsonContract rootContract = Serializer._contractResolver.ResolveContract(_rootType);
|
JsonContract rootContract = Serializer._contractResolver.ResolveContract(_rootType);
|
||||||
|
|
||||||
if (contract.UnderlyingType != rootContract.CreatedType)
|
if (contract.NonNullableUnderlyingType != rootContract.CreatedType)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user