Beispiel #1
0
        /// <summary>
        /// Reads a JSON array containing zero-or-more values.
        /// </summary>
        /// <returns>
        /// The new <see cref="JsonNode"/> instance.
        /// </returns>
        /// <exception cref="JsonParserException">
        /// If a syntax error was encountered whilst attempting to parse input content.
        /// Exception contains identifies the source of the error by providing the line
        /// number and position.
        /// </exception>
        private JsonNode ReadArray()
        {
            this.Accept();

            this.SkipWhitespace();

            var node = new JsonArrayNode();

            while (!this.HasReachedEnd)
            {
                if (this.Peek() == ']')
                {
                    this.Accept();
                    return(node);
                }
                else if (this.Peek() == ',' && node.Count != 0)
                {
                    this.Accept();

                    this.SkipWhitespace();
                    if (this.HasReachedEnd)
                    {
                        break;
                    }
                }

                node.Add(this.ReadValue());
                this.SkipWhitespace();
            }

            throw new JsonParserException("Expected ']' but reached end of input.", this.lineNumber, this.linePosition);
        }
Beispiel #2
0
        /// <inheritdoc/>
        public override JsonNode Clone()
        {
            var clone = new JsonArrayNode();

            foreach (var node in this.nodes)
            {
                if (node != null)
                {
                    clone.Add(node.Clone());
                }
                else
                {
                    clone.Add(null);
                }
            }
            return(clone);
        }
Beispiel #3
0
        /// <summary>
        /// Create array node and populate from a collection of values.
        /// </summary>
        /// <remarks>
        /// <para>Collection entries are cloned if they are already <see cref="JsonNode"/>
        /// instances.</para>
        /// </remarks>
        /// <param name="collection">The collection.</param>
        /// <returns>
        /// New <see cref="JsonArrayNode"/> instance containing zero or more nodes.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="collection"/> is <c>null</c>.
        /// </exception>
        public static JsonArrayNode FromCollection(IEnumerable collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }

            var node = new JsonArrayNode();

            foreach (var element in collection)
            {
                node.Add(ConvertFrom(element));
            }
            return(node);
        }
Beispiel #4
0
        /// <summary>
        /// Create array node and populate from a native array of values.
        /// </summary>
        /// <remarks>
        /// <para>Array elements are cloned if they are already <see cref="JsonNode"/>
        /// instances.</para>
        /// </remarks>
        /// <typeparam name="T">Type of array elements.</typeparam>
        /// <param name="array">Native array of objects.</param>
        /// <returns>
        /// New <see cref="JsonArrayNode"/> instance containing zero or more nodes.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="array"/> is <c>null</c>.
        /// </exception>
        public static JsonArrayNode FromArray <T>(T[] array)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            var node = new JsonArrayNode();

            foreach (T element in array)
            {
                node.Add(ConvertFrom(element));
            }
            return(node);
        }