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