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 // Use SortedDictionary to make result of ToString() deterministic
16 SortedDictionary<string, JsonValue> map;
18 public JsonObject (params JsonPair [] items)
20 map = new SortedDictionary<string, JsonValue> (StringComparer.Ordinal);
26 public JsonObject (JsonPairEnumerable items)
29 throw new ArgumentNullException ("items");
31 map = new SortedDictionary<string, JsonValue> (StringComparer.Ordinal);
35 public override int Count {
36 get { return map.Count; }
39 public IEnumerator<JsonPair> GetEnumerator ()
41 return map.GetEnumerator ();
44 IEnumerator IEnumerable.GetEnumerator ()
46 return map.GetEnumerator ();
49 public override sealed JsonValue this [string key] {
50 get { return map [key]; }
51 set { map [key] = value; }
54 public override JsonType JsonType {
55 get { return JsonType.Object; }
58 public ICollection<string> Keys {
59 get { return map.Keys; }
62 public ICollection<JsonValue> Values {
63 get { return map.Values; }
66 public void Add (string key, JsonValue value)
69 throw new ArgumentNullException ("key");
74 public void Add (JsonPair pair)
76 Add (pair.Key, pair.Value);
79 public void AddRange (JsonPairEnumerable items)
82 throw new ArgumentNullException ("items");
84 foreach (var pair in items)
85 map.Add (pair.Key, pair.Value);
88 public void AddRange (params JsonPair [] items)
90 AddRange ((JsonPairEnumerable) items);
98 bool ICollection<JsonPair>.Contains (JsonPair item)
100 return (map as ICollection<JsonPair>).Contains (item);
103 bool ICollection<JsonPair>.Remove (JsonPair item)
105 return (map as ICollection<JsonPair>).Remove (item);
108 public override bool ContainsKey (string key)
111 throw new ArgumentNullException ("key");
113 return map.ContainsKey (key);
116 public void CopyTo (JsonPair [] array, int arrayIndex)
118 (map as ICollection<JsonPair>).CopyTo (array, arrayIndex);
121 public bool Remove (string key)
124 throw new ArgumentNullException ("key");
126 return map.Remove (key);
129 bool ICollection<JsonPair>.IsReadOnly {
130 get { return false; }
133 public override void Save (Stream stream)
136 throw new ArgumentNullException ("stream");
137 stream.WriteByte ((byte) '{');
138 foreach (JsonPair pair in map) {
139 stream.WriteByte ((byte) '"');
140 byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (pair.Key));
141 stream.Write (bytes, 0, bytes.Length);
142 stream.WriteByte ((byte) '"');
143 stream.WriteByte ((byte) ',');
144 stream.WriteByte ((byte) ' ');
145 if (pair.Value == null) {
146 stream.WriteByte ((byte) 'n');
147 stream.WriteByte ((byte) 'u');
148 stream.WriteByte ((byte) 'l');
149 stream.WriteByte ((byte) 'l');
151 pair.Value.Save (stream);
153 stream.WriteByte ((byte) '}');
156 public bool TryGetValue (string key, out JsonValue value)
158 return map.TryGetValue (key, out value);