Add line information to JsonSerializationException (#1829)

This commit is contained in:
James Newton-King
2018-09-16 14:10:33 -07:00
committed by GitHub
parent c7d1615191
commit d7150871cb
3 changed files with 59 additions and 3 deletions
@@ -45,7 +45,11 @@ namespace Newtonsoft.Json.Tests.Converters
[Test]
public void DeserializeInvalidDataTable()
{
ExceptionAssert.Throws<JsonException>(() => JsonConvert.DeserializeObject<DataSet>("{\"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<JsonSerializationException>(() => JsonConvert.DeserializeObject<DataSet>("{\"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]
+5 -1
View File
@@ -121,13 +121,17 @@ namespace Newtonsoft.Json.Tests
[Test]
public void PopulateObjectWithOnlyComment()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
var ex = ExceptionAssert.Throws<JsonSerializationException>(() =>
{
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]
@@ -38,6 +38,24 @@ namespace Newtonsoft.Json
#endif
public class JsonSerializationException : JsonException
{
/// <summary>
/// Gets the line number indicating where the error occurred.
/// </summary>
/// <value>The line number indicating where the error occurred.</value>
public int LineNumber { get; }
/// <summary>
/// Gets the line position indicating where the error occurred.
/// </summary>
/// <value>The line position indicating where the error occurred.</value>
public int LinePosition { get; }
/// <summary>
/// Gets the path to the JSON where the error occurred.
/// </summary>
/// <value>The path to the JSON where the error occurred.</value>
public string Path { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializationException"/> class.
/// </summary>
@@ -80,6 +98,23 @@ namespace Newtonsoft.Json
}
#endif
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializationException"/> 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.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="path">The path to the JSON where the error occurred.</param>
/// <param name="lineNumber">The line number indicating where the error occurred.</param>
/// <param name="linePosition">The line position indicating where the error occurred.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or <c>null</c> if no inner exception is specified.</param>
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);
}
}
}