private JsonToken ParseObject() { if (NextClean() != '{') { throw new JsonException("An object must begin with '{'."); } return(Yield(JsonToken.Object(), ParseObjectMemberMethod)); }
/// <summary> /// Parses expecting an object in the source. /// </summary> private JsonToken ParseObject() { if (NextClean() != '{') { throw SyntaxError("An object must begin with '{'."); } return(Yield(JsonToken.Object(), ParseObjectMemberMethod)); }
public void ToObject() { JsonBuffer obj = NamedJsonBuffer.ToObject( new NamedJsonBuffer("foo", JsonBuffer.From("bar")), new NamedJsonBuffer("qux", JsonBuffer.From("quux"))); Assert.AreEqual(6, obj.Length); Assert.AreEqual(JsonToken.Object(), obj[0]); Assert.AreEqual(JsonToken.Member("foo"), obj[1]); Assert.AreEqual(JsonToken.String("bar"), obj[2]); Assert.AreEqual(JsonToken.Member("qux"), obj[3]); Assert.AreEqual(JsonToken.String("quux"), obj[4]); Assert.AreEqual(JsonToken.EndObject(), obj[5]); }
public void JsonTokenString() { Assert.AreEqual("String:hello", JsonToken.String("hello").ToString()); Assert.AreEqual("Number:123", JsonToken.Number("123").ToString()); Assert.AreEqual("Boolean:true", JsonToken.Boolean(true).ToString()); Assert.AreEqual("Boolean:false", JsonToken.Boolean(false).ToString()); Assert.AreEqual("Null:null", JsonToken.Null().ToString()); Assert.AreEqual("Array", JsonToken.Array().ToString()); Assert.AreEqual("EndArray", JsonToken.EndArray().ToString()); Assert.AreEqual("Object", JsonToken.Object().ToString()); Assert.AreEqual("EndObject", JsonToken.EndObject().ToString()); Assert.AreEqual("Member:test", JsonToken.Member("test").ToString()); Assert.AreEqual("BOF", JsonToken.BOF().ToString()); Assert.AreEqual("EOF", JsonToken.EOF().ToString()); }
protected override void WriteStartObjectImpl() { Write(JsonToken.Object()); }
protected override JsonToken ReadTokenImpl() { var baseReader = _reader; var index = _index; if (index < 0) { // // If the base reader was never started then do it now. // Check for zero depth is the same as BOF. // if (baseReader.Depth == 0) { baseReader.ReadToken(JsonTokenClass.Object); _innerDepth = baseReader.Depth; } _index = 0; return(JsonToken.Object()); } // // Buffered members, if any, get served first. // var bufferedMembers = _bufferedMembers; if (bufferedMembers != null && index < bufferedMembers.Count) { // // Is there a value to serve? // var value = _memberValue; if (!value.IsEmpty) { if (value.IsStructured) // JSON Array or Object { // // Get a reader on the structured (array or // object) value if not already acquired. // var valueReader = _memberStructuredValueReader ?? (_memberStructuredValueReader = value.CreateReader()); // // Serve tokens from the value reader until OEF. // if (valueReader.Read()) { return(valueReader.Token); } // // Proceed with next. // _memberValue = JsonBuffer.Empty; _memberStructuredValueReader = null; _index++; return(ReadTokenImpl()); } else // JSON Null, Boolean, Number or String { Debug.Assert(value.IsNull || value.IsScalar); // // Establish state to proceed with next and // serve token pertaining to scalar value. // _memberValue = JsonBuffer.Empty; _index++; return(value.IsNull ? JsonToken.Null() : value.CreateReader().Token); } } // // Serve buffered member name and establish state so // that the value will be served next. // var member = bufferedMembers[index]; _memberValue = member.Buffer; return(JsonToken.Member(member.Name)); } // // Done with buffered members so now move on to remaining // members from the underlying reader. // if (baseReader == null) { return(JsonToken.EOF()); // Done with serving } // // If base reader is on the object end at the same depth as // on entry then serve the last end token. The reader is // released so that on next read, the above will signal EOF. // if (baseReader.Depth == _innerDepth && baseReader.TokenClass == JsonTokenClass.EndObject) { _reader = null; } // // Move through the base reader. // var token = baseReader.Token; baseReader.Read(); return(token); }
public void ObjectTokens() { Assert.AreEqual(JsonTokenClass.Object, JsonToken.Object().Class); Assert.AreEqual(JsonTokenClass.EndObject, JsonToken.EndObject().Class); }
public MockedJsonReader Object() { return(Append(JsonToken.Object())); }