-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
@@ -25,6 +25,9 @@
using System;
using System.Globalization;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
@@ -1313,5 +1316,29 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(badText, (string)o["test"]);
}
#endif
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
public class BigIntegerTestClass
{
public BigInteger Blah { get; set; }
}
[Test]
public void WriteBigInteger()
{
BigInteger i = BigInteger.Parse("1999999999999999999999999999999999999999999999999999999999990");
byte[] data = MiscellaneousUtils.HexToBytes("2A-00-00-00-05-42-6C-61-68-00-1A-00-00-00-00-F6-FF-FF-FF-FF-FF-FF-1F-B2-21-CB-28-59-84-C4-AE-03-8A-44-34-2F-4C-4E-9E-3E-01-00");
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms);
JsonSerializer serializer = new JsonSerializer();
BigIntegerTestClass c = serializer.Deserialize<BigIntegerTestClass>(reader);
Assert.AreEqual(i, c.Blah);
}
#endif
}
}
@@ -25,6 +25,9 @@
using System;
using System.Collections.Generic;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
@@ -680,5 +683,42 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual("10-00-00-00-09-30-00-C8-88-07-6B-DC-00-00-00-00", (BitConverter.ToString(ms.ToArray())));
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
[Test]
public void WriteBigInteger()
{
BigInteger i = BigInteger.Parse("1999999999999999999999999999999999999999999999999999999999990");
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartObject();
writer.WritePropertyName("Blah");
writer.WriteValue(i);
writer.WriteEndObject();
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
Assert.AreEqual("2A-00-00-00-05-42-6C-61-68-00-1A-00-00-00-00-F6-FF-FF-FF-FF-FF-FF-1F-B2-21-CB-28-59-84-C4-AE-03-8A-44-34-2F-4C-4E-9E-3E-01-00", bson);
ms.Seek(0, SeekOrigin.Begin);
BsonReader reader = new BsonReader(ms);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
Assert.AreEqual(new byte[] { 246, 255, 255, 255, 255, 255, 255, 31, 178, 33, 203, 40, 89, 132, 196, 174, 3, 138, 68, 52, 47, 76, 78, 158, 62, 1 }, reader.Value);
Assert.AreEqual(i, new BigInteger((byte[])reader.Value));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
}
#endif
}
}
@@ -29,6 +29,9 @@ using System.IO;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#endif
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
@@ -39,6 +42,7 @@ using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAtt
#endif
using System.Xml;
using System.Xml.Schema;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Utilities;
using ValidationEventArgs = Newtonsoft.Json.Schema.ValidationEventArgs;
@@ -348,6 +352,51 @@ namespace Newtonsoft.Json.Tests
Assert.IsNotNull(validationEventArgs);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
[Test]
public void IntegerGreaterThanMaximumValue_BigInteger()
{
string schemaJson = @"{
""type"":""integer"",
""maximum"":5
}";
string json = "99999999999999999999999999999999999999999999999999999999999999999999";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual("Integer 99999999999999999999999999999999999999999999999999999999999999999999 exceeds maximum value of 5. Line 1, position 68.", validationEventArgs.Message);
Assert.AreEqual("", validationEventArgs.Path);
Assert.IsNotNull(validationEventArgs);
}
[Test]
public void IntegerLessThanMaximumValue_BigInteger()
{
string schemaJson = @"{
""type"":""integer"",
""minimum"":5
}";
JValue v = new JValue(new BigInteger(1));
Json.Schema.ValidationEventArgs validationEventArgs = null;
v.Validate(JsonSchema.Parse(schemaJson), (sender, args) => { validationEventArgs = args; });
Assert.IsNotNull(validationEventArgs);
Assert.AreEqual("Integer 1 is less than minimum value of 5.", validationEventArgs.Message);
Assert.AreEqual("", validationEventArgs.Path);
}
#endif
[Test]
public void ThrowExceptionWhenNoValidationEventHandler()
{
@@ -518,7 +567,7 @@ namespace Newtonsoft.Json.Tests
}
[Test]
public void FloatExceedsMaxDecimalPlaces()
public void FloatDivisibleBy()
{
string schemaJson = @"{
""type"":""array"",
@@ -557,6 +606,123 @@ namespace Newtonsoft.Json.Tests
Assert.IsNotNull(validationEventArgs);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
[Test]
public void BigIntegerDivisibleBy_Success()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""divisibleBy"":2
}
}";
string json = "[999999999999999999999999999999999999999999999999999999998]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsNull(validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
}
[Test]
public void BigIntegerDivisibleBy_Failure()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""divisibleBy"":2
}
}";
string json = "[999999999999999999999999999999999999999999999999999999999]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(@"Integer 999999999999999999999999999999999999999999999999999999999 is not evenly divisible by 2. Line 1, position 58.", validationEventArgs.Message);
Assert.AreEqual("[0]", validationEventArgs.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNotNull(validationEventArgs);
}
[Test]
public void BigIntegerDivisibleBy_Fraction()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""divisibleBy"":1.1
}
}";
string json = "[999999999999999999999999999999999999999999999999999999999]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsNotNull(validationEventArgs);
Assert.AreEqual(@"Integer 999999999999999999999999999999999999999999999999999999999 is not evenly divisible by 1.1. Line 1, position 58.", validationEventArgs.Message);
Assert.AreEqual("[0]", validationEventArgs.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
}
[Test]
public void BigIntegerDivisibleBy_FractionWithZeroValue()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""divisibleBy"":1.1
}
}";
JArray a = new JArray(new JValue(new BigInteger(0)));
ValidationEventArgs validationEventArgs = null;
a.Validate(JsonSchema.Parse(schemaJson), (sender, args) => { validationEventArgs = args; });
Assert.IsNull(validationEventArgs);
}
#endif
[Test]
public void IntValidForNumber()
{
@@ -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()
{
@@ -50,7 +50,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\Net40\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;NET40</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
@@ -60,7 +60,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\Net40\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;NET40</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
@@ -86,6 +86,7 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
@@ -87,6 +87,7 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
@@ -39,6 +39,7 @@
<Reference Include="System.Data.Entity" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Drawing" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
@@ -43,6 +43,8 @@ using System.Security;
[assembly: AssemblyTitle("Json.NET Tests .NET 2.0")]
#elif NET35
[assembly: AssemblyTitle("Json.NET Tests .NET 3.5")]
#elif NET40
[assembly: AssemblyTitle("Json.NET Tests .NET 4.0")]
#else
[assembly: AssemblyTitle("Json.NET Tests")]
#endif
@@ -73,4 +75,4 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.1.15913")]
[assembly: AssemblyFileVersion("5.0.1.15916")]
@@ -28,6 +28,9 @@ using System;
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
#if !SILVERLIGHT && !NET20 && !NETFX_CORE
using System.ComponentModel.DataAnnotations;
using System.Configuration;
@@ -7617,6 +7620,19 @@ Parameter name: value",
}
#endif
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
public void SerializeBigInteger()
{
BigInteger i = BigInteger.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990");
string json = JsonConvert.SerializeObject(new [] {i}, Formatting.Indented);
Assert.AreEqual(@"[
123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990
]", json);
}
#endif
#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
[Test]
public void DeserializeReadOnlyList()