internal static JsonObject Parse(string str, ref int position)
        {
            EatSpaces(str, ref position);

            if (position >= str.Length)
            {
                return(null);
            }

            if (str[position] != '{')
            {
                throw new JsonParseException(str, position);
            }

            JsonObject jsonObject = new JsonObject();

            // Read all the pairs
            bool continueReading = true;

            // Read starting '{'
            position++;
            while (continueReading)
            {
                EatSpaces(str, ref position);
                if (str[position] != '}')
                {
                    // Read string
                    JsonString jsonString = JsonString.Parse(str, ref position);
                    string     key        = jsonString.Value;

                    // Read seperator ':'
                    EatSpaces(str, ref position);
                    if (str[position] != ':')
                    {
                        throw new JsonParseException(str, position);
                    }
                    position++;

                    // Read value
                    JsonValue value = ParseValue(str, ref position);

                    jsonObject.Add(key, value);
                }

                EatSpaces(str, ref position);
                if (str[position] == '}')
                {
                    continueReading = false;
                }
                else if (str[position] != ',')
                {
                    throw new JsonParseException(str, position);
                }

                // Skip "," between pair of items
                position++;
            }

            return(jsonObject);
        }
Пример #2
0
        /// <summary>
        /// Parses the JSON value into the JsonValue object
        /// </summary>
        /// <param name="str">The string to parse</param>
        /// <param name="position">The current position</param>
        /// <returns>The JsonValue if parsed, null otherwise.</returns>
        protected static JsonValue ParseValue(string str, ref int position)
        {
            JsonString.EatSpaces(str, ref position);

            char ch = str[position];

            // JsonString
            if (ch == '\"')
            {
                return(JsonString.Parse(str, ref position));
            }
            // JsonObject
            else if (ch == '{')
            {
                return(JsonObject.Parse(str, ref position));
            }
            // JsonArray
            else if (ch == '[')
            {
                return(JsonArray.Parse(str, ref position));
            }
            // JsonNumber
            else if (JsonNumber.IsNumberPart(ch))
            {
                return(JsonNumber.Parse(str, ref position));
            }
            // 'null'
            else if ((str.Length > position + 4) &&
                     (str.Substring(position, 4).Equals("null", StringComparison.InvariantCultureIgnoreCase)))
            {
                position += 4;
                return(null);
            }
            // 'true'
            else if ((str.Length > position + 4) &&
                     (str.Substring(position, 4).Equals("true", StringComparison.InvariantCultureIgnoreCase)))
            {
                position += 4;
                return(new JsonBoolean(true));
            }
            // 'false'
            else if ((str.Length > position + 5) &&
                     (str.Substring(position, 5).Equals("false", StringComparison.InvariantCultureIgnoreCase)))
            {
                position += 5;
                return(new JsonBoolean(false));
            }
            else
            {
                throw new JsonParseException(str, position);
            }
        }