From f78b07660215aa61e87487fa48e12ed9a9b22a31 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 20 Jun 2014 22:31:40 +1200 Subject: [PATCH] -Refactored property default value creation --- .../Serialization/ContractResolverTests.cs | 29 ++++++++++++++++ .../Serialization/JsonProperty.cs | 33 ++++++++++++++++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs index 5dd5c424..8b882f0c 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs @@ -108,6 +108,35 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] 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] public void ListInterface() { diff --git a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs index 58206c09..32cbfb07 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs @@ -41,8 +41,10 @@ namespace Newtonsoft.Json.Serialization internal bool _hasExplicitDefaultValue; internal object _defaultValue; + private bool _hasGeneratedDefaultValue; private string _propertyName; private bool _skipPropertyNameEscape; + private Type _propertyType; // use to cache contract during deserialization internal JsonContract PropertyContract { get; set; } @@ -109,7 +111,18 @@ namespace Newtonsoft.Json.Serialization /// Gets or sets the type of the property. /// /// The type of the property. - public Type PropertyType { get; set; } + public Type PropertyType + { + get { return _propertyType; } + set + { + if (_propertyType != value) + { + _propertyType = value; + _hasGeneratedDefaultValue = false; + } + } + } /// /// Gets or sets the for the property. @@ -154,7 +167,13 @@ namespace Newtonsoft.Json.Serialization /// The default value. public object DefaultValue { - get { return _defaultValue; } + get + { + if (!_hasExplicitDefaultValue) + return null; + + return _defaultValue; + } set { _hasExplicitDefaultValue = true; @@ -164,8 +183,14 @@ namespace Newtonsoft.Json.Serialization internal object GetResolvedDefaultValue() { - if (!_hasExplicitDefaultValue && PropertyType != null) - return ReflectionUtils.GetDefaultValue(PropertyType); + if (_propertyType == null) + return null; + + if (!_hasExplicitDefaultValue && !_hasGeneratedDefaultValue) + { + _defaultValue = ReflectionUtils.GetDefaultValue(PropertyType); + _hasGeneratedDefaultValue = true; + } return _defaultValue; }