-Removed external API references to BigInteger

This commit is contained in:
James Newton-King
2013-04-08 21:12:44 +12:00
parent ce50f0f9e4
commit 2412c99d16
10 changed files with 127 additions and 125 deletions
@@ -1632,18 +1632,6 @@ Parameter name: arrayIndex",
});
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
[Test]
public void NumberTooBigForInt64()
{
string json = @"{""code"": 307953220000517141511}";
JObject o = JObject.Parse(json);
BigInteger b = (BigInteger)o["code"];
}
#endif
[Test]
public void ParseIncomplete()
{
+18 -12
View File
@@ -353,15 +353,19 @@ namespace Newtonsoft.Json.Tests.Linq
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
string bigIntegerText = "1234567899999999999999999999999999999999999999999999999999999999999990";
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)));
Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(BigInteger.Parse(bigIntegerText))).Value);
Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(bigIntegerText)).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(long.MaxValue), (new JValue(long.MaxValue)).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(4.5d), (new JValue((4.5d))).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(4.5f), (new JValue((4.5f))).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(byte.MaxValue), (new JValue(byte.MaxValue)).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject<BigInteger?>());
Assert.AreEqual(null, (new JValue((object)null)).ToObject<BigInteger?>());
byte[] intData = BigInteger.Parse(bigIntegerText).ToByteArray();
Assert.AreEqual(BigInteger.Parse(bigIntegerText), (BigInteger)(new JValue(intData)));
Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(intData)).ToObject<BigInteger>());
Assert.AreEqual(4.0d, (double)(new JValue(new BigInteger(4.5d))));
Assert.AreEqual(true, (bool)(new JValue(new BigInteger(1))));
@@ -427,9 +431,10 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); });
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
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()); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to BigInteger.", () => { var i = (new JValue(new Uri("http://www.google.com"))).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to BigInteger.", () => { var i = (new JValue((object)null)).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject<BigInteger?>(); });
#endif
}
@@ -491,8 +496,9 @@ namespace Newtonsoft.Json.Tests.Linq
#endif
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
Assert.IsTrue(JToken.DeepEquals(new JValue(new BigInteger(1)), (JValue)new BigInteger(1)));
Assert.IsTrue(JToken.DeepEquals(new JValue((BigInteger?)null), (JValue)(BigInteger?)null));
// had to remove implicit casting to avoid user reference to System.Numerics.dll
Assert.IsTrue(JToken.DeepEquals(new JValue(new BigInteger(1)), new JValue(new BigInteger(1))));
Assert.IsTrue(JToken.DeepEquals(new JValue((BigInteger?)null), new JValue((BigInteger?)null)));
#endif
Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true));
Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true));
@@ -25,6 +25,9 @@
using System;
using System.Collections.Generic;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
using System.Numerics;
#endif
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
@@ -116,6 +119,11 @@ namespace Newtonsoft.Json.Tests.Linq
jsonWriter.WriteValue("DVD read/writer");
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
jsonWriter.WriteValue(new BigInteger(123));
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
#endif
jsonWriter.WriteValue(new byte[0]);
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
@@ -140,6 +148,27 @@ namespace Newtonsoft.Json.Tests.Linq
/*fail*/]", writer.Token.ToString());
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
[Test]
public void WriteBigInteger()
{
JTokenWriter writer = new JTokenWriter();
writer.WriteStartArray();
writer.WriteValue(new BigInteger(123));
writer.WriteEndArray();
JValue i = (JValue) writer.Token[0];
Assert.AreEqual(new BigInteger(123), i.Value);
Assert.AreEqual(JTokenType.Integer, i.Type);
Assert.AreEqual(@"[
123
]", writer.Token.ToString());
}
#endif
[Test]
public void WriteRaw()
{
@@ -75,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.16007")]
[assembly: AssemblyFileVersion("5.0.1.16008")]
+19 -12
View File
@@ -221,6 +221,25 @@ namespace Newtonsoft.Json.Bson
}
#region WriteValue methods
/// <summary>
/// Writes a <see cref="Object"/> value.
/// An error will raised if the value cannot be written as a single JSON token.
/// </summary>
/// <param name="value">The <see cref="Object"/> value to write.</param>
public override void WriteValue(object value)
{
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
if (value is BigInteger)
{
InternalWriteValue(JsonToken.Integer);
AddToken(new BsonBinary(((BigInteger)value).ToByteArray(), BsonBinaryType.Binary));
}
else
#endif
{
base.WriteValue(value);
}
}
/// <summary>
/// Writes a null value.
@@ -461,18 +480,6 @@ namespace Newtonsoft.Json.Bson
base.WriteValue(value);
AddToken(new BsonString(value.ToString(), true));
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
/// <summary>
/// Writes a <see cref="BigInteger"/> value.
/// </summary>
/// <param name="value">The <see cref="BigInteger"/> value to write.</param>
public override void WriteValue(BigInteger value)
{
base.WriteValue(value);
AddToken(new BsonBinary(value.ToByteArray(), BsonBinaryType.Binary));
}
#endif
#endregion
/// <summary>
+15 -12
View File
@@ -303,6 +303,21 @@ namespace Newtonsoft.Json
}
#region WriteValue methods
public override void WriteValue(object value)
{
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
if (value is BigInteger)
{
InternalWriteValue(JsonToken.Integer);
WriteValueInternal(((BigInteger)value).ToString(CultureInfo.InvariantCulture), JsonToken.String);
}
else
#endif
{
base.WriteValue(value);
}
}
/// <summary>
/// Writes a null value.
/// </summary>
@@ -597,18 +612,6 @@ namespace Newtonsoft.Json
WriteValueInternal(JsonConvert.ToString(value, _quoteChar), JsonToken.String);
}
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
/// <summary>
/// Writes a <see cref="BigInteger"/> value.
/// </summary>
/// <param name="value">The <see cref="BigInteger"/> value to write.</param>
public override void WriteValue(BigInteger value)
{
InternalWriteValue(JsonToken.Integer);
WriteValueInternal(value.ToString(CultureInfo.InvariantCulture), JsonToken.String);
}
#endif
#endregion
/// <summary>
+19 -24
View File
@@ -1231,29 +1231,6 @@ namespace Newtonsoft.Json
InternalWriteValue(JsonToken.String);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
/// <summary>
/// Writes a <see cref="BigInteger"/> value.
/// </summary>
/// <param name="value">The <see cref="BigInteger"/> value to write.</param>
public virtual void WriteValue(BigInteger value)
{
InternalWriteValue(JsonToken.Integer);
}
/// <summary>
/// Writes a <see cref="Nullable{BigInteger}"/> value.
/// </summary>
/// <param name="value">The <see cref="Nullable{BigInteger}"/> value to write.</param>
public virtual void WriteValue(BigInteger? value)
{
if (value == null)
WriteNull();
else
WriteValue(value.Value);
}
#endif
/// <summary>
/// Writes a <see cref="Object"/> value.
/// An error will raised if the value cannot be written as a single JSON token.
@@ -1262,9 +1239,20 @@ namespace Newtonsoft.Json
public virtual void WriteValue(object value)
{
if (value == null)
{
WriteNull();
}
else
{
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
// this is here because adding a WriteValue(BigInteger) to JsonWriter will
// mean the user has to add a reference to System.Numerics.dll
if (value is BigInteger)
throw CreateUnsupportedTypeException(this, value);
#endif
WriteValue(this, ConvertUtils.GetTypeCode(value), value);
}
}
#endregion
@@ -1421,9 +1409,11 @@ namespace Newtonsoft.Json
break;
#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
case PrimitiveTypeCode.BigInteger:
// this will call to WriteValue(object)
writer.WriteValue((BigInteger)value);
break;
case PrimitiveTypeCode.BigIntegerNullable:
// this will call to WriteValue(object)
writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value);
break;
#endif
@@ -1456,10 +1446,15 @@ namespace Newtonsoft.Json
else
#endif
{
throw JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
throw CreateUnsupportedTypeException(writer, value);
}
break;
}
}
private static JsonWriterException CreateUnsupportedTypeException(JsonWriter writer, object value)
{
return JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
}
}
}
+4 -38
View File
@@ -1177,12 +1177,7 @@ namespace Newtonsoft.Json.Linq
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
/// <summary>
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Numerics.BigInteger"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator BigInteger(JToken value)
private static BigInteger ToBigInteger(JToken value)
{
JValue v = EnsureValue(value);
if (v == null || !ValidateToken(v, BigIntegerTypes, false))
@@ -1191,12 +1186,7 @@ namespace Newtonsoft.Json.Linq
return ConvertUtils.ToBigInteger(v.Value);
}
/// <summary>
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{BigInteger}"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator BigInteger?(JToken value)
private static BigInteger? ToBigIntegerNullable(JToken value)
{
JValue v = EnsureValue(value);
if (v == null || !ValidateToken(v, BigIntegerTypes, true))
@@ -1421,18 +1411,6 @@ namespace Newtonsoft.Json.Linq
return new JValue(value);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
/// <summary>
/// Performs an implicit conversion from <see cref="Nullable{BigInteger}"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
public static implicit operator JToken(BigInteger? value)
{
return new JValue(value);
}
#endif
/// <summary>
/// Performs an implicit conversion from <see cref="Double"/> to <see cref="JToken"/>.
/// </summary>
@@ -1495,18 +1473,6 @@ namespace Newtonsoft.Json.Linq
return new JValue(value);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
/// <summary>
/// Performs an implicit conversion from <see cref="T:System.Numerics.BigInteger"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
public static implicit operator JToken(BigInteger value)
{
return new JValue(value);
}
#endif
/// <summary>
/// Performs an implicit conversion from <see cref="T:System.Uri"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
/// </summary>
@@ -1722,9 +1688,9 @@ namespace Newtonsoft.Json.Linq
return (TimeSpan)this;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
case PrimitiveTypeCode.BigIntegerNullable:
return (BigInteger?)this;
return ToBigIntegerNullable(this);
case PrimitiveTypeCode.BigInteger:
return (BigInteger)this;
return ToBigInteger(this);
#endif
}
+21 -13
View File
@@ -181,6 +181,26 @@ namespace Newtonsoft.Json.Linq
}
#region WriteValue methods
/// <summary>
/// Writes a <see cref="Object"/> value.
/// An error will raised if the value cannot be written as a single JSON token.
/// </summary>
/// <param name="value">The <see cref="Object"/> value to write.</param>
public override void WriteValue(object value)
{
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
if (value is BigInteger)
{
InternalWriteValue(JsonToken.Integer);
AddValue(value, JsonToken.Integer);
}
else
#endif
{
base.WriteValue(value);
}
}
/// <summary>
/// Writes a null value.
/// </summary>
@@ -431,18 +451,6 @@ namespace Newtonsoft.Json.Linq
base.WriteValue(value);
AddValue(value, JsonToken.String);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
/// <summary>
/// Writes a <see cref="BigInteger"/> value.
/// </summary>
/// <param name="value">The <see cref="BigInteger"/> value to write.</param>
public override void WriteValue(BigInteger value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
#endif
#endregion
}
}
}
@@ -92,6 +92,6 @@ 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.16007")]
[assembly: AssemblyFileVersion("5.0.1.16008")]
[assembly: CLSCompliant(true)]