Allow passing null to JsonArray.Add()
[mono.git] / mcs / class / System.Json / System.Json / JsonArray.cs
1
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Globalization;
6 using System.IO;
7 using System.Text;
8
9 namespace System.Json
10 {
11         public class JsonArray : JsonValue, IList<JsonValue>
12         {
13                 List<JsonValue> list;
14
15                 public JsonArray (params JsonValue [] items)
16                 {
17                         list = new List<JsonValue> ();
18                         AddRange (items);
19                 }
20
21                 public JsonArray (IEnumerable<JsonValue> items)
22                 {
23                         if (items == null)
24                                 throw new ArgumentNullException ("items");
25
26                         list = new List<JsonValue> (items);
27                 }
28
29                 public override int Count {
30                         get { return list.Count; }
31                 }
32
33                 public bool IsReadOnly {
34                         get { return false; }
35                 }
36
37                 public override sealed JsonValue this [int index] {
38                         get { return list [index]; }
39                         set { list [index] = value; }
40                 }
41
42                 public override JsonType JsonType {
43                         get { return JsonType.Array; }
44                 }
45
46                 public void Add (JsonValue item)
47                 {
48                         list.Add (item);
49                 }
50
51                 public void AddRange (IEnumerable<JsonValue> items)
52                 {
53                         if (items == null)
54                                 throw new ArgumentNullException ("items");
55
56                         list.AddRange (items);
57                 }
58
59                 public void AddRange (params JsonValue [] items)
60                 {
61                         if (items == null)
62                                 return;
63
64                         list.AddRange (items);
65                 }
66
67                 public void Clear ()
68                 {
69                         list.Clear ();
70                 }
71
72                 public bool Contains (JsonValue item)
73                 {
74                         return list.Contains (item);
75                 }
76
77                 public void CopyTo (JsonValue [] array, int arrayIndex)
78                 {
79                         list.CopyTo (array, arrayIndex);
80                 }
81
82                 public int IndexOf (JsonValue item)
83                 {
84                         return list.IndexOf (item);
85                 }
86
87                 public void Insert (int index, JsonValue item)
88                 {
89                         list.Insert (index, item);
90                 }
91
92                 public bool Remove (JsonValue item)
93                 {
94                         return list.Remove (item);
95                 }
96
97                 public void RemoveAt (int index)
98                 {
99                         list.RemoveAt (index);
100                 }
101
102                 public override void Save (Stream stream)
103                 {
104                         if (stream == null)
105                                 throw new ArgumentNullException ("stream");
106                         stream.WriteByte ((byte) '[');
107                         for (int i = 0; i < list.Count; i++) {
108                                 JsonValue v = list [i];
109                                 if (v != null)
110                                         v.Save (stream);
111                                 else {
112                                         stream.WriteByte ((byte) 'n');
113                                         stream.WriteByte ((byte) 'u');
114                                         stream.WriteByte ((byte) 'l');
115                                         stream.WriteByte ((byte) 'l');
116                                 }
117
118                                 if (i < Count - 1) {
119                                         stream.WriteByte ((byte) ',');
120                                         stream.WriteByte ((byte) ' ');
121                                 }
122                         }
123                         stream.WriteByte ((byte) ']');
124                 }
125
126                 IEnumerator<JsonValue> IEnumerable<JsonValue>.GetEnumerator ()
127                 {
128                         return list.GetEnumerator ();
129                 }
130
131                 IEnumerator IEnumerable.GetEnumerator ()
132                 {
133                         return list.GetEnumerator ();
134                 }
135         }
136 }