-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
@@ -75,4 +75,4 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.6.16606")]
[assembly: AssemblyFileVersion("5.0.7.16611")]
@@ -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()
{
@@ -92,6 +92,6 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.6.16606")]
[assembly: AssemblyFileVersion("5.0.7.16611")]
[assembly: CLSCompliant(true)]
@@ -1444,6 +1444,11 @@ To fix this error either change the environment to be fully trusted, change the
{
ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo");
// only need to keep a track of properies presence if they are required or a value should be defaulted if missing
Dictionary<JsonProperty, PropertyPresence> propertiesPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate))
? contract.Properties.ToDictionary(m => m, m => PropertyPresence.None)
: null;
Type objectType = contract.UnderlyingType;
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
@@ -1461,6 +1466,14 @@ To fix this error either change the environment to be fully trusted, change the
constructorParameters[matchingConstructorParameter] = propertyValue.Value;
else
remainingPropertyValues.Add(propertyValue);
if (propertiesPresence != null)
{
// map from constructor property to normal property
var property = propertiesPresence.Keys.FirstOrDefault(p => p.PropertyName == propertyValue.Key.PropertyName);
if (property != null)
propertiesPresence[property] = (propertyValue.Value == null) ? PropertyPresence.Null : PropertyPresence.Value;
}
}
object createdObject = constructorInfo.Invoke(constructorParameters.Values.ToArray());
@@ -1520,6 +1533,8 @@ To fix this error either change the environment to be fully trusted, change the
}
}
EndObject(createdObject, reader, contract, reader.Depth, propertiesPresence);
OnDeserialized(reader, contract, createdObject);
return createdObject;
}