2 // Copyright (c) 2007 James Newton-King
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using System.Collections.Generic;
28 using System.ComponentModel;
31 using Newtonsoft.Json.Utilities;
33 using System.Globalization;
35 namespace Newtonsoft.Json.Linq
38 /// Represents a JSON array.
40 public class JArray : JContainer, IList<JToken>
43 /// Gets the node type for this <see cref="JToken"/>.
45 /// <value>The type.</value>
46 public override JTokenType Type
48 get { return JTokenType.Array; }
52 /// Initializes a new instance of the <see cref="JArray"/> class.
59 /// Initializes a new instance of the <see cref="JArray"/> class from another <see cref="JArray"/> object.
61 /// <param name="other">A <see cref="JArray"/> object to copy from.</param>
62 public JArray(JArray other)
68 /// Initializes a new instance of the <see cref="JArray"/> class with the specified content.
70 /// <param name="content">The contents of the array.</param>
71 public JArray(params object[] content)
72 : this((object)content)
77 /// Initializes a new instance of the <see cref="JArray"/> class with the specified content.
79 /// <param name="content">The contents of the array.</param>
80 public JArray(object content)
85 internal override bool DeepEquals(JToken node)
87 JArray t = node as JArray;
88 return (t != null && ContentsEqual(t));
91 internal override JToken CloneToken()
93 return new JArray(this);
97 /// Loads an <see cref="JArray"/> from a <see cref="JsonReader"/>.
99 /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.</param>
100 /// <returns>A <see cref="JArray"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
101 public static new JArray Load(JsonReader reader)
103 if (reader.TokenType == JsonToken.None)
106 throw new Exception("Error reading JArray from JsonReader.");
108 if (reader.TokenType != JsonToken.StartArray)
109 throw new Exception("Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
111 JArray a = new JArray();
112 a.SetLineInfo(reader as IJsonLineInfo);
114 a.ReadTokenFrom(reader);
120 /// Load a <see cref="JArray"/> from a string that contains JSON.
122 /// <param name="json">A <see cref="String"/> that contains JSON.</param>
123 /// <returns>A <see cref="JArray"/> populated from the string that contains JSON.</returns>
124 public static new JArray Parse(string json)
126 JsonReader jsonReader = new JsonTextReader(new StringReader(json));
128 return Load(jsonReader);
132 /// Creates a <see cref="JArray"/> from an object.
134 /// <param name="o">The object that will be used to create <see cref="JArray"/>.</param>
135 /// <returns>A <see cref="JArray"/> with the values of the specified object</returns>
136 public static new JArray FromObject(object o)
138 return FromObject(o, new JsonSerializer());
142 /// Creates a <see cref="JArray"/> from an object.
144 /// <param name="o">The object that will be used to create <see cref="JArray"/>.</param>
145 /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used to read the object.</param>
146 /// <returns>A <see cref="JArray"/> with the values of the specified object</returns>
147 public static new JArray FromObject(object o, JsonSerializer jsonSerializer)
149 JToken token = FromObjectInternal(o, jsonSerializer);
151 if (token.Type != JTokenType.Array)
152 throw new ArgumentException("Object serialized to {0}. JArray instance expected.".FormatWith(CultureInfo.InvariantCulture, token.Type));
154 return (JArray)token;
158 /// Writes this token to a <see cref="JsonWriter"/>.
160 /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
161 /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
162 public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
164 writer.WriteStartArray();
166 foreach (JToken token in Children())
168 token.WriteTo(writer, converters);
171 writer.WriteEndArray();
175 /// Gets the <see cref="JToken"/> with the specified key.
177 /// <value>The <see cref="JToken"/> with the specified key.</value>
178 public override JToken this[object key]
182 ValidationUtils.ArgumentNotNull(key, "o");
185 throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
187 return GetItem((int)key);
191 ValidationUtils.ArgumentNotNull(key, "o");
194 throw new ArgumentException("Set JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
196 SetItem((int)key, value);
201 /// Gets or sets the <see cref="Newtonsoft.Json.Linq.JToken"/> at the specified index.
204 public JToken this[int index]
206 get { return GetItem(index); }
207 set { SetItem(index, value); }
210 #region IList<JToken> Members
213 /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
215 /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
217 /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
219 public int IndexOf(JToken item)
221 return IndexOfItem(item);
225 /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
227 /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
228 /// <param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
229 /// <exception cref="T:System.ArgumentOutOfRangeException">
230 /// <paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception>
231 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
232 public void Insert(int index, JToken item)
234 InsertItem(index, item);
238 /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
240 /// <param name="index">The zero-based index of the item to remove.</param>
241 /// <exception cref="T:System.ArgumentOutOfRangeException">
242 /// <paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception>
243 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
244 public void RemoveAt(int index)
251 #region ICollection<JToken> Members
254 /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
256 /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
257 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
258 public void Add(JToken item)
264 /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
266 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
273 /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
275 /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
277 /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
279 public bool Contains(JToken item)
281 return ContainsItem(item);
284 void ICollection<JToken>.CopyTo(JToken[] array, int arrayIndex)
286 CopyItemsTo(array, arrayIndex);
290 /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
293 /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</returns>
296 get { return CountItems(); }
299 bool ICollection<JToken>.IsReadOnly
301 get { return false; }
305 /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
307 /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
309 /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
311 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
312 public bool Remove(JToken item)
314 return RemoveItem(item);
319 internal override int GetDeepHashCode()
321 return ContentsHashCode();