Пример #1
0
        public static bool Read(PeepingTomStream stream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            while (parser.Read() == false)
            {
                var read = stream.Read(buffer.Buffer.Array, buffer.Buffer.Offset, buffer.Length);
                if (read == 0)
                {
                    if (state.CurrentTokenType != JsonParserToken.EndObject)
                    {
                        throw new EndOfStreamException("Stream ended without reaching end of json content");
                    }

                    return(false);
                }

                parser.SetBuffer(buffer, 0, read);
            }
            return(true);
        }
Пример #2
0
        public static async Task <bool> ReadAsync(PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            if (parser.Read())
            {
                return(true);
            }

            var read = await peepingTomStream.ReadAsync(buffer.Buffer.Array, buffer.Buffer.Offset, buffer.Length).ConfigureAwait(false);

            if (read == 0)
            {
                if (state.CurrentTokenType != JsonParserToken.EndObject)
                {
                    throw new EndOfStreamException("Stream ended without reaching end of json content");
                }

                return(false);
            }

            parser.SetBuffer(buffer, 0, read);
            return(parser.Read());
        }
Пример #3
0
        public static unsafe string ReadString(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            if (Read(peepingTomStream, parser, state, buffer) == false)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            if (state.CurrentTokenType == JsonParserToken.Null)
            {
                return(null);
            }

            if (state.CurrentTokenType != JsonParserToken.String)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            return(context.AllocateStringValue(null, state.StringBuffer, state.StringSize).ToString());
        }
Пример #4
0
        public static void ThrowInvalidJson(string msg, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser)
        {
            string peepedWindow = GetPeepingTomBufferAsString(peepingTomStream);

            throw new InvalidOperationException("Invalid JSON. " + msg + " on " + parser.GenerateErrorState() + ". Last 4KB of read strem : '" + peepedWindow + "'");
        }
Пример #5
0
        public static IEnumerable <BlittableJsonReaderObject> ReadArrayToMemory(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            if (Read(peepingTomStream, parser, state, buffer) == false)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            if (state.CurrentTokenType != JsonParserToken.StartArray)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            while (true)
            {
                if (Read(peepingTomStream, parser, state, buffer) == false)
                {
                    ThrowInvalidJson(peepingTomStream);
                }

                if (state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    break;
                }

                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "readArray/singleResult", parser, state))
                {
                    ReadObject(builder, peepingTomStream, parser, buffer);

                    yield return(builder.CreateReader());
                }
            }
        }
Пример #6
0
        public static async Task ReadObjectAsync(BlittableJsonDocumentBuilder builder, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            builder.ReadNestedObject();
            while (builder.Read() == false)
            {
                var read = await peepingTomStream.ReadAsync(buffer.Buffer.Array, buffer.Buffer.Offset, buffer.Length).ConfigureAwait(false);

                if (read == 0)
                {
                    throw new EndOfStreamException("Stream ended without reaching end of json content");
                }

                parser.SetBuffer(buffer, 0, read);
            }
            builder.FinalizeDocument();
        }
Пример #7
0
        public static long ReadLong(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            if (Read(peepingTomStream, parser, state, buffer) == false)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            if (state.CurrentTokenType != JsonParserToken.Integer)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            return(state.Long);
        }
Пример #8
0
        public static void ReadObject(BlittableJsonDocumentBuilder builder, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            builder.ReadNestedObject();
            while (builder.Read() == false)
            {
                var read = peepingTomStream.Read(buffer.Buffer.Array, buffer.Buffer.Offset, buffer.Length);
                if (read == 0)
                {
                    throw new EndOfStreamException("Stream ended without reaching end of json content" + GetPeepingTomBufferAsString(peepingTomStream));
                }

                parser.SetBuffer(buffer, 0, read);
            }
            builder.FinalizeDocument();
        }