-Added BigInteger support

This commit is contained in:
James Newton-King
2013-03-16 19:06:05 +13:00
parent b81dfd352a
commit 1356c31670
24 changed files with 957 additions and 50 deletions
@@ -28,6 +28,9 @@ using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using System.Text;
using Newtonsoft.Json.Linq;
#if !NETFX_CORE
@@ -175,6 +178,9 @@ namespace Newtonsoft.Json.Tests.Linq
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
new JProperty("TimeSpan", TimeSpan.FromDays(1))
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
,new JProperty("BigInteger", BigInteger.Parse("1"))
#endif
);
dynamic d = o;
@@ -205,6 +211,9 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(d.Decimal == 1.1m);
Assert.IsTrue(d.Decimal != 1.0f);
Assert.IsTrue(d.Decimal != 1.0d);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
Assert.IsTrue(d.Decimal > new BigInteger(0));
#endif
Assert.IsTrue(d.Float == d.Float);
Assert.IsTrue(d.Float > 0);
@@ -218,6 +227,24 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(d.Float == 1.1m);
Assert.IsTrue(d.Float != 1.0f);
Assert.IsTrue(d.Float != 1.0d);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
Assert.IsTrue(d.Float > new BigInteger(0));
#endif
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
Assert.IsTrue(d.BigInteger == d.BigInteger);
Assert.IsTrue(d.BigInteger > 0);
Assert.IsTrue(d.BigInteger > 0.0m);
Assert.IsTrue(d.BigInteger > 0.0f);
Assert.IsTrue(d.BigInteger > null);
Assert.IsTrue(d.BigInteger >= null);
Assert.IsTrue(d.BigInteger < 2);
Assert.IsTrue(d.BigInteger <= 1.1);
Assert.IsTrue(d.BigInteger == 1);
Assert.IsTrue(d.BigInteger == 1m);
Assert.IsTrue(d.BigInteger != 1.1f);
Assert.IsTrue(d.BigInteger != 1.1d);
#endif
Assert.IsTrue(d.Bytes == d.Bytes);
Assert.IsTrue(d.Bytes == Encoding.UTF8.GetBytes("A string lol!"));
@@ -260,6 +287,9 @@ namespace Newtonsoft.Json.Tests.Linq
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
new JProperty("TimeSpan", TimeSpan.FromDays(1))
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
,new JProperty("BigInteger", new BigInteger(100))
#endif
);
dynamic d = o;
@@ -335,6 +365,23 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(null, r.Value);
r += 2;
Assert.AreEqual(null, r.Value);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
r = d.BigInteger + null;
Assert.AreEqual(null, r.Value);
r += 2;
Assert.AreEqual(null, r.Value);
r = d.BigInteger + 1;
Assert.AreEqual(101, (int)r);
r += 2;
Assert.AreEqual(103, (int)r);
r = d.BigInteger + 1.1d;
Assert.AreEqual(101m, (decimal)r);
r += 2;
Assert.AreEqual(103m, (decimal)r);
#endif
#endregion
#region Subtract
@@ -397,6 +444,18 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(null, r.Value);
r -= 2;
Assert.AreEqual(null, r.Value);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
r = d.BigInteger - null;
Assert.AreEqual(null, r.Value);
r -= 2;
Assert.AreEqual(null, r.Value);
r = d.BigInteger - 1.1d;
Assert.AreEqual(99m, (decimal)r);
r -= 2;
Assert.AreEqual(97m, (decimal)r);
#endif
#endregion
#region Multiply
@@ -459,6 +518,18 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(null, r.Value);
r *= 2;
Assert.AreEqual(null, r.Value);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
r = d.BigInteger * 1.1d;
Assert.AreEqual(100m, (decimal)r);
r *= 2;
Assert.AreEqual(200m, (decimal)r);
r = d.BigInteger * null;
Assert.AreEqual(null, r.Value);
r *= 2;
Assert.AreEqual(null, r.Value);
#endif
#endregion
#region Divide
@@ -521,6 +592,18 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(null, r.Value);
r /= 2;
Assert.AreEqual(null, r.Value);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
r = d.BigInteger / 1.1d;
Assert.AreEqual(100m, (decimal)r);
r /= 2;
Assert.AreEqual(50m, (decimal)r);
r = d.BigInteger / null;
Assert.AreEqual(null, r.Value);
r /= 2;
Assert.AreEqual(null, r.Value);
#endif
#endregion
}
@@ -538,6 +621,9 @@ namespace Newtonsoft.Json.Tests.Linq
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
new JProperty("TimeSpan", TimeSpan.FromDays(1))
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
,new JProperty("BigInteger", new BigInteger(100))
#endif
);
dynamic d = o;
@@ -552,6 +638,9 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual("http://json.codeplex.com/", d.Uri.ToString());
Assert.AreEqual("ea27fe1d-0d80-44f2-bf34-4654156fa7af", d.Guid.ToString());
Assert.AreEqual("1.00:00:00", d.TimeSpan.ToString());
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
Assert.AreEqual("100", d.BigInteger.ToString());
#endif
}
[Test]
@@ -633,6 +722,10 @@ namespace Newtonsoft.Json.Tests.Linq
AssertValueConverted<Guid?>(null);
AssertValueConverted<Uri>(new Uri("http://json.codeplex.com/"));
AssertValueConverted<Uri>(null);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
AssertValueConverted<BigInteger>(new BigInteger(100));
AssertValueConverted<BigInteger?>(null);
#endif
}
private static void AssertValueConverted<T>(object value)
@@ -27,6 +27,9 @@ using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using Newtonsoft.Json.Tests.TestObjects;
#if !NETFX_CORE
using NUnit.Framework;
@@ -1628,17 +1631,17 @@ Parameter name: arrayIndex",
});
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
[Test]
public void NumberTooBigForInt64()
{
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Path 'code', line 1, position 30.",
() =>
{
string json = @"{""code"": 307953220000517141511}";
string json = @"{""code"": 307953220000517141511}";
JObject.Parse(json);
});
JObject o = JObject.Parse(json);
BigInteger b = (BigInteger)o["code"];
}
#endif
[Test]
public void ParseIncomplete()
+44 -2
View File
@@ -25,6 +25,9 @@
using System;
using System.Collections.Generic;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using System.Text;
using Newtonsoft.Json.Converters;
#if !NETFX_CORE
@@ -344,6 +347,29 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(data, (byte[])(new JValue(data)));
Assert.AreEqual(5, (int)(new JValue(StringComparison.OrdinalIgnoreCase)));
string bigIntegerText = "1234567899999999999999999999999999999999999999999999999999999999999990";
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
Assert.AreEqual(BigInteger.Parse(bigIntegerText), (BigInteger)(new JValue(bigIntegerText)));
Assert.AreEqual(new BigInteger(long.MaxValue), (BigInteger)(new JValue(long.MaxValue)));
Assert.AreEqual(new BigInteger(4.5d), (BigInteger)(new JValue((4.5d))));
Assert.AreEqual(new BigInteger(4.5f), (BigInteger)(new JValue((4.5f))));
Assert.AreEqual(new BigInteger(byte.MaxValue), (BigInteger)(new JValue(byte.MaxValue)));
Assert.AreEqual(new BigInteger(123), (BigInteger)(new JValue(123)));
byte[] intData = BigInteger.Parse(bigIntegerText).ToByteArray();
Assert.AreEqual(BigInteger.Parse(bigIntegerText), (BigInteger)(new JValue(intData)));
Assert.AreEqual(4.0d, (double)(new JValue(new BigInteger(4.5d))));
Assert.AreEqual(true, (bool)(new JValue(new BigInteger(1))));
Assert.AreEqual(long.MaxValue, (long)(new JValue(new BigInteger(long.MaxValue))));
Assert.AreEqual(long.MaxValue, (long)(new JValue(new BigInteger(new byte[] { 255, 255, 255, 255, 255, 255, 255, 127 }))));
Assert.AreEqual("9223372036854775807", (string)(new JValue(new BigInteger(long.MaxValue))));
intData = (byte[]) (new JValue(new BigInteger(long.MaxValue)));
Assert.AreEqual(new byte[] { 255, 255, 255, 255, 255, 255, 255, 127 }, intData);
#endif
}
[Test]
@@ -394,14 +420,25 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to Guid.", () => { var i = (Guid)new JValue(new Uri("http://www.google.com")); });
#if !NET20
ExceptionAssert.Throws<Exception>("Can not convert Boolean to DateTimeOffset.", () => { var i = (DateTimeOffset)new JValue(true); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to DateTimeOffset.", () => { var i = (DateTimeOffset)new JValue(true); });
#endif
ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); });
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to BigInteger.", () => { var i = (BigInteger)new JValue(new Uri("http://www.google.com")); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to BigInteger.", () => { var i = (BigInteger)new JValue((object)null); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to BigInteger.", () => { var i = (BigInteger)new JValue(Guid.NewGuid()); });
#endif
ExceptionAssert.Throws<Exception>("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); });
}
[Test]
public void ToObject()
{
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
Assert.AreEqual((BigInteger)1, (new JValue(1).ToObject(typeof(BigInteger))));
Assert.AreEqual((BigInteger)1, (new JValue(1).ToObject(typeof(BigInteger?))));
Assert.AreEqual((BigInteger?)null, (new JValue((object)null).ToObject(typeof(BigInteger?))));
#endif
Assert.AreEqual((ushort)1, (new JValue(1).ToObject(typeof(ushort))));
Assert.AreEqual((ushort)1, (new JValue(1).ToObject(typeof(ushort?))));
Assert.AreEqual((uint)1L, (new JValue(1).ToObject(typeof(uint))));
@@ -451,6 +488,11 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(JToken.DeepEquals(new JValue((DateTimeOffset?)null), (JValue)(DateTimeOffset?)null));
#endif
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
Assert.IsTrue(JToken.DeepEquals(new JValue(new BigInteger(1)), (JValue)new BigInteger(1)));
Assert.IsTrue(JToken.DeepEquals(new JValue((BigInteger?)null), (JValue)(BigInteger?)null));
#endif
Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true));
Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true));
Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)(bool?)true));
Assert.IsTrue(JToken.DeepEquals(new JValue((bool?)null), (JValue)(bool?)null));
@@ -26,6 +26,9 @@
using System;
using System.Collections.Generic;
using System.IO;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
@@ -98,6 +101,13 @@ namespace Newtonsoft.Json.Tests.Linq
v.Value = g;
Assert.AreEqual(g, v.Value);
Assert.AreEqual(JTokenType.Guid, v.Type);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
BigInteger i = BigInteger.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990");
v.Value = i;
Assert.AreEqual(i, v.Value);
Assert.AreEqual(JTokenType.Integer, v.Type);
#endif
}
[Test]
@@ -151,8 +161,24 @@ namespace Newtonsoft.Json.Tests.Linq
v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5"));
Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
v = new JValue(BigInteger.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990"));
Assert.AreEqual("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990", v.ToString(null, CultureInfo.InvariantCulture));
#endif
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
[Test]
public void JValueParse()
{
JValue v = (JValue)JToken.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990");
Assert.AreEqual(JTokenType.Integer, v.Type);
Assert.AreEqual(BigInteger.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990"), v.Value);
}
#endif
[Test]
public void Last()
{
@@ -417,6 +443,14 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(Int32.MaxValue, Convert.ToInt32(new JValue(Int32.MaxValue)));
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
[Test]
public void ConvertsToInt32_BigInteger()
{
Assert.AreEqual(123, Convert.ToInt32(new JValue(BigInteger.Parse("123"))));
}
#endif
[Test]
public void ConvertsToChar()
{
@@ -483,6 +517,12 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(Decimal.MaxValue, Convert.ToDecimal(new JValue(Decimal.MaxValue)));
}
[Test]
public void ConvertsToDecimal_Int64()
{
Assert.AreEqual(123, Convert.ToDecimal(new JValue(123)));
}
[Test]
public void ConvertsToString_Decimal()
{