/// <summary> /// Parses the current JSON token value from the source as an <see cref="int"/>. /// Returns <see langword="true"/> if the entire UTF-8 encoded token value can be successfully /// parsed to an <see cref="int"/> value. /// Returns <see langword="false"/> 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> public bool TryGetInt32(out int value) { if (TokenType != JsonTokenType.Number) { throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; return(TryGetInt32Core(out value, span)); }
/// <summary> /// Reads the next JSON token value from the source and parses it to a <see cref="decimal"/>. /// Returns true if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="decimal"/> value. /// Returns false otherwise. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown if trying to get the value of JSON token that is not a <see cref="JsonTokenType.Number"/>. /// <seealso cref="TokenType" /> /// </exception> public bool TryGetDecimalValue(out decimal value) { if (TokenType != JsonTokenType.Number) { throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } char standardFormat = ValueSpan.IndexOfAny((byte)'e', (byte)'E') >= 0 ? 'e' : default; return(Utf8Parser.TryParse(ValueSpan, out value, out int bytesConsumed, standardFormat) && ValueSpan.Length == bytesConsumed); }
/// <summary> /// Reads the next JSON token value from the source and parses it to a <see cref="decimal"/>. /// Returns true if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="decimal"/> value. /// Returns false 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> public bool TryGetDecimal(out decimal value) { if (TokenType != JsonTokenType.Number) { throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; return(Utf8Parser.TryParse(span, out value, out int bytesConsumed, _numberFormat) && span.Length == bytesConsumed); }
/// <summary> /// Reads the next JSON token value from the source and parses it to a <see cref="decimal"/>. /// Returns true if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="decimal"/> value. /// Returns false otherwise. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown if trying to get the value of JSON token that is not a <see cref="JsonTokenType.Number"/>. /// <seealso cref="TokenType" /> /// </exception> public bool TryGetDecimalValue(out decimal value) { if (TokenType != JsonTokenType.Number) { throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; char standardFormat = span.IndexOfAny((byte)'e', (byte)'E') >= 0 ? 'e' : default; return(Utf8Parser.TryParse(span, out value, out int bytesConsumed, standardFormat) && span.Length == bytesConsumed); }
/// <summary> /// Parses the current JSON token value from the source as a <see cref="double"/>. /// Returns <see langword="true"/> if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="double"/> value. /// Returns <see langword="false"/> 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> public bool TryGetDouble(out double value) { if (TokenType != JsonTokenType.Number) { throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; if (Utf8Parser.TryParse(span, out double tmp, out int bytesConsumed, _numberFormat) && span.Length == bytesConsumed) { value = tmp; return(true); } value = 0; return(false); }