-Increment version to 5.0.7

-Added default value and required support to constructor deserialization
This commit is contained in:
James Newton-King
2013-10-11 20:43:07 +13:00
parent 14a2b3aca8
commit f304f7215f
7 changed files with 84 additions and 13 deletions
@@ -24,6 +24,7 @@
#endregion
using System;
using System.ComponentModel;
#if !(NET35 || NET20 || SILVERLIGHT)
using System.Collections.Concurrent;
#endif
@@ -2468,6 +2469,62 @@ keyword such as type of business.""
Assert.AreEqual("lastName", o.lastName);
}
public sealed class ConstructorAndDefaultValueAttributeTestClass
{
public ConstructorAndDefaultValueAttributeTestClass(string testProperty1)
{
this.TestProperty1 = testProperty1;
}
public string TestProperty1 { get; set; }
[DefaultValue(21)]
public int TestProperty2 { get; set; }
}
[Test]
public void PopulateDefaultValueWhenUsingConstructor()
{
string json = "{ 'testProperty1': 'value' }";
ConstructorAndDefaultValueAttributeTestClass c = JsonConvert.DeserializeObject<ConstructorAndDefaultValueAttributeTestClass>(json, new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Populate
});
Assert.AreEqual("value", c.TestProperty1);
Assert.AreEqual(21, c.TestProperty2);
c = JsonConvert.DeserializeObject<ConstructorAndDefaultValueAttributeTestClass>(json, new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
});
Assert.AreEqual("value", c.TestProperty1);
Assert.AreEqual(21, c.TestProperty2);
}
public sealed class ConstructorAndRequiredTestClass
{
public ConstructorAndRequiredTestClass(string testProperty1)
{
this.TestProperty1 = testProperty1;
}
public string TestProperty1 { get; set; }
[JsonProperty(Required = Required.AllowNull)]
public int TestProperty2 { get; set; }
}
[Test]
public void RequiredWhenUsingConstructor()
{
string json = "{ 'testProperty1': 'value' }";
ExceptionAssert.Throws<JsonSerializationException>("Required property 'TestProperty2' not found in JSON. Path '', line 1, position 28.",
() => JsonConvert.DeserializeObject<ConstructorAndRequiredTestClass>(json));
}
[Test]
public void DeserializePropertiesOnToNonDefaultConstructor()
{