Ejemplo n.º 1
0
        /// <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 (!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);
        }
Ejemplo n.º 2
0
        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);
        }