From 6c817fee931591f7364bdd1c131cb03a35930c3d Mon Sep 17 00:00:00 2001 From: JamesNK Date: Sun, 25 Apr 2010 07:26:01 +0000 Subject: [PATCH] -Improve OverflowException message for integer values larger than an Int64 --- Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs | 9 +++++++++ Src/Newtonsoft.Json/JsonTextReader.cs | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 2f5ba518..374576ff 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -1479,5 +1479,14 @@ Parameter name: arrayIndex")] string name = (string)o.Property("responseData"); } + + [Test] + [ExpectedException(typeof(JsonReaderException), ExpectedMessage = "JSON integer 307953220000517141511 is too large or small for an Int64.")] + public void NumberTooBigForInt64() + { + string json = @"{""code"": 307953220000517141511}"; + + JObject.Parse(json); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index 446542c1..a0286fec 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -708,7 +708,15 @@ namespace Newtonsoft.Json } else { - numberValue = Convert.ToInt64(number, CultureInfo.InvariantCulture); + try + { + numberValue = Convert.ToInt64(number, CultureInfo.InvariantCulture); + } + catch (OverflowException ex) + { + throw new JsonReaderException("JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, number), ex); + } + numberType = JsonToken.Integer; }