diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 90578582..b93d0e76 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -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() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs index 6d92d3ac..388650db 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs @@ -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()); + Assert.AreEqual(new BigInteger(long.MaxValue), (new JValue(long.MaxValue)).ToObject()); + Assert.AreEqual(new BigInteger(4.5d), (new JValue((4.5d))).ToObject()); + Assert.AreEqual(new BigInteger(4.5f), (new JValue((4.5f))).ToObject()); + Assert.AreEqual(new BigInteger(byte.MaxValue), (new JValue(byte.MaxValue)).ToObject()); + Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject()); + Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject()); + Assert.AreEqual(null, (new JValue((object)null)).ToObject()); byte[] intData = BigInteger.Parse(bigIntegerText).ToByteArray(); - Assert.AreEqual(BigInteger.Parse(bigIntegerText), (BigInteger)(new JValue(intData))); + Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(intData)).ToObject()); 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("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); }); #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) - ExceptionAssert.Throws("Can not convert Uri to BigInteger.", () => { var i = (BigInteger)new JValue(new Uri("http://www.google.com")); }); - ExceptionAssert.Throws("Can not convert Null to BigInteger.", () => { var i = (BigInteger)new JValue((object)null); }); - ExceptionAssert.Throws("Can not convert Guid to BigInteger.", () => { var i = (BigInteger)new JValue(Guid.NewGuid()); }); + ExceptionAssert.Throws("Can not convert Uri to BigInteger.", () => { var i = (new JValue(new Uri("http://www.google.com"))).ToObject(); }); + ExceptionAssert.Throws("Can not convert Null to BigInteger.", () => { var i = (new JValue((object)null)).ToObject(); }); + ExceptionAssert.Throws("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject(); }); + ExceptionAssert.Throws("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject(); }); #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)); diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs index fd67f2f1..f5494e8b 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs @@ -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() { diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 9e38b8f9..6fd50297 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -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")] diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index 93de99fd..3ebef070 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -221,6 +221,25 @@ namespace Newtonsoft.Json.Bson } #region WriteValue methods + /// + /// Writes a value. + /// An error will raised if the value cannot be written as a single JSON token. + /// + /// The value to write. + 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); + } + } /// /// 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) - /// - /// Writes a value. - /// - /// The value to write. - public override void WriteValue(BigInteger value) - { - base.WriteValue(value); - AddToken(new BsonBinary(value.ToByteArray(), BsonBinaryType.Binary)); - } -#endif #endregion /// diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 801c8437..eee65962 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -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); + } + } + /// /// Writes a null value. /// @@ -597,18 +612,6 @@ namespace Newtonsoft.Json WriteValueInternal(JsonConvert.ToString(value, _quoteChar), JsonToken.String); } } - -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) - /// - /// Writes a value. - /// - /// The value to write. - public override void WriteValue(BigInteger value) - { - InternalWriteValue(JsonToken.Integer); - WriteValueInternal(value.ToString(CultureInfo.InvariantCulture), JsonToken.String); - } -#endif #endregion /// diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 98144ec8..31b2b4d0 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -1231,29 +1231,6 @@ namespace Newtonsoft.Json InternalWriteValue(JsonToken.String); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) - /// - /// Writes a value. - /// - /// The value to write. - public virtual void WriteValue(BigInteger value) - { - InternalWriteValue(JsonToken.Integer); - } - - /// - /// Writes a value. - /// - /// The value to write. - public virtual void WriteValue(BigInteger? value) - { - if (value == null) - WriteNull(); - else - WriteValue(value.Value); - } -#endif - /// /// Writes a 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); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index d58fc55a..df2ad5a6 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -1177,12 +1177,7 @@ namespace Newtonsoft.Json.Linq } #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) - /// - /// Performs an explicit conversion from to . - /// - /// The value. - /// The result of the conversion. - 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); } - /// - /// Performs an explicit conversion from to . - /// - /// The value. - /// The result of the conversion. - 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) - /// - /// Performs an implicit conversion from to . - /// - /// The value to create a from. - /// The initialized with the specified value. - public static implicit operator JToken(BigInteger? value) - { - return new JValue(value); - } -#endif - /// /// Performs an implicit conversion from to . /// @@ -1495,18 +1473,6 @@ namespace Newtonsoft.Json.Linq return new JValue(value); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) - /// - /// Performs an implicit conversion from to . - /// - /// The value to create a from. - /// The initialized with the specified value. - public static implicit operator JToken(BigInteger value) - { - return new JValue(value); - } -#endif - /// /// Performs an implicit conversion from to . /// @@ -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 } diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs index 67728c0a..4f89cbf1 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs @@ -181,6 +181,26 @@ namespace Newtonsoft.Json.Linq } #region WriteValue methods + /// + /// Writes a value. + /// An error will raised if the value cannot be written as a single JSON token. + /// + /// The value to write. + 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); + } + } + /// /// Writes a null value. /// @@ -431,18 +451,6 @@ namespace Newtonsoft.Json.Linq base.WriteValue(value); AddValue(value, JsonToken.String); } - -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) - /// - /// Writes a value. - /// - /// The value to write. - public override void WriteValue(BigInteger value) - { - base.WriteValue(value); - AddValue(value, JsonToken.Integer); - } -#endif #endregion } -} +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index f0e96996..181171b7 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -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)]