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>>;
13 public class JsonObject : JsonValue, IDictionary<string, JsonValue>, ICollection<JsonPair>
15 Dictionary<string, JsonValue> map;
17 public JsonObject (params JsonPair [] items)
19 map = new Dictionary<string, JsonValue> ();
25 public JsonObject (JsonPairEnumerable items)
28 throw new ArgumentNullException ("items");
30 map = new Dictionary<string, JsonValue> ();
34 public override int Count {
35 get { return map.Count; }
38 public IEnumerator<JsonPair> GetEnumerator ()
40 return map.GetEnumerator ();
43 IEnumerator IEnumerable.GetEnumerator ()
45 return map.GetEnumerator ();
48 public override sealed JsonValue this [string key] {
49 get { return map [key]; }
50 set { map [key] = value; }
53 public override JsonType JsonType {
54 get { return JsonType.Object; }
57 public ICollection<string> Keys {
58 get { return map.Keys; }
61 public ICollection<JsonValue> Values {
62 get { return map.Values; }
65 public void Add (string key, JsonValue value)
68 throw new ArgumentNullException ("key");
73 public void Add (JsonPair pair)
75 Add (pair.Key, pair.Value);
78 public void AddRange (JsonPairEnumerable items)
81 throw new ArgumentNullException ("items");
83 foreach (var pair in items)
84 map.Add (pair.Key, pair.Value);
87 public void AddRange (params JsonPair [] items)
89 AddRange ((JsonPairEnumerable) items);
97 bool ICollection<JsonPair>.Contains (JsonPair item)
99 return (map as ICollection<JsonPair>).Contains (item);
102 bool ICollection<JsonPair>.Remove (JsonPair item)
104 return (map as ICollection<JsonPair>).Remove (item);
107 public override bool ContainsKey (string key)
110 throw new ArgumentNullException ("key");
112 return map.ContainsKey (key);
115 public void CopyTo (JsonPair [] array, int arrayIndex)
117 (map as ICollection<JsonPair>).CopyTo (array, arrayIndex);
120 public bool Remove (string key)
123 throw new ArgumentNullException ("key");
125 return map.Remove (key);
128 bool ICollection<JsonPair>.IsReadOnly {
129 get { return false; }
132 public override void Save (Stream stream)
135 throw new ArgumentNullException ("stream");
136 stream.WriteByte ((byte) '{');
137 foreach (JsonPair pair in map) {
138 stream.WriteByte ((byte) '"');
139 byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (pair.Key));
140 stream.Write (bytes, 0, bytes.Length);
141 stream.WriteByte ((byte) '"');
142 stream.WriteByte ((byte) ',');
143 stream.WriteByte ((byte) ' ');
144 if (pair.Value == null) {
145 stream.WriteByte ((byte) 'n');
146 stream.WriteByte ((byte) 'u');
147 stream.WriteByte ((byte) 'l');
148 stream.WriteByte ((byte) 'l');
150 pair.Value.Save (stream);
152 stream.WriteByte ((byte) '}');
155 public bool TryGetValue (string key, out JsonValue value)
157 return map.TryGetValue (key, out value);