diff --git a/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs index bd54e8c4..3c127422 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs @@ -45,7 +45,11 @@ namespace Newtonsoft.Json.Tests.Converters [Test] public void DeserializeInvalidDataTable() { - ExceptionAssert.Throws(() => JsonConvert.DeserializeObject("{\"pending_count\":23,\"completed_count\":45}"), "Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'pending_count', line 1, position 19."); + var ex = ExceptionAssert.Throws(() => JsonConvert.DeserializeObject("{\"pending_count\":23,\"completed_count\":45}"), "Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'pending_count', line 1, position 19."); + + Assert.AreEqual(1, ex.LineNumber); + Assert.AreEqual(19, ex.LinePosition); + Assert.AreEqual("pending_count", ex.Path); } [Test] diff --git a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs index 5d4cec78..32d91bd9 100644 --- a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs @@ -121,13 +121,17 @@ namespace Newtonsoft.Json.Tests [Test] public void PopulateObjectWithOnlyComment() { - ExceptionAssert.Throws(() => + var ex = ExceptionAssert.Throws(() => { string json = @"// file header"; PopulateTestObject o = new PopulateTestObject(); JsonConvert.PopulateObject(json, o); }, "No JSON content found. Path '', line 1, position 14."); + + Assert.AreEqual(1, ex.LineNumber); + Assert.AreEqual(14, ex.LinePosition); + Assert.AreEqual(string.Empty, ex.Path); } [Test] diff --git a/Src/Newtonsoft.Json/JsonSerializationException.cs b/Src/Newtonsoft.Json/JsonSerializationException.cs index 3a4acb97..308b799b 100644 --- a/Src/Newtonsoft.Json/JsonSerializationException.cs +++ b/Src/Newtonsoft.Json/JsonSerializationException.cs @@ -38,6 +38,24 @@ namespace Newtonsoft.Json #endif public class JsonSerializationException : JsonException { + /// + /// Gets the line number indicating where the error occurred. + /// + /// The line number indicating where the error occurred. + public int LineNumber { get; } + + /// + /// Gets the line position indicating where the error occurred. + /// + /// The line position indicating where the error occurred. + public int LinePosition { get; } + + /// + /// Gets the path to the JSON where the error occurred. + /// + /// The path to the JSON where the error occurred. + public string Path { get; } + /// /// Initializes a new instance of the class. /// @@ -80,6 +98,23 @@ namespace Newtonsoft.Json } #endif + /// + /// Initializes a new instance of the class + /// with a specified error message, JSON path, line number, line position, and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The path to the JSON where the error occurred. + /// The line number indicating where the error occurred. + /// The line position indicating where the error occurred. + /// The exception that is the cause of the current exception, or null if no inner exception is specified. + public JsonSerializationException(string message, string path, int lineNumber, int linePosition, Exception innerException) + : base(message, innerException) + { + Path = path; + LineNumber = lineNumber; + LinePosition = linePosition; + } + internal static JsonSerializationException Create(JsonReader reader, string message) { return Create(reader, message, null); @@ -94,7 +129,20 @@ namespace Newtonsoft.Json { message = JsonPosition.FormatMessage(lineInfo, path, message); - return new JsonSerializationException(message, ex); + int lineNumber; + int linePosition; + if (lineInfo != null && lineInfo.HasLineInfo()) + { + lineNumber = lineInfo.LineNumber; + linePosition = lineInfo.LinePosition; + } + else + { + lineNumber = 0; + linePosition = 0; + } + + return new JsonSerializationException(message, path, lineNumber, linePosition, ex); } } } \ No newline at end of file