-Added Populate and IgnoreAndPopulate options to DefaultValueHandling for automatically populating default values during deserialization
This commit is contained in:
@@ -631,6 +631,21 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
],
|
||||
""default"": ""Default!""
|
||||
},
|
||||
""DefaultValueHandlingPopulateProperty"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
],
|
||||
""default"": ""Default!""
|
||||
},
|
||||
""DefaultValueHandlingIgnoreAndPopulateProperty"": {
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
],
|
||||
""default"": ""Default!""
|
||||
},
|
||||
""NullValueHandlingIgnoreProperty"": {
|
||||
""type"": [
|
||||
""string"",
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultValueAttributeTest()
|
||||
public void SerializeDefaultValueAttributeTest()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass(),
|
||||
Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
|
||||
@@ -69,6 +69,24 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(@"{}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDefaultValueAttributeTest()
|
||||
{
|
||||
string json = "{}";
|
||||
|
||||
DefaultValueAttributeTestClass c = JsonConvert.DeserializeObject<DefaultValueAttributeTestClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
DefaultValueHandling = DefaultValueHandling.Populate
|
||||
});
|
||||
Assert.AreEqual("TestProperty1Value", c.TestProperty1);
|
||||
|
||||
c = JsonConvert.DeserializeObject<DefaultValueAttributeTestClass>(json, new JsonSerializerSettings
|
||||
{
|
||||
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
|
||||
});
|
||||
Assert.AreEqual("TestProperty1Value", c.TestProperty1);
|
||||
}
|
||||
|
||||
[JsonObject]
|
||||
public class NetworkUser
|
||||
{
|
||||
|
||||
@@ -1300,16 +1300,19 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonPropertyWithHandlingValues()
|
||||
public void SerializeJsonPropertyWithHandlingValues()
|
||||
{
|
||||
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
||||
o.DefaultValueHandlingIgnoreProperty = "Default!";
|
||||
o.DefaultValueHandlingIncludeProperty = "Default!";
|
||||
o.DefaultValueHandlingPopulateProperty = "Default!";
|
||||
o.DefaultValueHandlingIgnoreAndPopulateProperty = "Default!";
|
||||
|
||||
string json = JsonConvert.SerializeObject(o, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""DefaultValueHandlingIncludeProperty"": ""Default!"",
|
||||
""DefaultValueHandlingPopulateProperty"": ""Default!"",
|
||||
""NullValueHandlingIncludeProperty"": null,
|
||||
""ReferenceLoopHandlingErrorProperty"": null,
|
||||
""ReferenceLoopHandlingIgnoreProperty"": null,
|
||||
@@ -1320,10 +1323,23 @@ keyword such as type of business.""
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""DefaultValueHandlingIncludeProperty"": ""Default!"",
|
||||
""DefaultValueHandlingPopulateProperty"": ""Default!"",
|
||||
""NullValueHandlingIncludeProperty"": null
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeJsonPropertyWithHandlingValues()
|
||||
{
|
||||
string json = "{}";
|
||||
|
||||
JsonPropertyWithHandlingValues o = JsonConvert.DeserializeObject<JsonPropertyWithHandlingValues>(json);
|
||||
Assert.AreEqual("Default!", o.DefaultValueHandlingIgnoreAndPopulateProperty);
|
||||
Assert.AreEqual("Default!", o.DefaultValueHandlingPopulateProperty);
|
||||
Assert.AreEqual(null, o.DefaultValueHandlingIgnoreProperty);
|
||||
Assert.AreEqual(null, o.DefaultValueHandlingIncludeProperty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException))]
|
||||
public void JsonPropertyWithHandlingValues_ReferenceLoopError()
|
||||
|
||||
@@ -37,6 +37,14 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
[DefaultValue("Default!")]
|
||||
public string DefaultValueHandlingIncludeProperty { get; set; }
|
||||
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
|
||||
[DefaultValue("Default!")]
|
||||
public string DefaultValueHandlingPopulateProperty { get; set; }
|
||||
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
|
||||
[DefaultValue("Default!")]
|
||||
public string DefaultValueHandlingIgnoreAndPopulateProperty { get; set; }
|
||||
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string NullValueHandlingIgnoreProperty { get; set; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user