// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. namespace LightJson { using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; /// /// Represents an ordered collection of JsonValues. /// [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(JsonArrayDebugView))] internal sealed class JsonArray : IEnumerable { private IList items; /// /// Initializes a new instance of the class. /// public JsonArray() { this.items = new List(); } /// /// Initializes a new instance of the class, adding the given values to the collection. /// /// The values to be added to this collection. public JsonArray(params JsonValue[] values) : this() { if (values == null) { throw new ArgumentNullException(nameof(values)); } foreach (var value in values) { this.items.Add(value); } } /// /// Gets the number of values in this collection. /// /// The number of values in this collection. public int Count { get { return this.items.Count; } } /// /// Gets or sets the value at the given index. /// /// The zero-based index of the value to get or set. /// /// The getter will return JsonValue.Null if the given index is out of range. /// public JsonValue this[int index] { get { if (index >= 0 && index < this.items.Count) { return this.items[index]; } else { return JsonValue.Null; } } set { this.items[index] = value; } } /// /// Adds the given value to this collection. /// /// The value to be added. /// Returns this collection. public JsonArray Add(JsonValue value) { this.items.Add(value); return this; } /// /// Inserts the given value at the given index in this collection. /// /// The index where the given value will be inserted. /// The value to be inserted into this collection. /// Returns this collection. public JsonArray Insert(int index, JsonValue value) { this.items.Insert(index, value); return this; } /// /// Removes the value at the given index. /// /// The index of the value to be removed. /// Return this collection. public JsonArray Remove(int index) { this.items.RemoveAt(index); return this; } /// /// Clears the contents of this collection. /// /// Returns this collection. public JsonArray Clear() { this.items.Clear(); return this; } /// /// Determines whether the given item is in the JsonArray. /// /// The item to locate in the JsonArray. /// Returns true if the item is found; otherwise, false. public bool Contains(JsonValue item) { return this.items.Contains(item); } /// /// Determines the index of the given item in this JsonArray. /// /// The item to locate in this JsonArray. /// The index of the item, if found. Otherwise, returns -1. public int IndexOf(JsonValue item) { return this.items.IndexOf(item); } /// /// Returns an enumerator that iterates through the collection. /// /// The enumerator that iterates through the collection. public IEnumerator GetEnumerator() { return this.items.GetEnumerator(); } /// /// Returns an enumerator that iterates through the collection. /// /// The enumerator that iterates through the collection. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } [ExcludeFromCodeCoverage] private class JsonArrayDebugView { private JsonArray jsonArray; public JsonArrayDebugView(JsonArray jsonArray) { this.jsonArray = jsonArray; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public JsonValue[] Items { get { var items = new JsonValue[this.jsonArray.Count]; for (int i = 0; i < this.jsonArray.Count; i += 1) { items[i] = this.jsonArray[i]; } return items; } } } } }