Beispiel #1
0
 /// <summary>
 /// Parses the current JSON token value from the source as a <see cref="double"/>.
 /// Returns the value if the entire UTF-8 encoded token value can be successfully parsed to a <see cref="double"/>
 /// value.
 /// Throws exceptions otherwise.
 /// </summary>
 /// <exception cref="InvalidOperationException">
 /// Thrown if trying to get the value of a JSON token that is not a <see cref="JsonTokenType.Number"/>.
 /// <seealso cref="TokenType" />
 /// </exception>
 /// <exception cref="FormatException">
 /// On any framework that is not .NET Core 3.0 or higher, thrown if the JSON token value represents a number less than <see cref="double.MinValue"/> or greater
 /// than <see cref="double.MaxValue"/>.
 /// </exception>
 public double GetDouble()
 {
     if (!TryGetDouble(out double value))
     {
         ThrowHelper.ThrowFormatException(NumericType.Double);
     }
     return(value);
 }
Beispiel #2
0
 /// <summary>
 /// Parses the current JSON token value from the source and decodes the Base64 encoded JSON string as bytes.
 /// </summary>
 /// <exception cref="InvalidOperationException">
 /// Thrown if trying to get the value of a JSON token that is not a <see cref="JsonTokenType.String"/>.
 /// <seealso cref="TokenType" />
 /// </exception>
 /// <exception cref="FormatException">
 /// The JSON string contains data outside of the expected Base64 range, or if it contains invalid/more than two padding characters,
 /// or is incomplete (i.e. the JSON string length is not a multiple of 4).
 /// </exception>
 public byte[] GetBytesFromBase64()
 {
     if (!TryGetBytesFromBase64(out byte[]? value))
     {
         ThrowHelper.ThrowFormatException(DataType.Base64String);
     }
     return(value);
 }
Beispiel #3
0
 public sbyte GetSByte()
 {
     if (!TryGetSByte(out sbyte value))
     {
         ThrowHelper.ThrowFormatException(NumericType.SByte);
     }
     return(value);
 }
Beispiel #4
0
 /// <summary>
 /// Parses the current JSON token value from the source as a <see cref="decimal"/>.
 /// Returns the value if the entire UTF-8 encoded token value can be successfully parsed to a <see cref="decimal"/>
 /// value.
 /// Throws exceptions otherwise.
 /// </summary>
 /// <exception cref="InvalidOperationException">
 /// Thrown if trying to get the value of a JSON token that is not a <see cref="JsonTokenType.Number"/>.
 /// <seealso cref="TokenType" />
 /// </exception>
 /// <exception cref="FormatException">
 /// Thrown if the JSON token value represents a number less than <see cref="decimal.MinValue"/> or greater
 /// than <see cref="decimal.MaxValue"/>.
 /// </exception>
 public decimal GetDecimal()
 {
     if (!TryGetDecimal(out decimal value))
     {
         ThrowHelper.ThrowFormatException(NumericType.Decimal);
     }
     return(value);
 }
Beispiel #5
0
 public ulong GetUInt64()
 {
     if (!TryGetUInt64(out ulong value))
     {
         ThrowHelper.ThrowFormatException(NumericType.UInt64);
     }
     return(value);
 }
Beispiel #6
0
 /// <summary>
 /// Parses the current JSON token value from the source as a <see cref="float"/>.
 /// Returns the value if the entire UTF-8 encoded token value can be successfully parsed to a <see cref="float"/>
 /// value.
 /// Throws exceptions otherwise.
 /// </summary>
 /// <exception cref="InvalidOperationException">
 /// Thrown if trying to get the value of a JSON token that is not a <see cref="JsonTokenType.Number"/>.
 /// <seealso cref="TokenType" />
 /// </exception>
 /// <exception cref="FormatException">
 /// On any framework that is not .NET Core 3.0 or higher, thrown if the JSON token value represents a number less than <see cref="float.MinValue"/> or greater
 /// than <see cref="float.MaxValue"/>.
 /// </exception>
 public float GetSingle()
 {
     if (!TryGetSingle(out float value))
     {
         ThrowHelper.ThrowFormatException(NumericType.Single);
     }
     return(value);
 }
Beispiel #7
0
 public uint GetUInt32()
 {
     if (!TryGetUInt32(out uint value))
     {
         ThrowHelper.ThrowFormatException(NumericType.UInt32);
     }
     return(value);
 }
Beispiel #8
0
 public ushort GetUInt16()
 {
     if (!TryGetUInt16(out ushort value))
     {
         ThrowHelper.ThrowFormatException(NumericType.UInt16);
     }
     return(value);
 }
Beispiel #9
0
        /// <summary>
        /// Parses the current JSON token value from the source as a <see cref="DateTimeOffset"/>.
        /// Returns the value if the entire UTF-8 encoded token value can be successfully parsed to a <see cref="DateTimeOffset"/>
        /// value.
        /// Throws exceptions otherwise.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Thrown if trying to get the value of a JSON token that is not a <see cref="JsonTokenType.String"/>.
        /// <seealso cref="TokenType" />
        /// </exception>
        /// <exception cref="FormatException">
        /// Thrown if the JSON token value is of an unsupported format. Only a subset of ISO 8601 formats are supported.
        /// </exception>
        public DateTimeOffset GetDateTimeOffset()
        {
            if (!TryGetDateTimeOffset(out DateTimeOffset value))
            {
                ThrowHelper.ThrowFormatException(DataType.DateTimeOffset);
            }

            return(value);
        }
Beispiel #10
0
        /// <summary>
        /// Parses the current JSON token value from the source as a <see cref="DateTime"/>.
        /// Returns the value if the entire UTF-8 encoded token value can be successfully parsed to a <see cref="DateTime"/>
        /// value.
        /// Throws exceptions otherwise.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Thrown if trying to get the value of a JSON token that is not a <see cref="JsonTokenType.String"/>.
        /// <seealso cref="TokenType" />
        /// </exception>
        /// <exception cref="FormatException">
        /// Thrown if the JSON token value is of an unsupported format. Only a subset of ISO 8601 formats are supported.
        /// </exception>
        public DateTime GetDateTime()
        {
            if (!TryGetDateTime(out DateTime value))
            {
                ThrowHelper.ThrowFormatException(DataType.DateTime);
            }

            return(value);
        }
Beispiel #11
0
        /// <summary>
        /// Parses the current JSON token value from the source as a <see cref="Guid"/>.
        /// Returns the value if the entire UTF-8 encoded token value can be successfully parsed to a <see cref="Guid"/>
        /// value.
        /// Throws exceptions otherwise.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Thrown if trying to get the value of a JSON token that is not a <see cref="JsonTokenType.String"/>.
        /// <seealso cref="TokenType" />
        /// </exception>
        /// <exception cref="FormatException">
        /// Thrown if the JSON token value is of an unsupported format for a Guid.
        /// </exception>
        public Guid GetGuid()
        {
            if (!TryGetGuid(out Guid value))
            {
                ThrowHelper.ThrowFormatException(DataType.Guid);
            }

            return(value);
        }
Beispiel #12
0
        internal DateTimeOffset GetDateTimeOffsetNoValidation()
        {
            if (!TryGetDateTimeOffsetCore(out DateTimeOffset value))
            {
                ThrowHelper.ThrowFormatException(DataType.DateTimeOffset);
            }

            return(value);
        }
Beispiel #13
0
        internal Guid GetGuidNoValidation()
        {
            if (!TryGetGuidCore(out Guid value))
            {
                ThrowHelper.ThrowFormatException(DataType.Guid);
            }

            return(value);
        }
Beispiel #14
0
        internal decimal GetDecimalWithQuotes()
        {
            ReadOnlySpan <byte> span = GetUnescapedSpan();

            if (!TryGetDecimalCore(out decimal value, span))
            {
                ThrowHelper.ThrowFormatException(NumericType.Decimal);
            }
            return(value);
        }
Beispiel #15
0
        internal int GetInt32WithQuotes()
        {
            ReadOnlySpan <byte> span = GetUnescapedSpan();

            if (!TryGetInt32Core(out int value, span))
            {
                ThrowHelper.ThrowFormatException(NumericType.Int32);
            }
            return(value);
        }
Beispiel #16
0
        internal ulong GetUInt64WithQuotes()
        {
            ReadOnlySpan <byte> span = GetUnescapedSpan();

            if (!TryGetUInt64Core(out ulong value, span))
            {
                ThrowHelper.ThrowFormatException(NumericType.UInt64);
            }
            return(value);
        }
Beispiel #17
0
        internal sbyte GetSByteWithQuotes()
        {
            ReadOnlySpan <byte> span = GetUnescapedSpan();

            if (!TryGetSByteCore(out sbyte value, span))
            {
                ThrowHelper.ThrowFormatException(NumericType.SByte);
            }
            return(value);
        }
Beispiel #18
0
        internal double GetDoubleFloatingPointConstant()
        {
            ReadOnlySpan <byte> span = GetUnescapedSpan();

            if (!JsonReaderHelper.TryGetFloatingPointConstant(span, out double value))
            {
                ThrowHelper.ThrowFormatException(NumericType.Double);
            }

            return(value);
        }
Beispiel #19
0
        internal double GetDoubleWithQuotes()
        {
            ReadOnlySpan <byte> span = GetUnescapedSpan();

            if (JsonReaderHelper.TryGetFloatingPointConstant(span, out double value))
            {
                return(value);
            }

            // NETCOREAPP implementation of the TryParse method above permits case-insensitive variants of the
            // float constants "NaN", "Infinity", "-Infinity". This differs from the NETFRAMEWORK implementation.
            // The following logic reconciles the two implementations to enforce consistent behavior.
            if (!(Utf8Parser.TryParse(span, out value, out int bytesConsumed) &&
                  span.Length == bytesConsumed &&
                  JsonHelpers.IsFinite(value)))
            {
                ThrowHelper.ThrowFormatException(NumericType.Double);
            }

            return(value);
        }