Inheritance: IObjectReference
        public string ReadToken(JsonTokenClass token)
        {
            int depth = Depth;

            if (!token.IsTerminator)
            {
                MoveToContent();
            }
            string result;

            if (depth == 0 && TokenClass == JsonTokenClass.Array && (token.IsScalar || token == JsonTokenClass.Null))
            {
                Read();
                result = ReadToken(token);
                ReadToken(JsonTokenClass.EndArray);
            }
            else
            {
                if (TokenClass != token)
                {
                    throw new JsonException($"Found {TokenClass} where {token} was expected.");
                }
                result = Text;
                Read();
            }
            return(result);
        }
Esempio n. 2
0
 private void AssertTokenText(JsonTokenClass token, string text)
 {
     Assert.IsTrue(_reader.Read());
     Assert.AreEqual(token, _reader.TokenClass, "Found {0} (with text \x201c{1}\x201d) when expecting {2} (with text \x201c{3}\x201d).", _reader.TokenClass, _reader.Text, token, text);
     if (text != null)
     {
         Assert.AreEqual(text, _reader.Text);
     }
 }
        private NamedJsonBufferList _members; // buffered ones

        public FreeJsonMemberReadingHelper(JsonReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            JsonTokenClass clazz = reader.TokenClass;

            if (clazz != JsonTokenClass.BOF &&
                clazz != JsonTokenClass.Object &&
                clazz != JsonTokenClass.Member)
            {
                throw new ArgumentException(null, "reader");
            }

            _reader = reader;
        }
Esempio n. 4
0
        /// <summary>
        /// Reads the next token ensuring that it matches the specified
        /// token. If not, an exception is thrown.
        /// </summary>

        public string ReadToken(JsonTokenClass token)
        {
            int depth = Depth;

            if (!token.IsTerminator)
            {
                MoveToContent();
            }

            //
            // We allow an exception to the simple case of validating
            // the token and returning its value. If the reader is still at
            // the start (depth is zero) and we're being asked to check
            // for the null token or a scalar-type token then we allow that
            // to be appear within a one-length array. This is done because
            // valid JSON text must begin with an array or object. Our
            // JsonWriterBase automatically wraps a scalar value in an
            // array if not done explicitly. This exception here allow
            // that case to pass as being logically valid, as if the
            // token appeared entirely on its own between BOF and EOF.
            //

            string text;

            if (depth == 0 && TokenClass == JsonTokenClass.Array &&
                (token.IsScalar || token == JsonTokenClass.Null))
            {
                Read(/* array */);
                text = ReadToken(token);
                ReadToken(JsonTokenClass.EndArray);
            }
            else
            {
                if (TokenClass != token)
                {
                    throw new JsonException(string.Format("Found {0} where {1} was expected.", TokenClass, token));
                }

                text = Text;
                Read();
            }

            return(text);
        }
Esempio n. 5
0
        /// <summary>
        /// Buffers the value at which the reader is positioned.
        /// </summary>
        /// <returns>Returns a <see cref="JsonBuffer" /> object that holds
        /// the buffered value.</returns>

        public JsonBuffer BufferValue()
        {
            if (EOF)
            {
                return(JsonBuffer.Empty);
            }

            JsonTokenClass tokenClass = TokenClass;

            if (tokenClass.IsTerminator || tokenClass == JsonTokenClass.Member)
            {
                Read();
            }

            int start = _index;

            Skip();
            return(_buffer.Slice(start, _index));
        }
Esempio n. 6
0
        public static JsonBuffer From(JsonToken token)
        {
            JsonTokenClass clazz = token.Class;

            if (clazz == JsonTokenClass.Null)
            {
                return(_null);
            }

            if (!clazz.IsScalar)
            {
                throw new ArgumentException("Token must represent a JSON scalar value or null.", "token");
            }

            if (clazz == JsonTokenClass.Boolean)
            {
                return(token.Equals(JsonToken.True()) ? _true : _false);
            }

            JsonBufferStorage storage = new JsonBufferStorage(1);

            storage.Write(token);
            return(storage.ToBuffer());
        }
Esempio n. 7
0
 JsonToken(JsonTokenClass clazz, string text = null)
 {
     Class = clazz;
     Text  = text;
 }
Esempio n. 8
0
 private JsonToken(JsonTokenClass clazz, string text)
 {
     _class = clazz;
     _text  = text;
 }
Esempio n. 9
0
        private void ExitBracket()
        {
            JsonTokenClass bracket = (JsonTokenClass) _brackets.Pop();

            if (bracket == JsonTokenClass.BOF)
                bracket = JsonTokenClass.EOF;
            
            _currentBracket = bracket;
        }
Esempio n. 10
0
 private JsonToken(JsonTokenClass clazz) :
     this(clazz, null) {}
 private void AssertMember(string name, JsonTokenClass valueToken)
 {
     AssertMember(name, valueToken, null);
 }
 private void AssertToken(JsonTokenClass token)
 {
     AssertTokenText(token, null);
 }
Esempio n. 13
0
 private JsonToken(JsonTokenClass clazz) :
     this(clazz, null)
 {
 }
        /// <summary>
        /// Attempts to locate a member with a given (case-sensitive) name
        /// and returns a <see cref="JsonReader"/> that can be used to read
        /// the value. Otherwise it returns <c>null</c>.
        /// </summary>
        /// <remarks>
        /// The caller should not use the returned <see cref="JsonReader"/>
        /// instance for any other purpose but reading the value. It is
        /// possible that this method will return the same instance as
        /// <see cref="BaseReader"/> or a separate instance.
        /// </remarks>

        public JsonReader TryReadMember(string name)
        {
            //
            // Is the member already buffered? If yes then return a reader
            // on its buffered value.
            //

            JsonBuffer value = TryPopBufferedMember(name);

            if (!value.IsEmpty)
            {
                return(value.CreateReader());
            }

            //
            // Use the base reader from here on if it has not already been
            // exhausted...
            //

            if (_ended)
            {
                return(null);
            }

            JsonReader reader = BaseReader;

            if (!_started)
            {
                _started = true;
                if (!reader.MoveToContent())
                {
                    throw new JsonException(string.Format(
                                                @"Unexpected EOF while attempting to look for member named '{0}'.",
                                                name));
                }

                JsonTokenClass clazz = reader.TokenClass;
                if (clazz != JsonTokenClass.Object &&
                    clazz != JsonTokenClass.Member)
                {
                    throw new JsonException(string.Format(
                                                @"Found {0} where a JSON Object or Member was expected.", clazz));
                }
            }

            //
            // If the base reader is sitting on the start of an object then
            // move into it. This case should only arise on the first read
            // into a JSON object.
            //

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

            //
            // Go over the entire JSON object until its end.
            //

            while (reader.TokenClass != JsonTokenClass.EndObject)
            {
                //
                // Read the next member and if it matches what's being
                // sought then simply return the base reader that
                // should be aligned on the value.
                //

                string actualName = reader.ReadMember();
                if (string.CompareOrdinal(actualName, name) == 0)
                {
                    return(reader);
                }

                //
                // Not the sought member so buffer it to be served
                // later when it is sought or as part of the tail.
                //

                NamedJsonBufferList members = _members;
                if (members == null)
                {
                    members = _members = new NamedJsonBufferList(4);
                }
                members.Add(new NamedJsonBuffer(actualName, JsonBuffer.From(reader)));
            }

            _ended = true;

            //
            // Member not found.
            //

            return(null);
        }
Esempio n. 15
0
 public JsonWriter()
 {
     _brackets = new Stack(4);
     _currentBracket = JsonTokenClass.BOF;
 }
Esempio n. 16
0
 private void AssertToken(JsonTokenClass token)
 {
     AssertTokenText(token, null);
 }
Esempio n. 17
0
        /// <summary>
        /// Reads the next token ensuring that it matches the specified 
        /// token. If not, an exception is thrown.
        /// </summary>

        public string ReadToken(JsonTokenClass token)
        {
            MoveToContent();
            
            if (TokenClass != token)
                throw new JsonException(string.Format("Found {0} where {1} was expected.", TokenClass.ToString(), token.ToString()));
            
            string s = Text;
            Read();
            return s;
        }
 private void AssertTokenText(JsonTokenClass token, string text)
 {
     Assert.IsTrue(_reader.Read());
     Assert.AreEqual(token, _reader.TokenClass, "Found {0} (with text \x201c{1}\x201d) when expecting {2} (with text \x201c{3}\x201d).", _reader.TokenClass, _reader.Text, token, text);
     if (text != null)
         Assert.AreEqual(text, _reader.Text);
 }
Esempio n. 19
0
 private void AssertMember(string name, JsonTokenClass valueToken)
 {
     AssertMember(name, valueToken, null);
 }
 private void AssertMember(string name, JsonTokenClass valueToken, string valueText)
 {
     AssertTokenText(JsonTokenClass.Member, name);
     AssertTokenText(valueToken, valueText);
 }
Esempio n. 21
0
 private void AssertMember(string name, JsonTokenClass valueToken, string valueText)
 {
     AssertTokenText(JsonTokenClass.Member, name);
     AssertTokenText(valueToken, valueText);
 }
Esempio n. 22
0
        /// <summary>
        /// Reads the next token ensuring that it matches the specified 
        /// token. If not, an exception is thrown.
        /// </summary>
        public string ReadToken(JsonTokenClass token)
        {
            int depth = Depth;

            if (!token.IsTerminator)
                MoveToContent();

            //
            // We allow an exception to the simple case of validating
            // the token and returning its value. If the reader is still at
            // the start (depth is zero) and we're being asked to check
            // for the null token or a scalar-type token then we allow that
            // to be appear within a one-length array. This is done because
            // valid JSON text must begin with an array or object. Our
            // JsonWriterBase automatically wraps a scalar value in an
            // array if not done explicitly. This exception here allow
            // that case to pass as being logically valid, as if the
            // token appeared entirely on its own between BOF and EOF.
            //

            string text;

            if (depth == 0 && TokenClass == JsonTokenClass.Array &&
                (token.IsScalar || token == JsonTokenClass.Null))
            {
                Read(/* array */);
                text = ReadToken(token);
                ReadToken(JsonTokenClass.EndArray);
            }
            else
            {
                if (TokenClass != token)
                    throw new JsonException(string.Format("Found {0} where {1} was expected.", TokenClass, token));

                text = Text;
                Read();
            }

            return text;
        }
Esempio n. 23
0
 private JsonToken(JsonTokenClass clazz)
 {
     this = new JsonToken(clazz, null);
 }
Esempio n. 24
0
 private JsonToken(JsonTokenClass clazz, string text)
 {
     _class = clazz;
     _text = text;
 }
Esempio n. 25
0
 private void EnterBracket(JsonTokenClass newBracket)
 {
     Debug.Assert(newBracket == JsonTokenClass.Array || newBracket == JsonTokenClass.Object);
     
     _brackets.Push(_currentBracket);
     _currentBracket = newBracket;
 }