2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Globalization;
28
using System.Runtime.Serialization;
29
using Newtonsoft.Json.Utilities;
31
namespace Newtonsoft.Json
34
/// The exception thrown when an error occurs while reading Json text.
36
#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE)
39
public class JsonReaderException : JsonException
42
/// Gets the line number indicating where the error occurred.
44
/// <value>The line number indicating where the error occurred.</value>
45
public int LineNumber { get; private set; }
48
/// Gets the line position indicating where the error occurred.
50
/// <value>The line position indicating where the error occurred.</value>
51
public int LinePosition { get; private set; }
54
/// Gets the path to the JSON where the error occurred.
56
/// <value>The path to the JSON where the error occurred.</value>
57
public string Path { get; private set; }
60
/// Initializes a new instance of the <see cref="JsonReaderException"/> class.
62
public JsonReaderException()
67
/// Initializes a new instance of the <see cref="JsonReaderException"/> class
68
/// with a specified error message.
70
/// <param name="message">The error message that explains the reason for the exception.</param>
71
public JsonReaderException(string message)
77
/// Initializes a new instance of the <see cref="JsonReaderException"/> class
78
/// with a specified error message and a reference to the inner exception that is the cause of this exception.
80
/// <param name="message">The error message that explains the reason for the exception.</param>
81
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
82
public JsonReaderException(string message, Exception innerException)
83
: base(message, innerException)
87
#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE)
89
/// Initializes a new instance of the <see cref="JsonReaderException"/> class.
91
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
92
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
93
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
94
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
95
public JsonReaderException(SerializationInfo info, StreamingContext context)
101
internal JsonReaderException(string message, Exception innerException, string path, int lineNumber, int linePosition)
102
: base(message, innerException)
105
LineNumber = lineNumber;
106
LinePosition = linePosition;
109
internal static JsonReaderException Create(JsonReader reader, string message)
111
return Create(reader, message, null);
114
internal static JsonReaderException Create(JsonReader reader, string message, Exception ex)
116
return Create(reader as IJsonLineInfo, reader.Path, message, ex);
119
internal static JsonReaderException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex)
121
message = JsonPosition.FormatMessage(lineInfo, path, message);
125
if (lineInfo != null && lineInfo.HasLineInfo())
127
lineNumber = lineInfo.LineNumber;
128
linePosition = lineInfo.LinePosition;
136
return new JsonReaderException(message, ex, path, lineNumber, linePosition);