public void ArrayInsideObject() { ParserOutput output = new ParserOutput(); Assert.IsNull(output.TestCurrentArray, "No current array initially."); Assert.IsNull(output.TestCurrentObject, "No current object initially."); Assert.AreEqual(0, output.TestStack.Count, "Stack is empty initially."); output.StartObject(); Assert.IsNotNull(output.TestCurrentObject, "Has object when creating an object."); Assert.IsNull(output.TestCurrentArray, "No array when creating an object."); Assert.AreEqual(0, output.TestStack.Count, "Stack empty at top-level." ); output.StartArray(); Assert.IsNotNull(output.TestCurrentArray, "Has array when creating an array, even while inside creating an object."); Assert.IsNull(output.TestCurrentObject, "No object when creating an array, even while inside creating an object."); Assert.AreEqual(1, output.TestStack.Count, "One object pending on stack."); output.EndArray(); Assert.IsNotNull(output.TestCurrentObject, "Back to working with an object after finishing the nested array."); Assert.IsNull(output.TestCurrentArray, "No array when back to creating the last object."); Assert.AreEqual(0, output.TestStack.Count, "Stack empty since returned to top-level."); output.EndObject(); Assert.IsNull(output.TestCurrentArray, "No current array when done."); Assert.IsNull(output.TestCurrentObject, "No current object when done."); Assert.AreEqual(0, output.TestStack.Count, "Stack is empty when done."); }
public void EmptyMemberName() { ParserOutput output = new ParserOutput(); output.StartObject(); output.ObjectPut(string.Empty, string.Empty); JObject o = (JObject) output.EndObject(); Assert.AreEqual(1, o.Count); Assert.AreEqual(string.Empty, o[string.Empty]); }
/// <summary> /// Deserializes the next object from JSON data using a give type /// system. /// </summary> public virtual object DeserializeNext(IParserOutput output) { if (output == null) output = new ParserOutput(); MoveToContent(); switch (Token) { case JsonToken.String: return output.ToStringPrimitive(Text); case JsonToken.Number: return output.ToNumberPrimitive(Text); case JsonToken.Boolean : return Text == JsonReader.FalseText ? output.FalsePrimitive : output.TruePrimitive; case JsonToken.Null : return output.NullPrimitive; case JsonToken.Array : { output.StartArray(); while (ReadToken() != JsonToken.EndArray) output.ArrayPut(DeserializeNext(output)); return output.EndArray(); } case JsonToken.Object : { output.StartObject(); while (ReadToken() != JsonToken.EndObject) { if (Token != JsonToken.Member) throw new JsonException("Expecting member."); string name = Text; Read(); output.ObjectPut(name, DeserializeNext(output)); } return output.EndObject(); } case JsonToken.EOF : throw new JsonException("Unexpected EOF."); default : throw new JsonException(string.Format("{0} not expected.", this.Token)); } }
public virtual object DeserializeNext(IParserOutput output) { if (output == null) output = new ParserOutput(); MoveToContent(); if (TokenClass == JsonTokenClass.String) { return output.ToStringPrimitive(ReadString()); } else if (TokenClass == JsonTokenClass.Number) { return output.ToNumberPrimitive(ReadNumber().ToString()); } else if (TokenClass == JsonTokenClass.Boolean) { return ReadBoolean(); } else if (TokenClass == JsonTokenClass.Null) { ReadNull(); return output.NullPrimitive; } else if (TokenClass == JsonTokenClass.Array) { Read(); output.StartArray(); while (TokenClass != JsonTokenClass.EndArray) output.ArrayPut(DeserializeNext(output)); Read(); return output.EndArray(); } else if (TokenClass == JsonTokenClass.Object) { Read(); output.StartObject(); while (TokenClass != JsonTokenClass.EndObject) { string name = ReadMember(); output.ObjectPut(name, DeserializeNext(output)); } Read(); return output.EndObject(); } else { throw new JsonException(string.Format("{0} not expected.", TokenClass)); } }