|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace LightJson |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Diagnostics; |
| 8 | + |
| 9 | + /// <summary> |
| 10 | + /// Represents an ordered collection of JsonValues. |
| 11 | + /// </summary> |
| 12 | + [DebuggerDisplay("Count = {Count}")] |
| 13 | + [DebuggerTypeProxy(typeof(JsonArrayDebugView))] |
| 14 | + internal sealed class JsonArray : IEnumerable<JsonValue> |
| 15 | + { |
| 16 | + private IList<JsonValue> items; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="JsonArray"/> class. |
| 20 | + /// </summary> |
| 21 | + public JsonArray() |
| 22 | + { |
| 23 | + this.items = new List<JsonValue>(); |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="JsonArray"/> class, adding the given values to the collection. |
| 28 | + /// </summary> |
| 29 | + /// <param name="values">The values to be added to this collection.</param> |
| 30 | + public JsonArray(params JsonValue[] values) |
| 31 | + : this() |
| 32 | + { |
| 33 | + foreach (var value in values) |
| 34 | + { |
| 35 | + this.items.Add(value); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the number of values in this collection. |
| 41 | + /// </summary> |
| 42 | + /// <value>The number of values in this collection.</value> |
| 43 | + public int Count |
| 44 | + { |
| 45 | + get |
| 46 | + { |
| 47 | + return this.items.Count; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets the value at the given index. |
| 53 | + /// </summary> |
| 54 | + /// <param name="index">The zero-based index of the value to get or set.</param> |
| 55 | + /// <remarks> |
| 56 | + /// The getter will return JsonValue.Null if the given index is out of range. |
| 57 | + /// </remarks> |
| 58 | + public JsonValue this[int index] |
| 59 | + { |
| 60 | + get |
| 61 | + { |
| 62 | + if (index >= 0 && index < this.items.Count) |
| 63 | + { |
| 64 | + return this.items[index]; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + return JsonValue.Null; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + set |
| 73 | + { |
| 74 | + this.items[index] = value; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Adds the given value to this collection. |
| 80 | + /// </summary> |
| 81 | + /// <param name="value">The value to be added.</param> |
| 82 | + /// <returns>Returns this collection.</returns> |
| 83 | + public JsonArray Add(JsonValue value) |
| 84 | + { |
| 85 | + this.items.Add(value); |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Inserts the given value at the given index in this collection. |
| 91 | + /// </summary> |
| 92 | + /// <param name="index">The index where the given value will be inserted.</param> |
| 93 | + /// <param name="value">The value to be inserted into this collection.</param> |
| 94 | + /// <returns>Returns this collection.</returns> |
| 95 | + public JsonArray Insert(int index, JsonValue value) |
| 96 | + { |
| 97 | + this.items.Insert(index, value); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Removes the value at the given index. |
| 103 | + /// </summary> |
| 104 | + /// <param name="index">The index of the value to be removed.</param> |
| 105 | + /// <returns>Return this collection.</returns> |
| 106 | + public JsonArray Remove(int index) |
| 107 | + { |
| 108 | + this.items.RemoveAt(index); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Clears the contents of this collection. |
| 114 | + /// </summary> |
| 115 | + /// <returns>Returns this collection.</returns> |
| 116 | + public JsonArray Clear() |
| 117 | + { |
| 118 | + this.items.Clear(); |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Determines whether the given item is in the JsonArray. |
| 124 | + /// </summary> |
| 125 | + /// <param name="item">The item to locate in the JsonArray.</param> |
| 126 | + /// <returns>Returns true if the item is found; otherwise, false.</returns> |
| 127 | + public bool Contains(JsonValue item) |
| 128 | + { |
| 129 | + return this.items.Contains(item); |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Determines the index of the given item in this JsonArray. |
| 134 | + /// </summary> |
| 135 | + /// <param name="item">The item to locate in this JsonArray.</param> |
| 136 | + /// <returns>The index of the item, if found. Otherwise, returns -1.</returns> |
| 137 | + public int IndexOf(JsonValue item) |
| 138 | + { |
| 139 | + return this.items.IndexOf(item); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Returns an enumerator that iterates through the collection. |
| 144 | + /// </summary> |
| 145 | + /// <returns>The enumerator that iterates through the collection.</returns> |
| 146 | + public IEnumerator<JsonValue> GetEnumerator() |
| 147 | + { |
| 148 | + return this.items.GetEnumerator(); |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Returns an enumerator that iterates through the collection. |
| 153 | + /// </summary> |
| 154 | + /// <returns>The enumerator that iterates through the collection.</returns> |
| 155 | + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| 156 | + { |
| 157 | + return this.GetEnumerator(); |
| 158 | + } |
| 159 | + |
| 160 | + private class JsonArrayDebugView |
| 161 | + { |
| 162 | + private JsonArray jsonArray; |
| 163 | + |
| 164 | + public JsonArrayDebugView(JsonArray jsonArray) |
| 165 | + { |
| 166 | + this.jsonArray = jsonArray; |
| 167 | + } |
| 168 | + |
| 169 | + [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] |
| 170 | + public JsonValue[] Items |
| 171 | + { |
| 172 | + get |
| 173 | + { |
| 174 | + var items = new JsonValue[this.jsonArray.Count]; |
| 175 | + |
| 176 | + for (int i = 0; i < this.jsonArray.Count; i += 1) |
| 177 | + { |
| 178 | + items[i] = this.jsonArray[i]; |
| 179 | + } |
| 180 | + |
| 181 | + return items; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments