Code Coverage Statistics for Source File
Newtonsoft.Json\Linq\JArray.cs
Symbol Coverage: 93.83% (76 of 81)
Branch Coverage: 89.13% (41 of 46)
Cyclomatic Complexity Avg: 1.37 Max:5
Code Lines: 76
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
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 |
179 |
get { return JTokenType.Array; }
|
49 |
} |
|
50 |
||
51 |
/// <summary> |
|
52 |
/// Initializes a new instance of the <see cref="JArray"/> class. |
|
53 |
/// </summary> |
|
54 |
104 |
public JArray()
|
55 |
{ |
|
56 |
104 |
}
|
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 |
3 |
public JArray(JArray other)
|
63 |
3 |
: base(other)
|
64 |
{ |
|
65 |
3 |
}
|
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 |
77 |
public JArray(params object[] content)
|
72 |
77 |
: this((object)content)
|
73 |
{ |
|
74 |
77 |
}
|
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 |
102 |
public JArray(object content)
|
81 |
{ |
|
82 |
102 |
Add(content);
|
83 |
102 |
}
|
84 |
||
85 |
internal override bool DeepEquals(JToken node) |
|
86 |
{ |
|
87 |
16 |
JArray t = node as JArray;
|
88 |
16 |
return (t != null && ContentsEqual(t));
|
89 |
16 |
}
|
90 |
||
91 |
internal override JToken CloneToken() |
|
92 |
{ |
|
93 |
3 |
return new JArray(this);
|
94 |
3 |
}
|
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 JArray Load(JsonReader reader) |
|
102 |
{ |
|
103 |
13 |
if (reader.TokenType == JsonToken.None)
|
104 |
{ |
|
105 |
6 |
if (!reader.Read())
|
106 |
0 |
throw new Exception("Error reading JArray from JsonReader.");
|
107 |
} |
|
108 |
13 |
if (reader.TokenType != JsonToken.StartArray)
|
109 |
1 |
throw new Exception("Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
110 |
||
111 |
12 |
JArray a = new JArray();
|
112 |
12 |
a.SetLineInfo(reader as IJsonLineInfo);
|
113 |
||
114 |
12 |
if (!reader.Read())
|
115 |
0 |
throw new Exception("Error reading JArray from JsonReader.");
|
116 |
||
117 |
12 |
a.ReadContentFrom(reader);
|
118 |
||
119 |
12 |
return a;
|
120 |
12 |
}
|
121 |
||
122 |
/// <summary> |
|
123 |
/// Load a <see cref="JArray"/> from a string that contains JSON. |
|
124 |
/// </summary> |
|
125 |
/// <param name="json">A <see cref="String"/> that contains JSON.</param> |
|
126 |
/// <returns>A <see cref="JArray"/> populated from the string that contains JSON.</returns> |
|
127 |
public static JArray Parse(string json) |
|
128 |
{ |
|
129 |
6 |
JsonReader jsonReader = new JsonTextReader(new StringReader(json));
|
130 |
||
131 |
6 |
return Load(jsonReader);
|
132 |
5 |
}
|
133 |
||
134 |
/// <summary> |
|
135 |
/// Creates a <see cref="JArray"/> from an object. |
|
136 |
/// </summary> |
|
137 |
/// <param name="o">The object that will be used to create <see cref="JArray"/>.</param> |
|
138 |
/// <returns>A <see cref="JArray"/> with the values of the specified object</returns> |
|
139 |
public static new JArray FromObject(object o) |
|
140 |
{ |
|
141 |
2 |
return FromObject(o, new JsonSerializer());
|
142 |
2 |
}
|
143 |
||
144 |
/// <summary> |
|
145 |
/// Creates a <see cref="JArray"/> from an object. |
|
146 |
/// </summary> |
|
147 |
/// <param name="o">The object that will be used to create <see cref="JArray"/>.</param> |
|
148 |
/// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used to read the object.</param> |
|
149 |
/// <returns>A <see cref="JArray"/> with the values of the specified object</returns> |
|
150 |
public static new JArray FromObject(object o, JsonSerializer jsonSerializer) |
|
151 |
{ |
|
152 |
2 |
JToken token = FromObjectInternal(o, jsonSerializer);
|
153 |
||
154 |
2 |
if (token.Type != JTokenType.Array)
|
155 |
0 |
throw new ArgumentException("Object serialized to {0}. JArray instance expected.".FormatWith(CultureInfo.InvariantCulture, token.Type));
|
156 |
||
157 |
2 |
return (JArray)token;
|
158 |
2 |
}
|
159 |
||
160 |
/// <summary> |
|
161 |
/// Writes this token to a <see cref="JsonWriter"/>. |
|
162 |
/// </summary> |
|
163 |
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param> |
|
164 |
/// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param> |
|
165 |
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters) |
|
166 |
{ |
|
167 |
32 |
writer.WriteStartArray();
|
168 |
||
169 |
32 |
foreach (JToken token in Children())
|
170 |
{ |
|
171 |
80 |
token.WriteTo(writer, converters);
|
172 |
} |
|
173 |
||
174 |
32 |
writer.WriteEndArray();
|
175 |
32 |
}
|
176 |
||
177 |
/// <summary> |
|
178 |
/// Gets the <see cref="JToken"/> with the specified key. |
|
179 |
/// </summary> |
|
180 |
/// <value>The <see cref="JToken"/> with the specified key.</value> |
|
181 |
public override JToken this[object key] |
|
182 |
{ |
|
183 |
get |
|
184 |
{ |
|
185 |
33 |
ValidationUtils.ArgumentNotNull(key, "o");
|
186 |
||
187 |
33 |
if (!(key is int))
|
188 |
1 |
throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
189 |
||
190 |
32 |
return GetItem((int)key);
|
191 |
32 |
}
|
192 |
set |
|
193 |
{ |
|
194 |
2 |
ValidationUtils.ArgumentNotNull(key, "o");
|
195 |
||
196 |
2 |
if (!(key is int))
|
197 |
1 |
throw new ArgumentException("Set JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
198 |
||
199 |
1 |
SetItem((int)key, value);
|
200 |
1 |
}
|
201 |
} |
|
202 |
||
203 |
/// <summary> |
|
204 |
/// Gets or sets the <see cref="Newtonsoft.Json.Linq.JToken"/> at the specified index. |
|
205 |
/// </summary> |
|
206 |
/// <value></value> |
|
207 |
public JToken this[int index] |
|
208 |
{ |
|
209 |
87 |
get { return GetItem(index); }
|
210 |
1 |
set { SetItem(index, value); }
|
211 |
} |
|
212 |
||
213 |
#region IList<JToken> Members |
|
214 |
||
215 |
/// <summary> |
|
216 |
/// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>. |
|
217 |
/// </summary> |
|
218 |
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param> |
|
219 |
/// <returns> |
|
220 |
/// The index of <paramref name="item"/> if found in the list; otherwise, -1. |
|
221 |
/// </returns> |
|
222 |
public int IndexOf(JToken item) |
|
223 |
{ |
|
224 |
18 |
return IndexOfItem(item);
|
225 |
18 |
}
|
226 |
||
227 |
/// <summary> |
|
228 |
/// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index. |
|
229 |
/// </summary> |
|
230 |
/// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param> |
|
231 |
/// <param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param> |
|
232 |
/// <exception cref="T:System.ArgumentOutOfRangeException"> |
|
233 |
/// <paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception> |
|
234 |
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception> |
|
235 |
public void Insert(int index, JToken item) |
|
236 |
{ |
|
237 |
6 |
InsertItem(index, item);
|
238 |
4 |
}
|
239 |
||
240 |
/// <summary> |
|
241 |
/// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index. |
|
242 |
/// </summary> |
|
243 |
/// <param name="index">The zero-based index of the item to remove.</param> |
|
244 |
/// <exception cref="T:System.ArgumentOutOfRangeException"> |
|
245 |
/// <paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception> |
|
246 |
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception> |
|
247 |
public void RemoveAt(int index) |
|
248 |
{ |
|
249 |
4 |
RemoveItemAt(index);
|
250 |
2 |
}
|
251 |
||
252 |
#endregion |
|
253 |
||
254 |
#region ICollection<JToken> Members |
|
255 |
||
256 |
/// <summary> |
|
257 |
/// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>. |
|
258 |
/// </summary> |
|
259 |
/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> |
|
260 |
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception> |
|
261 |
public void Add(JToken item) |
|
262 |
{ |
|
263 |
24 |
Add((object)item);
|
264 |
23 |
}
|
265 |
||
266 |
/// <summary> |
|
267 |
/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>. |
|
268 |
/// </summary> |
|
269 |
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception> |
|
270 |
public void Clear() |
|
271 |
{ |
|
272 |
1 |
ClearItems();
|
273 |
1 |
}
|
274 |
||
275 |
/// <summary> |
|
276 |
/// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value. |
|
277 |
/// </summary> |
|
278 |
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> |
|
279 |
/// <returns> |
|
280 |
/// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. |
|
281 |
/// </returns> |
|
282 |
public bool Contains(JToken item) |
|
283 |
{ |
|
284 |
8 |
return ContainsItem(item);
|
285 |
8 |
}
|
286 |
||
287 |
void ICollection<JToken>.CopyTo(JToken[] array, int arrayIndex) |
|
288 |
{ |
|
289 |
5 |
CopyItemsTo(array, arrayIndex);
|
290 |
1 |
}
|
291 |
||
292 |
/// <summary> |
|
293 |
/// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>. |
|
294 |
/// </summary> |
|
295 |
/// <value></value> |
|
296 |
/// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</returns> |
|
297 |
public int Count |
|
298 |
{ |
|
299 |
46 |
get { return CountItems(); }
|
300 |
} |
|
301 |
||
302 |
bool ICollection<JToken>.IsReadOnly |
|
303 |
{ |
|
304 |
0 |
get { return false; }
|
305 |
} |
|
306 |
||
307 |
/// <summary> |
|
308 |
/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>. |
|
309 |
/// </summary> |
|
310 |
/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> |
|
311 |
/// <returns> |
|
312 |
/// 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"/>. |
|
313 |
/// </returns> |
|
314 |
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception> |
|
315 |
public bool Remove(JToken item) |
|
316 |
{ |
|
317 |
4 |
return RemoveItem(item);
|
318 |
4 |
}
|
319 |
||
320 |
#endregion |
|
321 |
||
322 |
internal override int GetDeepHashCode() |
|
323 |
{ |
|
324 |
4 |
return ContentsHashCode();
|
325 |
4 |
}
|
326 |
} |
|
327 |
} |