-Fixed DefaultValueHandling.Ignore with decimal properties

-Fixed approximate floating point comparisons
This commit is contained in:
James Newton-King
2012-06-09 13:54:30 +12:00
parent 55854f1682
commit 1b972cd65f
6 changed files with 90 additions and 4 deletions
@@ -26,6 +26,7 @@
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
using System;
#if !NETFX_CORE
using System.Collections.Generic;
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
@@ -233,6 +234,42 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(54.99, (double)table.Rows[1]["price"], 0.01);
Assert.IsInstanceOfType(typeof(System.DBNull), table.Rows[2]["price"]);
}
[Test]
public void SerializeKeyValuePairWithDataTableKey()
{
DataTable table = new DataTable();
DataColumn idColumn = new DataColumn("id", typeof(int));
idColumn.AutoIncrement = true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
DataRow r = table.NewRow();
r["item"] = "item!";
r.EndEdit();
table.Rows.Add(r);
KeyValuePair<DataTable, int> pair = new KeyValuePair<DataTable, int>(table, 1);
string serializedpair = JsonConvert.SerializeObject(pair, Formatting.Indented);
Assert.AreEqual(@"{
""Key"": [
{
""id"": 0,
""item"": ""item!""
}
],
""Value"": 1
}", serializedpair);
var pair2 = (KeyValuePair<DataTable, int>)JsonConvert.DeserializeObject(serializedpair, typeof(KeyValuePair<DataTable, int>));
Assert.AreEqual(1, pair2.Value);
Assert.AreEqual(1, pair2.Key.Rows.Count);
Assert.AreEqual("item!", pair2.Key.Rows[0]["item"]);
}
}
}
#endif
@@ -1314,6 +1314,27 @@ namespace Newtonsoft.Json.Tests.Converters
}", json);
}
[Test]
public void EmtpyElementWithArrayAttributeShouldWriteElement()
{
string xml = @"<root>
<Reports d1p1:Array=""true"" xmlns:d1p1=""http://james.newtonking.com/projects/json"" />
</root>";
XmlDocument d = new XmlDocument();
d.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(d, Formatting.Indented);
Assert.AreEqual(@"{
""root"": {
""Reports"": [
{}
]
}
}", json);
}
[Test]
public void DeserializeNonInt64IntegerValues()
{
@@ -72,5 +72,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.5.6.15008")]
[assembly: AssemblyFileVersion("4.5.6.15009")]
#endif
@@ -33,6 +33,7 @@ using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#endif
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Tests.Serialization
{
@@ -123,6 +124,14 @@ namespace Newtonsoft.Json.Tests.Serialization
[DefaultValue(-1)]
public long GlobalId { get; set; }
[JsonProperty(PropertyName = "age")]
[DefaultValue(0)]
public int Age { get; set; }
[JsonProperty(PropertyName = "amount")]
[DefaultValue(0.0)]
public decimal Amount { get; set; }
[JsonProperty(PropertyName = "floatUserId")]
[DefaultValue(-1.0d)]
public float FloatGlobalId { get; set; }
@@ -136,6 +145,8 @@ namespace Newtonsoft.Json.Tests.Serialization
{
GlobalId = -1;
FloatGlobalId = -1.0f;
Amount = 0.0m;
Age = 0;
}
}
@@ -151,5 +162,15 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(@"{""firstName"":""blub""}", json);
}
[Test]
public void ApproxEquals()
{
Assert.IsTrue(MathUtils.ApproxEquals(0.0, 0.0));
Assert.IsTrue(MathUtils.ApproxEquals(1000.0, 1000.0000000000001));
Assert.IsFalse(MathUtils.ApproxEquals(1000.0, 1000.000000000001));
Assert.IsFalse(MathUtils.ApproxEquals(0.0, 0.00001));
}
}
}
@@ -90,7 +90,7 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.5.6.15008")]
[assembly: AssemblyFileVersion("4.5.6.15009")]
#endif
[assembly: CLSCompliant(true)]
+9 -2
View File
@@ -83,8 +83,15 @@ namespace Newtonsoft.Json.Utilities
public static bool ApproxEquals(double d1, double d2)
{
// are values equal to within 6 (or so) digits of precision?
return Math.Abs(d1 - d2) < (Math.Abs(d1) * 1e-6);
const double epsilon = 2.2204460492503131E-16;
if (d1 == d2)
return true;
double tolerance = ((Math.Abs(d1) + Math.Abs(d2)) + 10.0) * epsilon;
double difference = d1 - d2;
return (-tolerance < difference && tolerance > difference);
}
}
}