All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Linq / JArray.cs
1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
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
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
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.
24 #endregion
25
26 using System;
27 using System.Collections.Generic;
28 using System.ComponentModel;
29 using System.Linq;
30 using System.Text;
31 using Newtonsoft.Json.Utilities;
32 using System.IO;
33 using System.Globalization;
34
35 namespace Newtonsoft.Json.Linq
36 {
37   /// <summary>
38   /// Represents a JSON array.
39   /// </summary>
40   public class JArray : JContainer, IList<JToken>
41   {
42     /// <summary>
43     /// Gets the node type for this <see cref="JToken"/>.
44     /// </summary>
45     /// <value>The type.</value>
46     public override JTokenType Type
47     {
48       get { return JTokenType.Array; }
49     }
50
51     /// <summary>
52     /// Initializes a new instance of the <see cref="JArray"/> class.
53     /// </summary>
54     public JArray()
55     {
56     }
57
58     /// <summary>
59     /// Initializes a new instance of the <see cref="JArray"/> class from another <see cref="JArray"/> object.
60     /// </summary>
61     /// <param name="other">A <see cref="JArray"/> object to copy from.</param>
62     public JArray(JArray other)
63       : base(other)
64     {
65     }
66
67     /// <summary>
68     /// Initializes a new instance of the <see cref="JArray"/> class with the specified content.
69     /// </summary>
70     /// <param name="content">The contents of the array.</param>
71     public JArray(params object[] content)
72       : this((object)content)
73     {
74     }
75
76     /// <summary>
77     /// Initializes a new instance of the <see cref="JArray"/> class with the specified content.
78     /// </summary>
79     /// <param name="content">The contents of the array.</param>
80     public JArray(object content)
81     {
82       Add(content);
83     }
84
85     internal override bool DeepEquals(JToken node)
86     {
87       JArray t = node as JArray;
88       return (t != null && ContentsEqual(t));
89     }
90
91     internal override JToken CloneToken()
92     {
93       return new JArray(this);
94     }
95
96     /// <summary>
97     /// Loads an <see cref="JArray"/> from a <see cref="JsonReader"/>. 
98     /// </summary>
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)
102     {
103       if (reader.TokenType == JsonToken.None)
104       {
105         if (!reader.Read())
106           throw new Exception("Error reading JArray from JsonReader.");
107       }
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));
110
111       JArray a = new JArray();
112       a.SetLineInfo(reader as IJsonLineInfo);
113
114       a.ReadTokenFrom(reader);
115
116       return a;
117     }
118
119     /// <summary>
120     /// Load a <see cref="JArray"/> from a string that contains JSON.
121     /// </summary>
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)
125     {
126       JsonReader jsonReader = new JsonTextReader(new StringReader(json));
127
128       return Load(jsonReader);
129     }
130
131     /// <summary>
132     /// Creates a <see cref="JArray"/> from an object.
133     /// </summary>
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)
137     {
138       return FromObject(o, new JsonSerializer());
139     }
140
141     /// <summary>
142     /// Creates a <see cref="JArray"/> from an object.
143     /// </summary>
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)
148     {
149       JToken token = FromObjectInternal(o, jsonSerializer);
150
151       if (token.Type != JTokenType.Array)
152         throw new ArgumentException("Object serialized to {0}. JArray instance expected.".FormatWith(CultureInfo.InvariantCulture, token.Type));
153
154       return (JArray)token;
155     }
156
157     /// <summary>
158     /// Writes this token to a <see cref="JsonWriter"/>.
159     /// </summary>
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)
163     {
164       writer.WriteStartArray();
165
166       foreach (JToken token in Children())
167       {
168         token.WriteTo(writer, converters);
169       }
170
171       writer.WriteEndArray();
172     }
173
174     /// <summary>
175     /// Gets the <see cref="JToken"/> with the specified key.
176     /// </summary>
177     /// <value>The <see cref="JToken"/> with the specified key.</value>
178     public override JToken this[object key]
179     {
180       get
181       {
182         ValidationUtils.ArgumentNotNull(key, "o");
183
184         if (!(key is int))
185           throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
186
187         return GetItem((int)key);
188       }
189       set
190       {
191         ValidationUtils.ArgumentNotNull(key, "o");
192
193         if (!(key is int))
194           throw new ArgumentException("Set JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
195
196         SetItem((int)key, value);
197       }
198     }
199
200     /// <summary>
201     /// Gets or sets the <see cref="Newtonsoft.Json.Linq.JToken"/> at the specified index.
202     /// </summary>
203     /// <value></value>
204     public JToken this[int index]
205     {
206       get { return GetItem(index); }
207       set { SetItem(index, value); }
208     }
209
210     #region IList<JToken> Members
211
212     /// <summary>
213     /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
214     /// </summary>
215     /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
216     /// <returns>
217     /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
218     /// </returns>
219     public int IndexOf(JToken item)
220     {
221       return IndexOfItem(item);
222     }
223
224     /// <summary>
225     /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
226     /// </summary>
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)
233     {
234       InsertItem(index, item);
235     }
236
237     /// <summary>
238     /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
239     /// </summary>
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)
245     {
246       RemoveItemAt(index);
247     }
248
249     #endregion
250
251     #region ICollection<JToken> Members
252
253     /// <summary>
254     /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
255     /// </summary>
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)
259     {
260       Add((object)item);
261     }
262
263     /// <summary>
264     /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
265     /// </summary>
266     /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
267     public void Clear()
268     {
269       ClearItems();
270     }
271
272     /// <summary>
273     /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
274     /// </summary>
275     /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
276     /// <returns>
277     /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
278     /// </returns>
279     public bool Contains(JToken item)
280     {
281       return ContainsItem(item);
282     }
283
284     void ICollection<JToken>.CopyTo(JToken[] array, int arrayIndex)
285     {
286       CopyItemsTo(array, arrayIndex);
287     }
288
289     /// <summary>
290     /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
291     /// </summary>
292     /// <value></value>
293     /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</returns>
294     public int Count
295     {
296       get { return CountItems(); }
297     }
298
299     bool ICollection<JToken>.IsReadOnly
300     {
301       get { return false; }
302     }
303
304     /// <summary>
305     /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
306     /// </summary>
307     /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
308     /// <returns>
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"/>.
310     /// </returns>
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)
313     {
314       return RemoveItem(item);
315     }
316
317     #endregion
318
319     internal override int GetDeepHashCode()
320     {
321       return ContentsHashCode();
322     }
323   }
324 }