Merge branch 'issue/1429' of https://github.com/paulirwin/Newtonsoft.Json into paulirwin-issue/1429
This commit is contained in:
@@ -40,6 +40,20 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class NullValueHandlingTests : TestFixtureBase
|
public class NullValueHandlingTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
private const string MovieNullValueHandlingIncludeExpectedResult = @"{
|
||||||
|
""Name"": ""Bad Boys III"",
|
||||||
|
""Description"": ""It's no Bad Boys"",
|
||||||
|
""Classification"": null,
|
||||||
|
""Studio"": null,
|
||||||
|
""ReleaseDate"": null,
|
||||||
|
""ReleaseCountries"": null
|
||||||
|
}";
|
||||||
|
|
||||||
|
private const string MovieNullValueHandlingIgnoreExpectedResult = @"{
|
||||||
|
""Name"": ""Bad Boys III"",
|
||||||
|
""Description"": ""It's no Bad Boys""
|
||||||
|
}";
|
||||||
|
|
||||||
#if !NET20
|
#if !NET20
|
||||||
[Test]
|
[Test]
|
||||||
public void DeserializeNullIntoDateTime()
|
public void DeserializeNullIntoDateTime()
|
||||||
@@ -110,19 +124,55 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
// "Description": "It's no Bad Boys"
|
// "Description": "It's no Bad Boys"
|
||||||
// }
|
// }
|
||||||
|
|
||||||
StringAssert.AreEqual(@"{
|
StringAssert.AreEqual(MovieNullValueHandlingIncludeExpectedResult, included);
|
||||||
""Name"": ""Bad Boys III"",
|
|
||||||
""Description"": ""It's no Bad Boys"",
|
|
||||||
""Classification"": null,
|
|
||||||
""Studio"": null,
|
|
||||||
""ReleaseDate"": null,
|
|
||||||
""ReleaseCountries"": null
|
|
||||||
}", included);
|
|
||||||
|
|
||||||
StringAssert.AreEqual(@"{
|
StringAssert.AreEqual(MovieNullValueHandlingIgnoreExpectedResult, ignored);
|
||||||
""Name"": ""Bad Boys III"",
|
}
|
||||||
""Description"": ""It's no Bad Boys""
|
|
||||||
}", ignored);
|
[Test]
|
||||||
|
public void JsonObjectNullValueHandlingIgnore()
|
||||||
|
{
|
||||||
|
var movie = new MovieWithJsonObjectNullValueHandlingIgnore
|
||||||
|
{
|
||||||
|
Name = "Bad Boys III",
|
||||||
|
Description = "It's no Bad Boys"
|
||||||
|
};
|
||||||
|
|
||||||
|
string ignored = JsonConvert.SerializeObject(movie,
|
||||||
|
Formatting.Indented,
|
||||||
|
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });
|
||||||
|
|
||||||
|
// {
|
||||||
|
// "Name": "Bad Boys III",
|
||||||
|
// "Description": "It's no Bad Boys"
|
||||||
|
// }
|
||||||
|
|
||||||
|
StringAssert.AreEqual(MovieNullValueHandlingIgnoreExpectedResult, ignored);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void JsonObjectNullValueHandlingInclude()
|
||||||
|
{
|
||||||
|
var movie = new MovieWithJsonObjectNullValueHandlingInclude
|
||||||
|
{
|
||||||
|
Name = "Bad Boys III",
|
||||||
|
Description = "It's no Bad Boys"
|
||||||
|
};
|
||||||
|
|
||||||
|
string included = JsonConvert.SerializeObject(movie,
|
||||||
|
Formatting.Indented,
|
||||||
|
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
||||||
|
|
||||||
|
// {
|
||||||
|
// "Name": "Bad Boys III",
|
||||||
|
// "Description": "It's no Bad Boys",
|
||||||
|
// "Classification": null,
|
||||||
|
// "Studio": null,
|
||||||
|
// "ReleaseDate": null,
|
||||||
|
// "ReleaseCountries": null
|
||||||
|
// }
|
||||||
|
|
||||||
|
StringAssert.AreEqual(MovieNullValueHandlingIncludeExpectedResult, included);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#region License
|
||||||
|
// Copyright (c) 2007 James Newton-King
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person
|
||||||
|
// obtaining a copy of this software and associated documentation
|
||||||
|
// files (the "Software"), to deal in the Software without
|
||||||
|
// restriction, including without limitation the rights to use,
|
||||||
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the
|
||||||
|
// Software is furnished to do so, subject to the following
|
||||||
|
// conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be
|
||||||
|
// included in all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Newtonsoft.Json.Tests.TestObjects
|
||||||
|
{
|
||||||
|
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public class MovieWithJsonObjectNullValueHandlingIgnore
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
public string Classification { get; set; }
|
||||||
|
public string Studio { get; set; }
|
||||||
|
public DateTime? ReleaseDate { get; set; }
|
||||||
|
public List<string> ReleaseCountries { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#region License
|
||||||
|
// Copyright (c) 2007 James Newton-King
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person
|
||||||
|
// obtaining a copy of this software and associated documentation
|
||||||
|
// files (the "Software"), to deal in the Software without
|
||||||
|
// restriction, including without limitation the rights to use,
|
||||||
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the
|
||||||
|
// Software is furnished to do so, subject to the following
|
||||||
|
// conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be
|
||||||
|
// included in all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Newtonsoft.Json.Tests.TestObjects
|
||||||
|
{
|
||||||
|
[JsonObject(ItemNullValueHandling = NullValueHandling.Include)]
|
||||||
|
public class MovieWithJsonObjectNullValueHandlingInclude
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
public string Classification { get; set; }
|
||||||
|
public string Studio { get; set; }
|
||||||
|
public DateTime? ReleaseDate { get; set; }
|
||||||
|
public List<string> ReleaseCountries { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,6 +38,7 @@ namespace Newtonsoft.Json
|
|||||||
// yuck. can't set nullable properties on an attribute in C#
|
// yuck. can't set nullable properties on an attribute in C#
|
||||||
// have to use this approach to get an unset default state
|
// have to use this approach to get an unset default state
|
||||||
internal Required? _itemRequired;
|
internal Required? _itemRequired;
|
||||||
|
internal NullValueHandling? _itemNullValueHandling;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the member serialization.
|
/// Gets or sets the member serialization.
|
||||||
@@ -49,6 +50,16 @@ namespace Newtonsoft.Json
|
|||||||
set => _memberSerialization = value;
|
set => _memberSerialization = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the null value handling.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The null value handling.</value>
|
||||||
|
public NullValueHandling ItemNullValueHandling
|
||||||
|
{
|
||||||
|
get => _itemNullValueHandling ?? default(NullValueHandling);
|
||||||
|
set => _itemNullValueHandling = value;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value that indicates whether the object's properties are required.
|
/// Gets or sets a value that indicates whether the object's properties are required.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1509,6 +1509,11 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (property.NullValueHandling == null && containerAttribute is JsonObjectAttribute objectAttribute)
|
||||||
|
{
|
||||||
|
property.NullValueHandling = objectAttribute._itemNullValueHandling;
|
||||||
|
}
|
||||||
|
|
||||||
if (requiredAttribute != null)
|
if (requiredAttribute != null)
|
||||||
{
|
{
|
||||||
property._required = Required.Always;
|
property._required = Required.Always;
|
||||||
|
|||||||
Reference in New Issue
Block a user