-Fixed DefaultValueHandling including a property when the default value is the same value but a different type
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user