Ejemplo n.º 1
0
        public static bool TryGetEscapedDateTimeOffset(ReadOnlySpan <byte> source, out DateTimeOffset value)
        {
            int backslash = source.IndexOf(JsonConstants.BackSlash);

            Debug.Assert(backslash != -1);

            Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength);
            Span <byte> sourceUnescaped = stackalloc byte[source.Length];

            Unescape(source, sourceUnescaped, backslash, out int written);
            Debug.Assert(written > 0);

            sourceUnescaped = sourceUnescaped.Slice(0, written);
            Debug.Assert(!sourceUnescaped.IsEmpty);

            if (sourceUnescaped.Length <= JsonConstants.MaximumDateTimeOffsetParseLength &&
                JsonHelpers.TryParseAsISO(sourceUnescaped, out DateTimeOffset tmp))
            {
                value = tmp;
                return(true);
            }

            value = default;
            return(false);
        }
Ejemplo n.º 2
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 (!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);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public static bool TryGetEscapedDateTime(ReadOnlySpan <byte> source, out DateTime value)
        {
            Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength);
            Span <byte> sourceUnescaped = stackalloc byte[JsonConstants.MaximumEscapedDateTimeOffsetParseLength];

            Unescape(source, sourceUnescaped, out int written);
            Debug.Assert(written > 0);

            sourceUnescaped = sourceUnescaped.Slice(0, written);
            Debug.Assert(!sourceUnescaped.IsEmpty);

            if (JsonHelpers.IsValidUnescapedDateTimeOffsetParseLength(sourceUnescaped.Length) &&
                JsonHelpers.TryParseAsISO(sourceUnescaped, out DateTime tmp))
            {
                value = tmp;
                return(true);
            }

            value = default;
            return(false);
        }