コード例 #1
0
        /// <summary>
        /// Get the next value. The value can be wrapped in quotes. 
        /// The value can be empty.
        /// </summary>

        private string GetValue(TextParser parser) 
        {
            char ch;
            
            do 
            {
                ch = parser.Next();
            } 
            while (ch <= ' ' && ch != TextParser.EOF);

            switch (ch) 
            {
                case TextParser.EOF:
                    return null;
                case '"':
                case '\'':
                    return parser.NextString(ch);
                case ',':
                    parser.Back();
                    return "";
                default:
                    parser.Back();
                    return parser.NextTo(',');
            }
        }
コード例 #2
0
        /// <summary>
        /// Gets the next value as object. The value can be a Boolean, a Double,
        /// an Integer, an object array, a JObject, a String, or 
        /// JObject.NULL.
        /// </summary>
        
        private object NextObject(TextParser parser)
        {
            Debug.Assert(parser != null);

            char ch = NextClean(parser);

            //
            // String
            //

            if (ch == '"' || ch == '\'') 
                return _output.ToStringPrimitive(parser.NextString(ch));

            //
            // Object
            //

            if (ch == '{') 
            {
                parser.Back();
                return ParseObject(parser);
            }

            //
            // JSON Array
            //

            if (ch == '[')
            {
                parser.Back();
                return ParseArray(parser);
            }

            StringBuilder sb = new StringBuilder();
            char b = ch;
            
            while (ch >= ' ' && ch != ':' && ch != ',' && ch != ']' && ch != '}' && ch != '/')
            {
                sb.Append(ch);
                ch = parser.Next();
            }

            parser.Back();

            string s = sb.ToString().Trim();
            
            if (s == "true")
                return _output.TruePrimitive;
            
            if (s == "false")
                return _output.FalsePrimitive;

            if (s == "null")
                return _output.NullPrimitive;
            
            if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') 
            {
                object number = _output.ToNumberPrimitive(s);

                if (number == null)
                    throw new ParseException(string.Format("Cannot convert '{0}' to a number.", s));

                return number;
            }

            if (s.Length == 0)
                throw new ParseException("Missing value.");

            return s;
        }