Exemple #1
0
        public static unsafe object?ParseJson(
            string sourceText,
            ParserOptions?options = null)
        {
            if (string.IsNullOrEmpty(sourceText))
            {
                throw new ArgumentException(
                          LangResources.SourceText_Empty,
                          nameof(sourceText));
            }

            options ??= ParserOptions.Default;

            var length = checked (sourceText.Length * 4);

            byte[]? source = null;

            Span <byte> sourceSpan = length <= GraphQLConstants.StackallocThreshold
                ? stackalloc byte[length]
                : source = ArrayPool <byte> .Shared.Rent(length);

            try
            {
                Utf8GraphQLParser.ConvertToBytes(sourceText, ref sourceSpan);
                return(ParseJson(sourceSpan, options));
            }
            finally
            {
                if (source != null)
                {
                    sourceSpan.Clear();
                    ArrayPool <byte> .Shared.Return(source);
                }
            }
        }
        public static unsafe IReadOnlyList <GraphQLRequest> Parse(
            string sourceText,
            ParserOptions?options = null,
            IDocumentCache?cache  = null,
            IDocumentHashProvider?hashProvider = null)
        {
            if (string.IsNullOrEmpty(sourceText))
            {
                throw new ArgumentException(SourceText_Empty, nameof(sourceText));
            }

            var length = checked (sourceText.Length * 4);

            byte[]? source = null;

            Span <byte> sourceSpan = length <= GraphQLConstants.StackallocThreshold
                ? stackalloc byte[length]
                : source = ArrayPool <byte> .Shared.Rent(length);

            try
            {
                Utf8GraphQLParser.ConvertToBytes(sourceText, ref sourceSpan);
                var parser = new Utf8GraphQLRequestParser(sourceSpan, options, cache, hashProvider);
                return(parser.Parse());
            }
            finally
            {
                if (source != null)
                {
                    sourceSpan.Clear();
                    ArrayPool <byte> .Shared.Return(source);
                }
            }
        }
Exemple #3
0
        public ReadOnlySpan <byte> AsSpan()
        {
            if (_memory.IsEmpty)
            {
                int           length   = checked (_value !.Length * 4);
                Memory <byte> memory   = new byte[length];
                Span <byte>   span     = memory.Span;
                int           buffered = Utf8GraphQLParser.ConvertToBytes(_value, ref span);
                _memory = memory.Slice(0, buffered);
            }

            return(_memory.Span);
        }