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;
}