Skip to content

Commit

Permalink
-Fixed DefaultValueHandling.Ignore with decimal properties
Browse files Browse the repository at this point in the history
-Fixed approximate floating point comparisons
  • Loading branch information
JamesNK committed Jun 9, 2012
1 parent 55854f1 commit 1b972cd
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
37 changes: 37 additions & 0 deletions Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -233,6 +234,42 @@ public void DerializeDataTableWithExplicitNull()
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
21 changes: 21 additions & 0 deletions Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,27 @@ public void EmtpyElementWithArrayAttributeShouldWriteAttributes()
}", 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()
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#endif
using Newtonsoft.Json.Utilities;

namespace Newtonsoft.Json.Tests.Serialization
{
Expand Down Expand Up @@ -123,6 +124,14 @@ public class NetworkUser
[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; }
Expand All @@ -136,6 +145,8 @@ public NetworkUser()
{
GlobalId = -1;
FloatGlobalId = -1.0f;
Amount = 0.0m;
Age = 0;
}
}

Expand All @@ -151,5 +162,15 @@ public void IgnoreNumberTypeDifferencesWithDefaultValue()

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));
}
}
}
2 changes: 1 addition & 1 deletion Src/Newtonsoft.Json/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
// 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)]
11 changes: 9 additions & 2 deletions Src/Newtonsoft.Json/Utilities/MathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ public static char IntToHex(int n)

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);
}
}
}

0 comments on commit 1b972cd

Please sign in to comment.