< Summary

Information
Line coverage
68%
Covered lines: 32
Uncovered lines: 15
Coverable lines: 47
Total lines: 154
Line coverage: 68%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor()100%110%
.ctor(...)100%110%
GetObjectData(...)100%110%
SetMessage(...)100%11100%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\JsonException.cs

#LineLine coverage
 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
 4using System.ComponentModel;
 5using System.Runtime.Serialization;
 6
 7namespace 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>
 3564030        public JsonException(string? message, string? path, long? lineNumber, long? bytePositionInLine, Exception? inner
 3564031        {
 3564032            _message = message;
 3564033            LineNumber = lineNumber;
 3564034            BytePositionInLine = bytePositionInLine;
 3564035            Path = path;
 3564036        }
 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>
 3698848        public JsonException(string? message, string? path, long? lineNumber, long? bytePositionInLine) : base(message)
 3698849        {
 3698850            _message = message;
 3698851            LineNumber = lineNumber;
 3698852            BytePositionInLine = bytePositionInLine;
 3698853            Path = path;
 3698854        }
 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>
 1752661        public JsonException(string? message, Exception? innerException) : base(message, innerException)
 1752662        {
 1752663            _message = message;
 1752664        }
 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>
 256270        public JsonException(string? message) : base(message)
 256271        {
 256272            _message = message;
 256273        }
 74
 75        /// <summary>
 76        /// Creates a new exception object to relay error information to the user.
 77        /// </summary>
 078        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
 092        protected JsonException(SerializationInfo info, StreamingContext context) : base(info, context)
 093        {
 094            LineNumber = (long?)info.GetValue("LineNumber", typeof(long?));
 095            BytePositionInLine = (long?)info.GetValue("BytePositionInLine", typeof(long?));
 096            Path = info.GetString("Path");
 097            SetMessage(info.GetString("ActualMessage"));
 098        }
 99
 100        /// <summary>
 101        /// Specifies that 'try' logic should append Path information to the exception message.
 102        /// </summary>
 41304103        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)
 0115        {
 0116            base.GetObjectData(info, context);
 0117            info.AddValue("LineNumber", LineNumber, typeof(long?));
 0118            info.AddValue("BytePositionInLine", BytePositionInLine, typeof(long?));
 0119            info.AddValue("Path", Path, typeof(string));
 0120            info.AddValue("ActualMessage", Message, typeof(string));
 0121        }
 122
 123        /// <summary>
 124        /// The number of lines read so far before the exception (starting at 0).
 125        /// </summary>
 128356126        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>
 128356131        public long? BytePositionInLine { get; internal set; }
 132
 133        /// <summary>
 134        /// The path within the JSON where the exception was encountered.
 135        /// </summary>
 208082136        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
 35640144            {
 35640145                return _message ?? base.Message;
 35640146            }
 147        }
 148
 149        internal void SetMessage(string? message)
 20088150        {
 20088151            _message = message;
 20088152        }
 153    }
 154}