File: JsonArray.cs

package info (click to toggle)
keepass2-plugin-keepassrpc 2.0.2%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,304 kB
  • sloc: cs: 29,001; makefile: 14
file content (472 lines) | stat: -rw-r--r-- 13,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#region License, Terms and Conditions
//
// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
// Written by Atif Aziz (www.raboof.com)
// Copyright (c) 2005 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Jayrock.Json
{
    #region Imports

    using System;
    using System.Collections;
    using System.Globalization;
    using System.IO;
    using Jayrock.Json.Conversion;

    #if !NET_1_0 && !NET_1_1
    using System.Collections.Generic;
    #endif

    #endregion

    /// <summary>
    /// An ordered sequence of values. This class also provides a number of
    /// methods that can be found on a JavaScript Array for sake of parity.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Public Domain 2002 JSON.org, ported to C# by Are Bjolseth (teleplan.no)
    /// and re-adapted by Atif Aziz (www.raboof.com)</para>
    /// </remarks>

    [ Serializable ]
    public class JsonArray : CollectionBase, IJsonImportable, IJsonExportable
        #if !NET_1_0 && !NET_1_1
        , IList<object>
        #endif
    {
        public JsonArray() {}

        public JsonArray(IEnumerable collection)
        {
            foreach (object item in collection)
                InnerList.Add(item);
        }

        public virtual object this[int index]
        {
            get { return InnerList[index]; }
            set { InnerList[index] = value; }
        }

        public int Length
        {
            get { return Count; }
        }

        public void Put(object value)
        {
            Add(value);
        }

        public virtual void Add(object value)
        {
            InnerList.Add(value);
        }

        public virtual void Remove(object value)
        {
            InnerList.Remove(value);
        }

        public virtual bool Contains(object value)
        {
            return InnerList.Contains(value);
        }

        public virtual int IndexOf(object value)
        {
            return InnerList.IndexOf(value);
        }

        public virtual bool HasValueAt(int index)
        {
            return this[index] != null;
        }

        public virtual object GetValue(int index)
        {
            return GetValue(index, null);
        }

        public virtual object GetValue(int index, object defaultValue)
        {
            object value = this[index];
            return value != null ? value : defaultValue;
        }

        public virtual bool GetBoolean(int index)
        {
            return GetBoolean(index, false);
        }

        public virtual bool GetBoolean(int index, bool defaultValue)
        {
            object value = GetValue(index);
            if (value == null) return defaultValue;
            return Convert.ToBoolean(value, CultureInfo.InvariantCulture);
        }

        public virtual double GetDouble(int index)
        {
            return GetDouble(index, float.NaN);
        }

        public virtual double GetDouble(int index, float defaultValue)
        {
            object value = GetValue(index);
            if (value == null) return defaultValue;
            return Convert.ToDouble(value, CultureInfo.InvariantCulture);
        }

        public virtual int GetInt32(int index)
        {
            return GetInt32(index, 0);
        }

        public virtual int GetInt32(int index, int defaultValue)
        {
            object value = GetValue(index);
            if (value == null) return defaultValue;
            return Convert.ToInt32(value, CultureInfo.InvariantCulture);
        }

        public virtual string GetString(int index)
        {
            return GetString(index, string.Empty);
        }

        public virtual string GetString(int index, string defaultValue)
        {
            object value = GetValue(index);
            if (value == null) return defaultValue;
            return value.ToString();
        }

        public virtual JsonArray GetArray(int index)
        {
            return (JsonArray) GetValue(index);
        }

        public virtual JsonObject GetObject(int index)
        {
            return (JsonObject) GetValue(index);
        }

        /// <summary>
        /// Make an JSON external form string of this JsonArray. For
        /// compactness, no unnecessary whitespace is added.
        /// </summary>
        /// <remarks>
        /// This method assumes that the data structure is acyclical.
        /// </remarks>

        public override string ToString()
        {
            StringWriter writer = new StringWriter();
            Export(JsonText.CreateWriter(writer));
            return writer.ToString();
        }
        
        /// <summary>
        /// Make an JSON external form string of this JsonArray. For
        /// compactness, no unnecessary whitespace is added.
        /// </summary>
        /// <remarks>
        /// This method assumes that the data structure is acyclical.
        /// </remarks>

        public virtual void Export(JsonWriter writer)
        {
            Export(JsonConvert.CreateExportContext(), writer);
        }

        void IJsonExportable.Export(ExportContext context, JsonWriter writer)
        {
            Export(context, writer);
        }

        protected virtual void Export(ExportContext context, JsonWriter writer)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (writer == null)
                throw new ArgumentNullException("writer");
            
            writer.WriteStartArray();

            foreach (object value in this)
                context.Export(value, writer);
            
            writer.WriteEndArray();
        }
        
        public virtual void Import(JsonReader reader)
        {
            Import(JsonConvert.CreateImportContext(), reader);
        }

        void IJsonImportable.Import(ImportContext context, JsonReader reader)
        {
            Import(context, reader);
        }

        protected virtual void Import(ImportContext context, JsonReader reader)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            
            if (reader == null)
                throw new ArgumentNullException("reader");
            
            //
            // IMPORTANT! A new list is created and then committed to make
            // sure that this method is exception-safe. If something goes
            // wrong during the import of elements then this instance 
            // will remain largely untouched.
            //
            
            ArrayList list = new ArrayList();
            
            reader.ReadToken(JsonTokenClass.Array);
            
            while (reader.TokenClass != JsonTokenClass.EndArray)
                list.Add(context.Import(reader));
            
            reader.Read();
            
            InnerList.Clear();
            InnerList.AddRange(list);
        }

        /// <summary>
        /// Copies the elements to a new object array.
        /// </summary>

        public virtual object[] ToArray()
        {
            return (object[]) ToArray(typeof(object));
        }

        /// <summary>
        /// Copies the elements to a new array of the specified type.
        /// </summary>

        public virtual Array ToArray(Type elementType)
        {
            return InnerList.ToArray(elementType);
        }

        public virtual void Reverse()
        {
            InnerList.Reverse();
        }

        //
        // Methods that imitate the JavaScript array methods.
        //

        /// <summary>
        /// Appends new elements to an array.
        /// </summary>
        /// <returns>
        /// The new length of the array.
        /// </returns>
        /// <remarks>
        /// This method appends elements in the order in which they appear. If
        /// one of the arguments is an array, it is added as a single element.
        /// Use the <see cref="Concat"/> method to join the elements from two or
        /// more arrays.
        /// </remarks>
        
        public virtual int Push(object value)
        {
            Add(value);
            return Count;
        }

        /// <summary>
        /// Appends new elements to an array.
        /// </summary>
        /// <returns>
        /// The new length of the array.
        /// </returns>
        /// <remarks>
        /// This method appends elements in the order in which they appear. If
        /// one of the arguments is an array, it is added as a single element.
        /// Use the <see cref="Concat"/> method to join the elements from two or
        /// more arrays.
        /// </remarks>

        public virtual int Push(params object[] values)
        {
            if (values != null)
            {
                foreach (object value in values)
                    Push(value);
            }

            return Count;
        }

        /// <summary>
        /// Removes the last element from an array and returns it.
        /// </summary>
        /// <remarks>
        /// If the array is empty, null is returned.
        /// </remarks>

        public virtual object Pop()
        {
            if (Count == 0)
                return null;

            object lastValue = InnerList[Count - 1];
            RemoveAt(Count - 1);
            return lastValue;
        }

        /// <summary>
        /// Returns a new array consisting of a combination of two or more
        /// arrays.
        /// </summary>

        public virtual JsonArray Concat(params object[] values)
        {
            JsonArray newArray = new JsonArray(this);

            if (values != null)
            {
                foreach (object value in values)
                {
                    JsonArray arrayValue = value as JsonArray;
                    
                    if (arrayValue != null)
                    {
                        foreach (object arrayValueValue in arrayValue)
                            newArray.Push(arrayValueValue);
                    }
                    else
                    {
                        newArray.Push(value);
                    }
                }
            }

            return newArray;
        }

        /// <summary>
        /// Removes the first element from an array and returns it.
        /// </summary>

        public virtual object Shift()
        {
            if (Count == 0)
                return null;

            object firstValue = InnerList[0];
            RemoveAt(0);
            return firstValue;
        }

        /// <summary>
        /// Inserts the specified element at the beginning of the array.
        /// </summary>
        /// <remarks>
        /// The unshift method inserts elements into the start of an array, so
        /// they appear in the same order in which they appear in the argument
        /// list.
        /// </remarks>

        public virtual void Unshift(object value)
        {
            InnerList.Insert(0, value);
        }

        /// <summary>
        /// Inserts the specified elements at the beginning of the array.
        /// </summary>
        /// <remarks>
        /// The unshift method inserts elements into the start of an array, so
        /// they appear in the same order in which they appear in the argument
        /// list.
        /// </remarks>

        public virtual void Unshift(params object[] values)
        {
            if (values != null)
            {
                foreach (object value in values)
                    Unshift(value);
            }
        }

        protected override void OnValidate(object value)
        {
            // NOP other base implementation does not allow null values.
        }

        #if !NET_1_0 && !NET_1_1

        /// <summary>
        /// Returns an enumerator that iterates through the elements
        /// of the array.
        /// </summary>

        public new IEnumerator<object> GetEnumerator()
        {
            foreach (object item in InnerList)
                yield return item;
        }

        /// <summary>
        /// Copies the elements to an <see cref="T:System.Array"/>, 
        /// starting at a particular given index.
        /// </summary>

        public void CopyTo(object[] array, int arrayIndex)
        {
            InnerList.CopyTo(array, arrayIndex);
        }

        bool ICollection<object>.Remove(object item)
        {
            int index = IndexOf(item);
            if (index < 0)
                return false;
            RemoveAt(index);
            return true;
        }

        bool ICollection<object>.IsReadOnly
        {
            get { return InnerList.IsReadOnly; }
        }

        void IList<object>.Insert(int index, object item)
        {
            List.Insert(index, item);
        }
        
        #endif // !NET_1_0 && !NET_1_1
    }
}