-Fixed DefaultValueHandling including a property when the default value is the same value but a different type

This commit is contained in:
JamesNK
2011-04-10 02:53:43 +00:00
parent 1ab8f8b686
commit 48f3242095
5 changed files with 62 additions and 2 deletions
@@ -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