private static JsonDocument Parse( ReadOnlyMemory <byte> utf8Json, JsonReaderOptions readerOptions, byte[]?extraRentedArrayPoolBytes = null, PooledByteBufferWriter?extraPooledByteBufferWriter = null) { ReadOnlySpan <byte> utf8JsonSpan = utf8Json.Span; var database = MetadataDb.CreateRented(utf8Json.Length, convertToAlloc: false); var stack = new StackRowStack(JsonDocumentOptions.DefaultMaxDepth * StackRow.Size); try { Parse(utf8JsonSpan, readerOptions, ref database, ref stack); } catch { database.Dispose(); throw; } finally { stack.Dispose(); } return(new JsonDocument(utf8Json, database, extraRentedArrayPoolBytes, extraPooledByteBufferWriter)); }
private static JsonDocument Parse( ReadOnlyMemory <byte> utf8Json, JsonReaderOptions readerOptions, byte[]?extraRentedBytes) { ReadOnlySpan <byte> utf8JsonSpan = utf8Json.Span; var database = new MetadataDb(utf8Json.Length); var stack = new StackRowStack(JsonDocumentOptions.DefaultMaxDepth * StackRow.Size); try { Parse(utf8JsonSpan, readerOptions, ref database, ref stack); } catch { database.Dispose(); throw; } finally { stack.Dispose(); } return(new JsonDocument(utf8Json, database, extraRentedBytes)); }
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)); }