Normalize line endings.
[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                 int known_count = -1;
14                 List<JsonValue> list;
15                 IEnumerable<JsonValue> source;
16
17                 public JsonArray (params JsonValue [] items)
18                         : this ((IEnumerable<JsonValue>) items)
19                 {
20                 }
21
22                 public JsonArray (IEnumerable<JsonValue> items)
23                 {
24                         if (items == null)
25                                 throw new ArgumentNullException ("items");
26                         this.source = items;
27                 }
28
29                 public override int Count {
30                         get {
31                                 if (known_count < 0) {
32                                         if (list != null)
33                                                 known_count = list.Count;
34                                         else {
35                                                 known_count = 0;
36                                                 foreach (JsonValue v in source)
37                                                         known_count++;
38                                         }
39                                 }
40                                 return known_count;
41                         }
42                 }
43
44                 public bool IsReadOnly {
45                         get { return false; }
46                 }
47
48                 public override sealed JsonValue this [int index] {
49                         get {
50                                 if (list != null)
51                                         return list [index];
52                                 int i = -1;
53                                 foreach (JsonValue v in source)
54                                         if (++i == index)
55                                                 return v;
56                                 throw new ArgumentOutOfRangeException ("index");
57                         }
58                         set {
59                                 PopulateList ();
60                                 list [index] = value;
61                         }
62                 }
63
64                 public override JsonType JsonType {
65                         get { return JsonType.Array; }
66                 }
67
68                 public void Add (JsonValue item)
69                 {
70                         if (item == null)
71                                 throw new ArgumentNullException ("item");
72                         PopulateList ();
73                         list.Add (item);
74                 }
75
76                 public void AddRange (IEnumerable<JsonValue> items)
77                 {
78                         if (items == null)
79                                 throw new ArgumentNullException ("items");
80                         if (list != null)
81                                 list.AddRange (items);
82                         else
83                                 source = new MergedEnumerable<JsonValue> (source, items);
84                 }
85
86                 public void AddRange (JsonValue [] items)
87                 {
88                         AddRange ((IEnumerable<JsonValue>) items);
89                 }
90
91                 static readonly JsonValue [] empty = new JsonValue [0];
92
93                 public void Clear ()
94                 {
95                         if (list != null)
96                                 list.Clear ();
97                         else
98                                 source = empty;
99                 }
100
101                 public bool Contains (JsonValue item)
102                 {
103                         if (item == null)
104                                 throw new ArgumentNullException ("item");
105                         foreach (JsonValue v in this)
106                                 if (item == v)
107                                         return true;
108                         return false;
109                 }
110
111                 public void CopyTo (JsonValue [] array, int arrayIndex)
112                 {
113                         if (list != null)
114                                 list.CopyTo (array, arrayIndex);
115                         else
116                                 foreach (JsonValue v in source)
117                                         array [arrayIndex++] = v;
118                 }
119
120                 public int IndexOf (JsonValue item)
121                 {
122                         if (item == null)
123                                 throw new ArgumentNullException ("item");
124                         int idx = 0;
125                         foreach (JsonValue v in this) {
126                                 if (item == v)
127                                         return idx;
128                                 idx++;
129                         }
130                         return -1;
131                 }
132
133                 public void Insert (int index, JsonValue item)
134                 {
135                         PopulateList ();
136                         list.Insert (index, item);
137                 }
138
139                 public bool Remove (JsonValue item)
140                 {
141                         PopulateList ();
142                         return list.Remove (item);
143                 }
144
145                 public void RemoveAt (int index)
146                 {
147                         PopulateList ();
148                         list.RemoveAt (index);
149                 }
150
151                 public override void Save (Stream stream)
152                 {
153                         if (stream == null)
154                                 throw new ArgumentNullException ("stream");
155                         stream.WriteByte ((byte) '[');
156                         for (int i = 0; i < Count; i++) {
157                                 this [i].Save (stream);
158                                 if (i < Count - 1) {
159                                         stream.WriteByte ((byte) ',');
160                                         stream.WriteByte ((byte) ' ');
161                                 }
162                         }
163                         stream.WriteByte ((byte) ']');
164                 }
165
166                 void PopulateList ()
167                 {
168                         if (list == null) {
169                                 list = new List<JsonValue> (source);
170                                 source = list;
171                         }
172                 }
173
174                 IEnumerator<JsonValue> IEnumerable<JsonValue>.GetEnumerator ()
175                 {
176                         return source.GetEnumerator ();
177                 }
178
179                 IEnumerator IEnumerable.GetEnumerator ()
180                 {
181                         return source.GetEnumerator ();
182                 }
183         }
184 }