Represents a reader that provides fast, non-cached, forward-only access to a JsonBuffer object.
Inheritance: Jayrock.Json.JsonReaderBase
Exemple #1
0
        public void Issue26()
        {
            // 1. Create JsonBuffer from array with objects
            JsonBuffer buffer = JsonBuffer.From(@"[{},{a:{},b:{}}]");
            // 2. Create reader from the buffer...
            JsonBufferReader reader = buffer.CreateReader();

            //    ...read in the first object
            while (reader.TokenClass != JsonTokenClass.Object)
            {
                reader.Read();
            }
            reader.Read(); // Read Object token
            reader.Read(); // Read EndObject token

            //    ...create a subbuffer to buffer the next object
            JsonBuffer subBuffer = JsonBuffer.From(reader);
            //    ...create reader from the subbuffer
            JsonBufferReader reader2 = subBuffer.CreateReader();

            // 3. Call reader.BufferValue() this should break
            JsonBuffer buffer2 = reader2.BufferValue();

            Assert.IsTrue(buffer2.IsObject);
        }
Exemple #2
0
        private static void AssertBufferedValueScalarOrNull(JsonToken expected, JsonBufferWriter writer)
        {
            JsonBuffer       buffer = writer.GetBuffer();
            JsonBufferReader reader = buffer.CreateReader();

            reader.Read();
            reader.Read();
            JsonBuffer value = reader.BufferValue();

            if (expected.Class == JsonTokenClass.Null)
            {
                Assert.IsTrue(value.IsNull);
            }
            else
            {
                Assert.IsTrue(value.IsScalar);
            }
            JsonBufferReader vr = value.CreateReader();

            Assert.AreEqual(1, vr.Depth);
            Assert.AreEqual(expected, vr.Token);
            vr.Read();
            vr.ReadToken(JsonTokenClass.EndArray);
            Assert.IsTrue(vr.EOF);
        }
Exemple #3
0
        public IEnumerable <NamedJsonBuffer> GetMembers()
        {
            JsonBufferReader reader = CreateReader();

            reader.ReadToken(JsonTokenClass.Object);
            while (reader.TokenClass == JsonTokenClass.Member)
            {
                yield return(new NamedJsonBuffer(reader.Text, reader.BufferValue()));
            }
        }
Exemple #4
0
        public void BufferValueOnEOF()
        {
            JsonBufferWriter writer = new JsonBufferWriter();

            writer.WriteFromReader(JsonText.CreateReader("[]"));
            JsonBufferReader reader = writer.GetBuffer().CreateReader();

            reader.Read(); // array start
            reader.Read(); // array end
            Assert.IsTrue(reader.BufferValue().IsEmpty);
        }
Exemple #5
0
        public void BufferValueOnMember()
        {
            JsonBufferWriter writer = new JsonBufferWriter();

            writer.WriteFromReader(JsonText.CreateReader("{foo:bar}"));
            JsonBufferReader reader = writer.GetBuffer().CreateReader();

            reader.Read(); // object start
            reader.Read(); // foo
            Assert.AreEqual(JsonToken.String("bar"), reader.BufferValue().CreateReader().Token);
        }
Exemple #6
0
        public void BufferScalarValue()
        {
            JsonBufferWriter writer = new JsonBufferWriter();

            writer.WriteString("foobar");
            JsonBuffer       buffer = writer.GetBuffer();
            JsonBufferReader reader = buffer.CreateReader();

            reader.Read(); // array start
            reader.Read(); // string
            Assert.AreEqual("foobar", reader.BufferValue().CreateReader().BufferValue().CreateReader().ReadString());
        }
Exemple #7
0
        public void BufferValueOnObjectEnd()
        {
            JsonBufferWriter writer = new JsonBufferWriter();

            writer.WriteFromReader(JsonText.CreateReader("[{},foo]"));
            JsonBufferReader reader = writer.GetBuffer().CreateReader();

            reader.Read(); // outer object start
            reader.Read(); // inner object start
            reader.Read(); // inner object end
            Assert.AreEqual(JsonToken.String("foo"), reader.BufferValue()[0]);
        }
Exemple #8
0
        /// <summary>
        /// Creates a <see cref="JsonBufferReader" /> object that can be
        /// used to read the content of the buffer.
        /// </summary>
        /// <remarks>
        /// If the buffer contains a JSON null or scalar value then the
        /// returned reader is already started and positioned on the value.
        /// </remarks>

        public JsonBufferReader CreateReader()
        {
            if (IsEmpty)
            {
                throw new InvalidOperationException();
            }
            JsonBufferReader reader = new JsonBufferReader(this);

            if (!IsStructured)
            {
                reader.ReadToken(JsonTokenClass.Array);
            }
            return(reader);
        }
Exemple #9
0
        public int GetMembers(NamedJsonBuffer[] members, int index, int count)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", index, null);
            }

            JsonBufferReader reader = CreateReader();

            if (!reader.MoveToContent())
            {
                throw new JsonException("Unexpected EOF.");
            }

            if (reader.TokenClass == JsonTokenClass.Null)
            {
                return(0);
            }

            reader.ReadToken(JsonTokenClass.Object);
            int readCount = 0;

            while (reader.TokenClass == JsonTokenClass.Member)
            {
                if (count-- == 0)
                {
                    return(~readCount);
                }

                if (members != null)
                {
                    members[index++] = new NamedJsonBuffer(reader.Text, reader.BufferValue());
                }
                else
                {
                    reader.Skip();
                }

                readCount++;
            }

            return(readCount);
        }
Exemple #10
0
        public int GetArray(JsonBuffer[] values, int index, int count)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", index, null);
            }

            JsonBufferReader reader = CreateReader();

            if (!reader.MoveToContent())
            {
                throw new JsonException("Unexpected EOF.");
            }

            if (reader.TokenClass == JsonTokenClass.Null)
            {
                return(0);
            }

            reader.ReadToken(JsonTokenClass.Array);
            int readCount = 0;

            while (reader.TokenClass != JsonTokenClass.EndArray)
            {
                if (count-- == 0)
                {
                    return(~readCount);
                }

                if (values != null)
                {
                    values[index++] = reader.BufferValue();
                }
                else
                {
                    reader.Skip();
                }

                readCount++;
            }

            return(readCount);
        }
Exemple #11
0
        public static JsonBuffer From(JsonReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            JsonBufferReader bufferReader = reader as JsonBufferReader;

            if (bufferReader != null)
            {
                return(bufferReader.BufferValue());
            }

            if (!reader.MoveToContent())
            {
                return(Empty);
            }

            if (reader.TokenClass == JsonTokenClass.Member)
            {
                reader.Read();
            }

            bool structured = reader.TokenClass == JsonTokenClass.Array ||
                              reader.TokenClass == JsonTokenClass.Object;

            JsonBufferWriter writer = new JsonBufferWriter();

            writer.MaxDepth = reader.MaxDepth;
            writer.WriteFromReader(reader);
            JsonBuffer buffer = writer.GetBuffer();

            if (!structured)
            {
                bufferReader = buffer.CreateReader();
                bufferReader.MoveToContent();
                bufferReader.Read();
                buffer = bufferReader.BufferValue();
            }

            return(buffer);
        }
Exemple #12
0
        public void WritingReading()
        {
            JsonBufferWriter writer = new JsonBufferWriter();

            writer.WriteStartArray();
            writer.WriteString("foo");
            writer.WriteString("bar");
            writer.WriteString("baz");
            writer.WriteEndArray();
            JsonBuffer buffer = writer.GetBuffer();

            Assert.AreEqual(5, buffer.Length);
            JsonBufferReader reader = buffer.CreateReader();

            reader.ReadToken(JsonTokenClass.Array);
            Assert.AreEqual("foo", reader.ReadString());
            Assert.AreEqual("bar", reader.ReadString());
            Assert.AreEqual("baz", reader.ReadString());
            reader.ReadToken(JsonTokenClass.EndArray);
            Assert.IsTrue(reader.EOF);
        }
Exemple #13
0
 /// <summary>
 /// Creates a <see cref="JsonBufferReader" /> object that can be
 /// used to read the content of the buffer.
 /// </summary>
 /// <remarks>
 /// If the buffer contains a JSON null or scalar value then the
 /// returned reader is already started and positioned on the value.
 /// </remarks>
 public JsonBufferReader CreateReader()
 {
     if (IsEmpty)
         throw new InvalidOperationException();
     JsonBufferReader reader = new JsonBufferReader(this);
     if (!IsStructured)
         reader.ReadToken(JsonTokenClass.Array);
     return reader;
 }