2 using System.Collections;
3 using System.Collections.Generic;
5 using System.Globalization;
9 using JsonPair = System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>;
10 using JsonPairEnumerable = System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>>;
15 public class JsonObject : JsonValue, IDictionary<string, JsonValue>, ICollection<KeyValuePair<string, JsonValue>>
18 Dictionary<string, JsonValue> map;
19 JsonPairEnumerable source;
21 public JsonObject (params KeyValuePair<string, JsonValue> [] items)
22 : this ((JsonPairEnumerable) items)
26 public JsonObject (JsonPairEnumerable items)
29 throw new ArgumentNullException ("items");
33 public override int Count {
35 if (known_count < 0) {
37 known_count = map.Count;
40 foreach (JsonPair p in source)
48 public IEnumerator<JsonPair> GetEnumerator ()
50 return AsEnumerable ().GetEnumerator ();
53 JsonPairEnumerable AsEnumerable ()
58 public bool IsReadOnly {
62 bool ICollection<JsonPair>.IsReadOnly {
63 get { return ((ICollection<JsonPair>) map).IsReadOnly; }
66 public override sealed JsonValue this [string key] {
68 foreach (JsonPair pair in AsEnumerable ())
71 throw new KeyNotFoundException (String.Format ("key '{0}' was not found.", key));
79 public override JsonType JsonType {
80 get { return JsonType.Object; }
83 public ICollection<string> Keys {
90 public ICollection<JsonValue> Values {
97 public void Add (string key, JsonValue value)
100 throw new ArgumentNullException ("key");
102 throw new ArgumentNullException ("value");
104 map.Add (key, value);
107 public void Add (JsonPair pair)
109 Add (pair.Key, pair.Value);
112 public void AddRange (JsonPairEnumerable items)
115 throw new ArgumentNullException ("items");
116 source = source.Concat (items);
120 public void AddRange (JsonPair [] items)
122 AddRange ((JsonPairEnumerable) items);
125 static readonly JsonPair [] empty = new JsonPair [0];
135 bool ICollection<JsonPair>.Contains (JsonPair item)
137 return ContainsKey (item.Key);
140 public override bool ContainsKey (string key)
143 throw new ArgumentNullException ("key");
144 foreach (JsonPair p in AsEnumerable ())
150 public void CopyTo (JsonPair [] array, int arrayIndex)
152 foreach (JsonPair p in AsEnumerable ())
153 array [arrayIndex++] = p;
156 public bool Remove (string key)
159 return map.Remove (key);
162 bool ICollection<JsonPair>.Remove (JsonPair item)
164 return ((ICollection<JsonPair>) map).Remove (item);
167 public override void Save (Stream stream)
170 throw new ArgumentNullException ("stream");
171 stream.WriteByte ((byte) '{');
172 foreach (JsonPair pair in this) {
173 stream.WriteByte ((byte) '"');
174 byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (pair.Key));
175 stream.Write (bytes, 0, bytes.Length);
176 stream.WriteByte ((byte) '"');
177 stream.WriteByte ((byte) ',');
178 stream.WriteByte ((byte) ' ');
179 pair.Value.Save (stream);
181 stream.WriteByte ((byte) '}');
184 public bool TryGetValue (string key, out JsonValue value)
186 foreach (JsonPair p in AsEnumerable ())
198 map = new Dictionary<string, JsonValue> ();
199 foreach (JsonPair pair in source)
200 map.Add (pair.Key, pair.Value);