2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Globalization;
8 using JsonPair = System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>;
9 using JsonPairEnumerable = System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>>;
14 public class JsonObject : JsonValue//, IDictionary<string, JsonValue>, ICollection<KeyValuePair<string, JsonValue>>
17 Dictionary<string, JsonValue> map;
18 JsonPairEnumerable source;
20 public JsonObject (params KeyValuePair<string, JsonValue> [] items)
21 : this ((JsonPairEnumerable) items)
25 public JsonObject (JsonPairEnumerable items)
28 throw new ArgumentNullException ("items");
32 public override int Count {
34 if (known_count < 0) {
36 known_count = map.Count;
39 foreach (JsonPair p in source)
47 public IEnumerator<JsonPair> GetEnumerator ()
49 return AsEnumerable ().GetEnumerator ();
52 JsonPairEnumerable AsEnumerable ()
57 public bool IsReadOnly {
61 public override sealed JsonValue this [string key] {
63 foreach (JsonPair pair in AsEnumerable ())
66 throw new KeyNotFoundException (String.Format ("key '{0}' was not found.", key));
74 public override JsonType JsonType {
75 get { return JsonType.Object; }
78 public IEnumerable<string> Keys {
80 foreach (JsonPair pair in AsEnumerable ())
81 yield return pair.Key;
85 public IEnumerable<JsonValue> Values {
87 foreach (JsonPair pair in AsEnumerable ())
88 yield return pair.Value;
92 public void Add (string key, JsonValue value)
95 throw new ArgumentNullException ("key");
97 throw new ArgumentNullException ("value");
102 public void Add (JsonPair pair)
104 Add (pair.Key, pair.Value);
107 public void AddRange (JsonPairEnumerable items)
110 throw new ArgumentNullException ("items");
111 source = new MergedEnumerable<JsonPair> (source, items);
115 public void AddRange (JsonPair [] items)
117 AddRange ((JsonPairEnumerable) items);
120 static readonly JsonPair [] empty = new JsonPair [0];
130 public override bool ContainsKey (string key)
133 throw new ArgumentNullException ("key");
134 foreach (string k in Keys)
140 public void CopyTo (JsonPair [] array, int arrayIndex)
142 foreach (JsonPair p in AsEnumerable ())
143 array [arrayIndex++] = p;
146 public void Remove (string key)
152 public override void Save (Stream stream)
155 throw new ArgumentNullException ("stream");
156 stream.WriteByte ((byte) '{');
157 foreach (JsonPair pair in this) {
158 stream.WriteByte ((byte) '"');
159 byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (pair.Key));
160 stream.Write (bytes, 0, bytes.Length);
161 stream.WriteByte ((byte) '"');
162 stream.WriteByte ((byte) ',');
163 stream.WriteByte ((byte) ' ');
164 pair.Value.Save (stream);
166 stream.WriteByte ((byte) '}');
172 map = new Dictionary<string, JsonValue> ();
173 foreach (JsonPair pair in source)
174 map.Add (pair.Key, pair.Value);