コード例 #1
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;
            }
        }
コード例 #2
0
 /// <summary>
 /// Adds a subnode to this node. This may only be used for JsonNodeType.Array and JsonNodeType.Object.
 /// </summary>
 /// <param name="item"></param>
 public void Add(JsonNode item)
 {
     CheckAdd(JsonNodeType.Object);
     AddItem(item);
 }