-Refactored property default value creation
This commit is contained in:
@@ -108,6 +108,35 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class ContractResolverTests : TestFixtureBase
|
public class ContractResolverTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
[Test]
|
||||||
|
public void JsonPropertyDefaultValue()
|
||||||
|
{
|
||||||
|
JsonProperty p = new JsonProperty();
|
||||||
|
|
||||||
|
Assert.AreEqual(null, p.GetResolvedDefaultValue());
|
||||||
|
Assert.AreEqual(null, p.DefaultValue);
|
||||||
|
|
||||||
|
p.PropertyType = typeof(int);
|
||||||
|
|
||||||
|
Assert.AreEqual(0 , p.GetResolvedDefaultValue());
|
||||||
|
Assert.AreEqual(null, p.DefaultValue);
|
||||||
|
|
||||||
|
p.PropertyType = typeof(DateTime);
|
||||||
|
|
||||||
|
Assert.AreEqual(new DateTime(), p.GetResolvedDefaultValue());
|
||||||
|
Assert.AreEqual(null, p.DefaultValue);
|
||||||
|
|
||||||
|
p.PropertyType = null;
|
||||||
|
|
||||||
|
Assert.AreEqual(null, p.GetResolvedDefaultValue());
|
||||||
|
Assert.AreEqual(null, p.DefaultValue);
|
||||||
|
|
||||||
|
p.PropertyType = typeof(CompareOptions);
|
||||||
|
|
||||||
|
Assert.AreEqual(CompareOptions.None, (CompareOptions)p.GetResolvedDefaultValue());
|
||||||
|
Assert.AreEqual(null, p.DefaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ListInterface()
|
public void ListInterface()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -41,8 +41,10 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
internal bool _hasExplicitDefaultValue;
|
internal bool _hasExplicitDefaultValue;
|
||||||
internal object _defaultValue;
|
internal object _defaultValue;
|
||||||
|
|
||||||
|
private bool _hasGeneratedDefaultValue;
|
||||||
private string _propertyName;
|
private string _propertyName;
|
||||||
private bool _skipPropertyNameEscape;
|
private bool _skipPropertyNameEscape;
|
||||||
|
private Type _propertyType;
|
||||||
|
|
||||||
// use to cache contract during deserialization
|
// use to cache contract during deserialization
|
||||||
internal JsonContract PropertyContract { get; set; }
|
internal JsonContract PropertyContract { get; set; }
|
||||||
@@ -109,7 +111,18 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
/// Gets or sets the type of the property.
|
/// Gets or sets the type of the property.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The type of the property.</value>
|
/// <value>The type of the property.</value>
|
||||||
public Type PropertyType { get; set; }
|
public Type PropertyType
|
||||||
|
{
|
||||||
|
get { return _propertyType; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_propertyType != value)
|
||||||
|
{
|
||||||
|
_propertyType = value;
|
||||||
|
_hasGeneratedDefaultValue = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the <see cref="JsonConverter" /> for the property.
|
/// Gets or sets the <see cref="JsonConverter" /> for the property.
|
||||||
@@ -154,7 +167,13 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
/// <value>The default value.</value>
|
/// <value>The default value.</value>
|
||||||
public object DefaultValue
|
public object DefaultValue
|
||||||
{
|
{
|
||||||
get { return _defaultValue; }
|
get
|
||||||
|
{
|
||||||
|
if (!_hasExplicitDefaultValue)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return _defaultValue;
|
||||||
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
_hasExplicitDefaultValue = true;
|
_hasExplicitDefaultValue = true;
|
||||||
@@ -164,8 +183,14 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
internal object GetResolvedDefaultValue()
|
internal object GetResolvedDefaultValue()
|
||||||
{
|
{
|
||||||
if (!_hasExplicitDefaultValue && PropertyType != null)
|
if (_propertyType == null)
|
||||||
return ReflectionUtils.GetDefaultValue(PropertyType);
|
return null;
|
||||||
|
|
||||||
|
if (!_hasExplicitDefaultValue && !_hasGeneratedDefaultValue)
|
||||||
|
{
|
||||||
|
_defaultValue = ReflectionUtils.GetDefaultValue(PropertyType);
|
||||||
|
_hasGeneratedDefaultValue = true;
|
||||||
|
}
|
||||||
|
|
||||||
return _defaultValue;
|
return _defaultValue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user