diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs index 94be042a..9d14e336 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 9c612d18..bee8774b 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 d917918e..97b41b52 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 c25e995e..2c89c2f4 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 f0fa2dda..84375eb7 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);