public bool TryGetUInt64(out ulong 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) && span.Length == bytesConsumed); }
/// <summary> /// Reads the next JSON token value from the source and parses it to a <see cref="DateTimeOffset"/>. /// Returns true if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="DateTimeOffset"/> 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.String"/>. /// <seealso cref="TokenType" /> /// </exception> public bool TryGetDateTimeOffset(out DateTimeOffset value) { if (TokenType != JsonTokenType.String) { throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; return(JsonHelpers.TryParseAsISO(span, out value, out int bytesConsumed) && span.Length == bytesConsumed); }
public bool TryGetUInt64(out ulong value) { if (TokenType != JsonTokenType.Number) { throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; return(TryGetUInt64Core(out value, span)); }
public bool TryGetSByte(out sbyte value) { if (TokenType != JsonTokenType.Number) { ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; return(TryGetSByteCore(out value, span)); }
/// <summary> /// Reads the next JSON token value from the source transcoded as a <see cref="string"/>. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown if trying to get the value of the JSON token that is not a string /// (i.e. other than <see cref="JsonTokenType.String"/> or <see cref="JsonTokenType.PropertyName"/>). /// <seealso cref="TokenType" /> /// </exception> /// <exception cref="ArgumentException"> /// Thrown if invalid UTF-8 byte sequences are detected while transcoding. /// </exception> public string GetStringValue() { if (TokenType != JsonTokenType.String && TokenType != JsonTokenType.PropertyName) { throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; // TODO: https://github.com/dotnet/corefx/issues/33292 return(s_utf8Encoding.GetString(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); } 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="bool"/>. /// Returns <see langword="true"/> if the TokenType is JsonTokenType.True and <see langword="false"/> if the TokenType is JsonTokenType.False. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown if trying to get the value of a JSON token that is not a boolean (i.e. <see cref="JsonTokenType.True"/> or <see cref="JsonTokenType.False"/>). /// <seealso cref="TokenType" /> /// </exception> public bool GetBoolean() { JsonTokenType type = TokenType; if (type == JsonTokenType.True) { Debug.Assert((HasValueSequence ? ValueSequence.ToArray() : ValueSpan).Length == 4); return(true); } else if (type != JsonTokenType.False) { ThrowHelper.ThrowInvalidOperationException_ExpectedBoolean(TokenType); Debug.Fail("Throw helper should have thrown an exception."); } Debug.Assert((HasValueSequence ? ValueSequence.ToArray() : ValueSpan).Length == 5); return(false); }
/// <summary> /// Reads the next JSON token value from the source as a <see cref="bool"/>. /// Returns true if the TokenType is JsonTokenType.True and false if the TokenType is JsonTokenType.False. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown if trying to get the value of JSON token that is not a boolean (i.e. <see cref="JsonTokenType.True"/> or <see cref="JsonTokenType.False"/>). /// <seealso cref="TokenType" /> /// </exception> public bool GetBooleanValue() { ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; if (TokenType == JsonTokenType.True) { Debug.Assert(span.Length == 4); return(true); } else if (TokenType == JsonTokenType.False) { Debug.Assert(span.Length == 5); return(false); } else { throw ThrowHelper.GetInvalidOperationException_ExpectedBoolean(TokenType); } }
/// <summary> /// Parses the current JSON token value from the source as a <see cref="decimal"/>. /// Returns <see langword="true"/> if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="decimal"/> 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 TryGetDecimal(out decimal value) { if (TokenType != JsonTokenType.Number) { throw ThrowHelper.GetInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; if (Utf8Parser.TryParse(span, out decimal tmp, out int bytesConsumed, _numberFormat) && span.Length == bytesConsumed) { value = tmp; return(true); } value = 0; return(false); }
/// <summary> /// Parses the current JSON token value from the source and decodes the Base64 encoded JSON string as bytes. /// Returns <see langword="true"/> if the entire token value is encoded as valid Base64 text and can be successfully /// decoded to bytes. /// 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.String"/>. /// <seealso cref="TokenType" /> /// </exception> public bool TryGetBytesFromBase64(out byte[] value) { if (TokenType != JsonTokenType.String) { throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; if (_stringHasEscaping) { int idx = span.IndexOf(JsonConstants.BackSlash); Debug.Assert(idx != -1); return(JsonReaderHelper.TryGetUnescapedBase64Bytes(span, idx, out value)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); return(JsonReaderHelper.TryDecodeBase64(span, out value)); }
/// <summary> /// Parses the current JSON token value from the source, unescaped, and transcoded as a <see cref="string"/>. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown if trying to get the value of the JSON token that is not a string /// (i.e. other than <see cref="JsonTokenType.String"/> or <see cref="JsonTokenType.PropertyName"/>). /// <seealso cref="TokenType" /> /// It will also throw when the JSON string contains invalid UTF-8 bytes, or invalid UTF-16 surrogates. /// </exception> public string GetString() { if (TokenType != JsonTokenType.String && TokenType != JsonTokenType.PropertyName) { throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; if (_stringHasEscaping) { int idx = span.IndexOf(JsonConstants.BackSlash); Debug.Assert(idx != -1); return(JsonReaderHelper.GetUnescapedString(span, idx)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); return(JsonReaderHelper.TranscodeHelper(span)); }
/// <summary> /// Parses the current JSON token value from the source as a <see cref="float"/>. /// Returns <see langword="true"/> if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="float"/> 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 TryGetSingle(out float value) { if (TokenType != JsonTokenType.Number) { ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(TokenType); } ReadOnlySpan <byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; if (Utf8Parser.TryParse(span, out float tmp, out int bytesConsumed) && span.Length == bytesConsumed) { value = tmp; return(true); } value = 0; return(false); }