Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/Src
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2011-04-10 06:53:43 +0400
committerJamesNK <james@newtonking.com>2011-04-10 06:53:43 +0400
commit48f3242095d9eb2fcd0b5ddf9bcfd44bbf445ab6 (patch)
treef77f22f1fdcd93c4d31e53bc6986e2ec562d5887 /Src
parent1ab8f8b6867a60889cfe7ae9f78a16a585c0a425 (diff)
-Fixed DefaultValueHandling including a property when the default value is the same value but a different type
Diffstat (limited to 'Src')
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs37
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs1
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs2
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs2
-rw-r--r--Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs22
5 files changed, 62 insertions, 2 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs
index 94be042..9d14e33 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Tests.TestObjects;
@@ -67,5 +68,41 @@ namespace Newtonsoft.Json.Tests.Serialization
Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
Assert.AreEqual(@"{}", json);
}
+
+ [JsonObject]
+ public class NetworkUser
+ {
+ [JsonProperty(PropertyName = "userId")]
+ [DefaultValue(-1)]
+ public long GlobalId { get; set; }
+
+ [JsonProperty(PropertyName = "floatUserId")]
+ [DefaultValue(-1.0d)]
+ public float FloatGlobalId { get; set; }
+
+ [JsonProperty(PropertyName = "firstName")]
+ public string Firstname { get; set; }
+ [JsonProperty(PropertyName = "lastName")]
+ public string Lastname { get; set; }
+
+ public NetworkUser()
+ {
+ GlobalId = -1;
+ FloatGlobalId = -1.0f;
+ }
+ }
+
+ [Test]
+ public void IgnoreNumberTypeDifferencesWithDefaultValue()
+ {
+ NetworkUser user = new NetworkUser
+ {
+ Firstname = "blub"
+ };
+
+ string json = JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore });
+
+ Assert.AreEqual(@"{""firstName"":""blub""}", json);
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
index 9c612d1..bee8774 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -59,6 +59,7 @@ using System.Linq.Expressions;
#endif
#if !(NET35 || NET20 || WINDOWS_PHONE)
using System.Dynamic;
+using System.ComponentModel;
#endif
namespace Newtonsoft.Json.Tests.Serialization
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
index d917918..97b41b5 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
@@ -569,7 +569,7 @@ namespace Newtonsoft.Json.Serialization
if (property.NullValueHandling.GetValueOrDefault(Serializer.NullValueHandling) == NullValueHandling.Ignore && value == null)
return false;
- if (property.DefaultValueHandling.GetValueOrDefault(Serializer.DefaultValueHandling) == DefaultValueHandling.Ignore && Equals(value, property.DefaultValue))
+ if (property.DefaultValueHandling.GetValueOrDefault(Serializer.DefaultValueHandling) == DefaultValueHandling.Ignore && MiscellaneousUtils.ValueEquals(value, property.DefaultValue))
return false;
if (!property.Writable)
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
index c25e995..2c89c2f 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
@@ -184,7 +184,7 @@ namespace Newtonsoft.Json.Serialization
return;
if (property.DefaultValueHandling.GetValueOrDefault(Serializer.DefaultValueHandling) ==
- DefaultValueHandling.Ignore && Equals(memberValue, defaultValue))
+ DefaultValueHandling.Ignore && MiscellaneousUtils.ValueEquals(memberValue, defaultValue))
return;
if (ShouldWriteReference(memberValue, property, contract))
diff --git a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs
index f0fa2dd..84375eb 100644
--- a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs
+++ b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs
@@ -12,6 +12,28 @@ namespace Newtonsoft.Json.Utilities
internal static class MiscellaneousUtils
{
+ public static bool ValueEquals(object objA, object objB)
+ {
+ if (objA == null && objB == null)
+ return true;
+ if (objA != null && objB == null)
+ return false;
+ if (objA == null && objB != null)
+ return false;
+
+ if (objA.GetType() != objB.GetType())
+ {
+ if (ConvertUtils.IsInteger(objA) && ConvertUtils.IsInteger(objB))
+ return Convert.ToDecimal(objA).Equals(Convert.ToDecimal(objB));
+ else if ((objA is double || objA is float || objA is decimal) && (objB is double || objB is float || objB is decimal))
+ return MathUtils.ApproxEquals(Convert.ToDouble(objA), Convert.ToDouble(objB));
+ else
+ return false;
+ }
+
+ return objA.Equals(objB);
+ }
+
public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message)
{
string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);