/
JsonException.cs
154 lines (139 loc) · 7.18 KB
/
JsonException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Runtime.Serialization;
namespace System.Text.Json
{
/// <summary>
/// Defines a custom exception object that is thrown when invalid JSON text is encountered, when the defined maximum depth is passed,
/// or the JSON text is not compatible with the type of a property on an object.
/// </summary>
[Serializable]
public class JsonException : Exception
{
// Allow the message to mutate to avoid re-throwing and losing the StackTrace to an inner exception.
internal string? _message;
/// <summary>
/// Creates a new exception object to relay error information to the user.
/// </summary>
/// <param name="message">The context specific error message.</param>
/// <param name="lineNumber">The line number at which the invalid JSON was encountered (starting at 0) when deserializing.</param>
/// <param name="bytePositionInLine">The byte count within the current line where the invalid JSON was encountered (starting at 0).</param>
/// <param name="path">The path where the invalid JSON was encountered.</param>
/// <param name="innerException">The exception that caused the current exception.</param>
/// <remarks>
/// Note that the <paramref name="bytePositionInLine"/> counts the number of bytes (i.e. UTF-8 code units) and not characters or scalars.
/// </remarks>
public JsonException(string? message, string? path, long? lineNumber, long? bytePositionInLine, Exception? innerException) : base(message, innerException)
{
_message = message;
LineNumber = lineNumber;
BytePositionInLine = bytePositionInLine;
Path = path;
}
/// <summary>
/// Creates a new exception object to relay error information to the user.
/// </summary>
/// <param name="message">The context specific error message.</param>
/// <param name="path">The path where the invalid JSON was encountered.</param>
/// <param name="lineNumber">The line number at which the invalid JSON was encountered (starting at 0) when deserializing.</param>
/// <param name="bytePositionInLine">The byte count within the current line where the invalid JSON was encountered (starting at 0).</param>
/// <remarks>
/// Note that the <paramref name="bytePositionInLine"/> counts the number of bytes (i.e. UTF-8 code units) and not characters or scalars.
/// </remarks>
public JsonException(string? message, string? path, long? lineNumber, long? bytePositionInLine) : base(message)
{
_message = message;
LineNumber = lineNumber;
BytePositionInLine = bytePositionInLine;
Path = path;
}
/// <summary>
/// Creates a new exception object to relay error information to the user.
/// </summary>
/// <param name="message">The context specific error message.</param>
/// <param name="innerException">The exception that caused the current exception.</param>
public JsonException(string? message, Exception? innerException) : base(message, innerException)
{
_message = message;
}
/// <summary>
/// Creates a new exception object to relay error information to the user.
/// </summary>
/// <param name="message">The context specific error message.</param>
public JsonException(string? message) : base(message)
{
_message = message;
}
/// <summary>
/// Creates a new exception object to relay error information to the user.
/// </summary>
public JsonException() : base() { }
/// <summary>
/// Creates a new exception object with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="info"/> is <see langword="null" />.
/// </exception>
#if NET8_0_OR_GREATER
[Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
protected JsonException(SerializationInfo info, StreamingContext context) : base(info, context)
{
LineNumber = (long?)info.GetValue("LineNumber", typeof(long?));
BytePositionInLine = (long?)info.GetValue("BytePositionInLine", typeof(long?));
Path = info.GetString("Path");
SetMessage(info.GetString("ActualMessage"));
}
/// <summary>
/// Specifies that 'try' logic should append Path information to the exception message.
/// </summary>
internal bool AppendPathInformation { get; set; }
/// <summary>
/// Sets the <see cref="SerializationInfo"/> with information about the exception.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
#if NET8_0_OR_GREATER
[Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("LineNumber", LineNumber, typeof(long?));
info.AddValue("BytePositionInLine", BytePositionInLine, typeof(long?));
info.AddValue("Path", Path, typeof(string));
info.AddValue("ActualMessage", Message, typeof(string));
}
/// <summary>
/// The number of lines read so far before the exception (starting at 0).
/// </summary>
public long? LineNumber { get; internal set; }
/// <summary>
/// The number of bytes read within the current line before the exception (starting at 0).
/// </summary>
public long? BytePositionInLine { get; internal set; }
/// <summary>
/// The path within the JSON where the exception was encountered.
/// </summary>
public string? Path { get; internal set; }
/// <summary>
/// Gets a message that describes the current exception.
/// </summary>
public override string Message
{
get
{
return _message ?? base.Message;
}
}
internal void SetMessage(string? message)
{
_message = message;
}
}
}