public static bool TryParseAsISO(ReadOnlySpan <char> source, out DateTimeOffset value) { if (!IsValidDateTimeOffsetParseLength(source.Length)) { value = default; return(false); } int maxLength = checked (source.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); Span <byte> bytes = maxLength <= JsonConstants.StackallocThreshold ? stackalloc byte[JsonConstants.StackallocThreshold] : new byte[maxLength]; int length = JsonReaderHelper.GetUtf8FromText(source, bytes); bytes = bytes.Slice(0, length); if (bytes.IndexOf(JsonConstants.BackSlash) != -1) { return(JsonReaderHelper.TryGetEscapedDateTimeOffset(bytes, out value)); } Debug.Assert(bytes.IndexOf(JsonConstants.BackSlash) == -1); if (TryParseAsISO(bytes, out DateTimeOffset tmp)) { value = tmp; return(true); } value = default; return(false); }
/// <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); }
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); }