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); }
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); }
public IEnumerable <NamedJsonBuffer> GetMembers() { JsonBufferReader reader = CreateReader(); reader.ReadToken(JsonTokenClass.Object); while (reader.TokenClass == JsonTokenClass.Member) { yield return(new NamedJsonBuffer(reader.Text, reader.BufferValue())); } }
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); }
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); }
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()); }
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]); }
/// <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); }
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); }
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); }
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); }
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); }
/// <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; }