Пример #1
0
        public unsafe BlittableJsonReaderArray ParseBufferToArray(string value, string debugTag,
                                                                  BlittableJsonDocumentBuilder.UsageMode mode, IBlittableDocumentModifier modifier = null)
        {
            if (_disposed)
            {
                ThrowObjectDisposed();
            }

            _jsonParserState.Reset();
            using (var parser = new UnmanagedJsonParser(this, _jsonParserState, debugTag))
                using (var builder = new BlittableJsonDocumentBuilder(this, mode, debugTag, parser, _jsonParserState, modifier: modifier))
                    using (GetManagedBuffer(out var buffer))
                    {
                        CachedProperties.NewDocument();
                        builder.ReadArrayDocument();

                        var maxChars = buffer.Length / 8; //utf8 max size is 8 bytes, must consider worst case possiable

                        bool lastReadResult = false;
                        for (int i = 0; i < value.Length; i += maxChars)
                        {
                            var charsToRead = Math.Min(value.Length - i, maxChars);
                            var length      = Encodings.Utf8.GetBytes(value, i,
                                                                      charsToRead,
                                                                      buffer.Buffer.Array,
                                                                      buffer.Buffer.Offset);

                            parser.SetBuffer(buffer.Pointer, length);
                            lastReadResult = builder.Read();
                        }
                        if (lastReadResult == false)
                        {
                            throw new EndOfStreamException("Buffer ended without reaching end of json content");
                        }

                        builder.FinalizeDocument();

                        var reader = builder.CreateArrayReader(false);
                        return(reader);
                    }
        }
Пример #2
0
        public async Task <BlittableJsonReaderArray> ParseArrayToMemoryAsync(Stream stream, string debugTag,
                                                                             BlittableJsonDocumentBuilder.UsageMode mode)
        {
            _jsonParserState.Reset();
            ManagedPinnedBuffer bytes;

            using (GetManagedBuffer(out bytes))
                using (var parser = new UnmanagedJsonParser(this, _jsonParserState, debugTag))
                {
                    var writer = new BlittableJsonDocumentBuilder(this, mode, debugTag, parser, _jsonParserState);
                    try
                    {
                        CachedProperties.NewDocument();
                        writer.ReadArrayDocument();
                        while (true)
                        {
                            var read = await stream.ReadAsync(bytes.Buffer.Array, bytes.Buffer.Offset, bytes.Length);

                            if (read == 0)
                            {
                                throw new EndOfStreamException("Stream ended without reaching end of json content");
                            }
                            parser.SetBuffer(bytes, read);
                            if (writer.Read())
                            {
                                break;
                            }
                        }
                        writer.FinalizeDocument();
                        // here we "leak" the memory used by the array, in practice this is used
                        // in short scoped context, so we don't care
                        return(writer.CreateArrayReader());
                    }
                    catch (Exception)
                    {
                        writer.Dispose();
                        throw;
                    }
                }
        }