/// <summary> /// Parses the current JSON token value from the source as a <see cref="DateTimeOffset"/>. /// Returns <see langword="true"/> if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="DateTimeOffset"/> 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.String"/>. /// <seealso cref="TokenType" /> /// </exception> public bool TryGetDateTimeOffset(out DateTimeOffset value) { if (TokenType != JsonTokenType.String) { throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan <byte> span = stackalloc byte[0]; if (HasValueSequence) { long sequenceLength = ValueSequence.Length; if (!JsonReaderHelper.IsValidDateTimeOffsetParseLength(sequenceLength)) { value = default; return(false); } Debug.Assert(sequenceLength <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength); Span <byte> stackSpan = stackalloc byte[(int)sequenceLength]; ValueSequence.CopyTo(stackSpan); span = stackSpan; } else { if (!JsonReaderHelper.IsValidDateTimeOffsetParseLength(ValueSpan.Length)) { value = default; return(false); } span = ValueSpan; } if (_stringHasEscaping) { return(JsonReaderHelper.TryGetEscapedDateTimeOffset(span, out value)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); if (span.Length <= JsonConstants.MaximumDateTimeOffsetParseLength && JsonHelpers.TryParseAsISO(span, out DateTimeOffset tmp, out int bytesConsumed) && span.Length == bytesConsumed) { value = tmp; return(true); } value = default; return(false); }
/// <summary> /// Parses the current JSON token value from the source as a <see cref="Guid"/>. /// Returns <see langword="true"/> if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="Guid"/> value. Only supports <see cref="Guid"/> values with hyphens /// and without any surrounding decorations. /// 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 TryGetGuid(out Guid value) { if (TokenType != JsonTokenType.String) { throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan <byte> span = stackalloc byte[0]; if (HasValueSequence) { long sequenceLength = ValueSequence.Length; if (sequenceLength > JsonConstants.MaximumEscapedGuidLength) { value = default; return(false); } Debug.Assert(sequenceLength <= JsonConstants.MaximumEscapedGuidLength); Span <byte> stackSpan = stackalloc byte[(int)sequenceLength]; ValueSequence.CopyTo(stackSpan); span = stackSpan; } else { if (ValueSpan.Length > JsonConstants.MaximumEscapedGuidLength) { value = default; return(false); } span = ValueSpan; } if (_stringHasEscaping) { return(JsonReaderHelper.TryGetEscapedGuid(span, out value)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); if (span.Length == JsonConstants.MaximumFormatGuidLength && Utf8Parser.TryParse(span, out Guid tmp, out _, 'D')) { value = tmp; return(true); } value = default; return(false); }
/// <summary> /// Reads the next JSON token value from the source and parses it to a <see cref="Guid"/>. /// Returns <see langword="true"/> if the entire UTF-8 encoded token value can be successfully /// parsed to a <see cref="Guid"/> value. Only supports <see cref="Guid"/> values with hyphens /// and without any surrounding decorations. /// 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 TryGetGuid(out Guid value) { if (TokenType != JsonTokenType.String) { throw ThrowHelper.GetInvalidOperationException_ExpectedString(TokenType); } ReadOnlySpan <byte> span = ValueSpan; Span <byte> stackSpan; if (HasValueSequence) { long sequenceLength = ValueSequence.Length; if (sequenceLength > JsonConstants.MaximumEscapedGuidLength) { value = default; return(false); } // Cannot create a span directly since it gets passed to instance methods on a ref struct. unsafe { byte *ptr = stackalloc byte[(int)sequenceLength]; stackSpan = new Span <byte>(ptr, (int)sequenceLength); } ValueSequence.CopyTo(stackSpan); span = stackSpan; } if (span.Length > JsonConstants.MaximumEscapedGuidLength) { value = default; return(false); } if (_stringHasEscaping) { return(JsonReaderHelper.TryGetEscapedGuid(span, out value)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); if (span.Length != JsonConstants.MaximumFormatGuidLength) { value = default; return(false); } return(Utf8Parser.TryParse(span, out value, out int bytesConsumed) && span.Length == bytesConsumed); }
internal bool TryGetGuidCore(out Guid value) { ReadOnlySpan <byte> span = stackalloc byte[0]; int maximumLength = _stringHasEscaping ? JsonConstants.MaximumEscapedGuidLength : JsonConstants.MaximumFormatGuidLength; if (HasValueSequence) { long sequenceLength = ValueSequence.Length; if (sequenceLength > maximumLength) { value = default; return(false); } Debug.Assert(sequenceLength <= JsonConstants.MaximumEscapedGuidLength); Span <byte> stackSpan = stackalloc byte[_stringHasEscaping ? JsonConstants.MaximumEscapedGuidLength : JsonConstants.MaximumFormatGuidLength]; ValueSequence.CopyTo(stackSpan); span = stackSpan.Slice(0, (int)sequenceLength); } else { if (ValueSpan.Length > maximumLength) { value = default; return(false); } span = ValueSpan; } if (_stringHasEscaping) { return(JsonReaderHelper.TryGetEscapedGuid(span, out value)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); if (span.Length == JsonConstants.MaximumFormatGuidLength && Utf8Parser.TryParse(span, out Guid tmp, out _, 'D')) { value = tmp; return(true); } value = default; return(false); }
internal bool TryGetDateTimeOffsetCore(out DateTimeOffset value) { ReadOnlySpan <byte> span = stackalloc byte[0]; if (HasValueSequence) { long sequenceLength = ValueSequence.Length; if (!JsonHelpers.IsValidDateTimeOffsetParseLength(sequenceLength)) { value = default; return(false); } Debug.Assert(sequenceLength <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength); Span <byte> stackSpan = stackalloc byte[(int)sequenceLength]; ValueSequence.CopyTo(stackSpan); span = stackSpan; } else { if (!JsonHelpers.IsValidDateTimeOffsetParseLength(ValueSpan.Length)) { value = default; return(false); } span = ValueSpan; } if (_stringHasEscaping) { return(JsonReaderHelper.TryGetEscapedDateTimeOffset(span, out value)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); if (JsonHelpers.TryParseAsISO(span, out DateTimeOffset tmp)) { value = tmp; return(true); } value = default; return(false); }