Ejemplo n.º 1
0
        void ParseObject(JsonNode obj, string jsonString, ref int index)
        {
            Token token;

            // {
            Token check = NextToken(jsonString, ref index);

            while (true)
            {
                token = PeekToken(jsonString, index);
                switch (token)
                {
                case Token.None:
                    // in some cases we need to exit here clean, check!
                    throw new InvalidDataException(string.Format("Missing data at position {0}!", index));

                case Token.Comma:
                    if (check == Token.ArrayOpen)
                    {
                        throw new InvalidDataException(string.Format("Json object, value or array expected at position {0}, got {1}!", index, token));
                    }

                    NextToken(jsonString, ref index);
                    break;

                case Token.ObjectClose:
                    if (check == Token.Comma)
                    {
                        throw new InvalidDataException(string.Format("Json object, value or array expected at position {0}, got {1}!", index, token));
                    }

                    NextToken(jsonString, ref index);
                    return;

                default:
                    // name
                    string name = ParseString(jsonString, ref index);

                    // :
                    token = NextToken(jsonString, ref index);
                    if (token != Token.Colon)
                    {
                        throw new InvalidDataException(string.Format("Missing colon in object definition!"));
                    }

                    // value
                    var sub = new JsonNode(JsonNodeType.Object, name);
                    ParseContent(sub, jsonString, ref index);
                    obj.Add(sub);
                    break;
                }
                check = token;
            }
        }
Ejemplo n.º 2
0
        void ParseArray(JsonNode array, string jsonString, ref int index)
        {
            if (array.Type == JsonNodeType.Array)
            {
                var newArray = new JsonNode(JsonNodeType.Array, string.Empty);
                array.Add(newArray);
                array = newArray;
            }
            else
            {
                array.ConvertToArray();
            }

            // [
            Token check = NextToken(jsonString, ref index);

            if (check != Token.ArrayOpen)
            {
                throw new InvalidDataException();
            }

            int i = 0;

            while (true)
            {
                Token token = PeekToken(jsonString, index);
                switch (token)
                {
                case Token.None:
                    // in some cases we need to exit here clean, check!
                    throw new InvalidDataException(string.Format("Json object, value or array expected at position {0}, got {1}!", index, token));

                case Token.Comma:
                    if (check == Token.ArrayOpen)
                    {
                        throw new InvalidDataException(string.Format("Json object, value or array expected at position {0}, got {1}!", index, token));
                    }

                    token = NextToken(jsonString, ref index);
                    break;

                case Token.ArrayClose:
                    if (check == Token.Comma)
                    {
                        throw new InvalidDataException(string.Format("Json object, value or array expected at position {0}, got {1}!", index, token));
                    }

                    token = NextToken(jsonString, ref index);
                    return;

                case Token.ObjectOpen:
                    var sub = new JsonNode(JsonNodeType.Object, i++.ToString());
                    ParseContent(sub, jsonString, ref index);
                    token = PeekToken(jsonString, index);
                    array.Add(sub);
                    break;

                default:
                    ParseContent(array, jsonString, ref index);
                    token = PeekToken(jsonString, index);
                    break;
                }
                check = token;
            }
        }