예제 #1
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);
        }
예제 #2
0
 private JsonToken(JsonTokenClass clazz, string text)
 {
     _class = clazz;
     _text  = text;
 }
예제 #3
0
 private JsonToken(JsonTokenClass clazz) :
     this(clazz, null)
 {
 }