public BlittableJsonReaderObject ReadObjectWithExternalProperties(DynamicJsonValue obj, string debugTag)
        {
            var state = new JsonParserState();

            using (var parser = new ObjectJsonParser(state, obj, this))
            {
                var writer = new BlittableJsonDocumentBuilder(this, BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, state);
                try
                {
                    writer.ReadObject();
                    if (writer.Read() == false)
                    {
                        throw new InvalidOperationException("Partial json content in object json parser shouldn't happen");
                    }
                    writer.FinalizeDocumentWithoutProperties(CachedProperties.Version);
                    _disposables.Add(writer);
                    return(writer.CreateReader(CachedProperties));
                }
                catch (Exception)
                {
                    writer.Dispose();
                    throw;
                }
            }
        }
        public async Task <BlittableJsonReaderObject> ReadFromWebSocket(
            WebSocket webSocket,
            string debugTag,
            CancellationToken cancellationToken)
        {
            var jsonParserState = new JsonParserState();

            using (var parser = new UnmanagedJsonParser(this, jsonParserState, debugTag))
            {
                var buffer = new ArraySegment <byte>(GetManagedBuffer());

                var writer = new BlittableJsonDocumentBuilder(this,
                                                              BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, jsonParserState);

                writer.ReadObject();
                var result = await webSocket.ReceiveAsync(buffer, cancellationToken);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    return(null);
                }

                parser.SetBuffer(buffer.Array, result.Count);
                while (writer.Read() == false)
                {
                    result = await webSocket.ReceiveAsync(buffer, cancellationToken);

                    parser.SetBuffer(buffer.Array, result.Count);
                }
                writer.FinalizeDocument();
                return(writer.CreateReader());
            }
        }
        private BlittableJsonReaderObject ReadObjectInternal(object builder, string documentId, BlittableJsonDocumentBuilder.UsageMode mode)
        {
            var state = new JsonParserState();

            using (var parser = new ObjectJsonParser(state, builder, this))
            {
                var writer = new BlittableJsonDocumentBuilder(this, mode, documentId, parser, state);
                try
                {
                    CachedProperties.NewDocument();
                    writer.ReadObject();
                    if (writer.Read() == false)
                    {
                        throw new InvalidOperationException("Partial content in object json parser shouldn't happen");
                    }
                    writer.FinalizeDocument();
                    _disposables.Add(writer);
                    return(writer.CreateReader());
                }
                catch (Exception)
                {
                    writer.Dispose();
                    throw;
                }
            }
        }
            public async Task <BlittableJsonReaderObject> ParseAsync(BlittableJsonDocumentBuilder.UsageMode mode, string debugTag)
            {
                var writer = new BlittableJsonDocumentBuilder(_context, mode, debugTag, _parser, _state);

                try
                {
                    writer.ReadObject();
                    _context.CachedProperties.NewDocument();
                    while (true)
                    {
                        if (_parser.BufferOffset == _parser.BufferSize)
                        {
                            var read = await _stream.ReadAsync(_buffer, 0, _buffer.Length);

                            if (read == 0)
                            {
                                throw new EndOfStreamException("Stream ended without reaching end of json content");
                            }
                            _parser.SetBuffer(_buffer, read);
                        }
                        else
                        {
                            _parser.SetBuffer(new ArraySegment <byte>(_buffer, _parser.BufferOffset, _parser.BufferSize));
                        }
                        if (writer.Read())
                        {
                            break;
                        }
                    }
                    writer.FinalizeDocument();
                    return(writer.CreateReader());
                }
                catch (Exception)
                {
                    writer.Dispose();
                    throw;
                }
            }
        private async Task <BlittableJsonReaderObject> ParseToMemoryAsync(Stream stream, string documentId, BlittableJsonDocumentBuilder.UsageMode mode)
        {
            var state  = new JsonParserState();
            var buffer = GetParsingBuffer();

            using (var parser = new UnmanagedJsonParser(this, state, documentId))
            {
                var writer = new BlittableJsonDocumentBuilder(this, mode, documentId, parser, state);
                try
                {
                    CachedProperties.NewDocument();
                    writer.ReadObject();
                    while (true)
                    {
                        var read = await stream.ReadAsync(buffer, 0, buffer.Length);

                        if (read == 0)
                        {
                            throw new EndOfStreamException("Stream ended without reaching end of json content");
                        }
                        parser.SetBuffer(buffer, read);
                        if (writer.Read())
                        {
                            break;
                        }
                    }
                    writer.FinalizeDocument();

                    _disposables.Add(writer);
                    return(writer.CreateReader());
                }
                catch (Exception)
                {
                    writer.Dispose();
                    throw;
                }
            }
        }