Add line information to JsonSerializationException (#1829)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user