예제 #1
0
        private static JsonDocument CreateForLiteral(JsonTokenType tokenType)
        {
            switch (tokenType)
            {
            case JsonTokenType.False:
                s_falseLiteral ??= Create(JsonConstants.FalseValue.ToArray());
                return(s_falseLiteral);

            case JsonTokenType.True:
                s_trueLiteral ??= Create(JsonConstants.TrueValue.ToArray());
                return(s_trueLiteral);

            default:
                Debug.Assert(tokenType == JsonTokenType.Null);
                s_nullLiteral ??= Create(JsonConstants.NullValue.ToArray());
                return(s_nullLiteral);
            }

            JsonDocument Create(byte[] utf8Json)
            {
                MetadataDb database = MetadataDb.CreateLocked(utf8Json.Length);

                database.Append(tokenType, startLocation: 0, utf8Json.Length);
                return(new JsonDocument(utf8Json, database));
            }
        }
예제 #2
0
        private static JsonDocument ParseUnrented(
            ReadOnlyMemory <byte> utf8Json,
            JsonReaderOptions readerOptions,
            JsonTokenType tokenType = JsonTokenType.None)
        {
            // These tokens should already have been processed.
            Debug.Assert(
                tokenType != JsonTokenType.Null &&
                tokenType != JsonTokenType.False &&
                tokenType != JsonTokenType.True);

            ReadOnlySpan <byte> utf8JsonSpan = utf8Json.Span;
            MetadataDb          database;

            if (tokenType == JsonTokenType.String || tokenType == JsonTokenType.Number)
            {
                // For primitive types, we can avoid renting MetadataDb and creating StackRowStack.
                database = MetadataDb.CreateLocked(utf8Json.Length);
                StackRowStack stack = default;
                Parse(utf8JsonSpan, readerOptions, ref database, ref stack);
            }
            else
            {
                database = MetadataDb.CreateRented(utf8Json.Length, convertToAlloc: true);
                var stack = new StackRowStack(JsonDocumentOptions.DefaultMaxDepth * StackRow.Size);
                try
                {
                    Parse(utf8JsonSpan, readerOptions, ref database, ref stack);
                }
                finally
                {
                    stack.Dispose();
                }
            }

            return(new JsonDocument(utf8Json, database));
        }