示例#1
0
        private JsonToken ParseNextMember()
        {
            switch (NextClean())
            {
            case ';':
            case ',':
            {
                if (NextClean() == '}')
                {
                    return(Yield(JsonToken.EndObject()));
                }
                break;
            }

            case '}':
                return(Yield(JsonToken.EndObject()));

            default:
                throw SyntaxError("Expected a ',' or '}'.");
            }

            _reader.Back();
            string name = Parse().Text;

            return(Yield(JsonToken.Member(name), ParseObjectMemberValueMethod));
        }
示例#2
0
        public void MemberToken()
        {
            JsonToken token = JsonToken.Member("test");

            Assert.AreEqual(JsonTokenClass.Member, token.Class);
            Assert.AreEqual("test", token.Text);
        }
示例#3
0
        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]);
        }
示例#4
0
 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());
 }
        private JsonToken ParseObjectMember()
        {
            switch (NextClean())
            {
            case '}':
                return(Yield(JsonToken.EndObject()));

            case '\0':
                throw new JsonException("An object must end with '}'.");

            default:
            {
                _reader.Back();
                string text = Parse().Text;
                return(Yield(JsonToken.Member(text), ParseObjectMemberValueMethod));
            }
            }
        }
示例#6
0
        /// <summary>
        /// Parses the first member name of the object or the end of the array
        /// in case of an empty object.
        /// </summary>

        private JsonToken ParseObjectMember()
        {
            char ch = NextClean();

            if (ch == '}')
            {
                return(Yield(JsonToken.EndObject()));
            }

            if (ch == BufferedCharReader.EOF)
            {
                throw SyntaxError("An object must end with '}'.");
            }

            _reader.Back();
            string name = Parse().Text;

            return(Yield(JsonToken.Member(name), ParseObjectMemberValueMethod));
        }
        private JsonToken ParseNextMember()
        {
            switch (NextClean())
            {
            case ',':
            case ';':
            {
                if (NextClean() == '}')
                {
                    return(Yield(JsonToken.EndObject()));
                }
                _reader.Back();
                string text = Parse().Text;
                return(Yield(JsonToken.Member(text), ParseObjectMemberValueMethod));
            }

            case '}':
                return(Yield(JsonToken.EndObject()));

            default:
                throw new JsonException("Expected a ',' or '}'.");
            }
        }
示例#8
0
 protected override void WriteMemberImpl(string name)
 {
     Write(JsonToken.Member(name));
 }
            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);
            }
示例#10
0
 public MockedJsonReader Member(string name)
 {
     return(Append(JsonToken.Member(name)));
 }