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 IEnumerator IEnumerable
.GetEnumerator ()
54 return AsEnumerable ().GetEnumerator ();
57 JsonPairEnumerable
AsEnumerable ()
62 public bool IsReadOnly
{
66 bool ICollection
<JsonPair
>.IsReadOnly
{
67 get { return ((ICollection<JsonPair>) map).IsReadOnly; }
70 public override sealed JsonValue
this [string key
] {
72 foreach (JsonPair pair
in AsEnumerable ())
75 throw new KeyNotFoundException (String
.Format ("key '{0}' was not found.", key
));
83 public override JsonType JsonType
{
84 get { return JsonType.Object; }
87 public ICollection
<string> Keys
{
94 public ICollection
<JsonValue
> Values
{
101 public void Add (string key
, JsonValue
value)
104 throw new ArgumentNullException ("key");
106 throw new ArgumentNullException ("value");
111 public void Add (JsonPair pair
)
113 Add (pair
.Key
, pair
.Value
);
116 public void AddRange (JsonPairEnumerable items
)
119 throw new ArgumentNullException ("items");
120 source
= new MergedEnumerable
<JsonPair
> (source
, items
);
124 public void AddRange (JsonPair
[] items
)
126 AddRange ((JsonPairEnumerable
) items
);
129 static readonly JsonPair
[] empty
= new JsonPair
[0];
139 bool ICollection
<JsonPair
>.Contains (JsonPair item
)
141 return ContainsKey (item
.Key
);
144 public override bool ContainsKey (string key
)
147 throw new ArgumentNullException ("key");
148 foreach (JsonPair p
in AsEnumerable ())
154 public void CopyTo (JsonPair
[] array
, int arrayIndex
)
156 foreach (JsonPair p
in AsEnumerable ())
157 array
[arrayIndex
++] = p
;
160 public bool Remove (string key
)
163 return map
.Remove (key
);
166 bool ICollection
<JsonPair
>.Remove (JsonPair item
)
168 return ((ICollection
<JsonPair
>) map
).Remove (item
);
171 public override void Save (Stream stream
)
174 throw new ArgumentNullException ("stream");
175 stream
.WriteByte ((byte) '{');
176 foreach (JsonPair pair
in this) {
177 stream
.WriteByte ((byte) '"');
178 byte [] bytes
= Encoding
.UTF8
.GetBytes (EscapeString (pair
.Key
));
179 stream
.Write (bytes
, 0, bytes
.Length
);
180 stream
.WriteByte ((byte) '"');
181 stream
.WriteByte ((byte) ',');
182 stream
.WriteByte ((byte) ' ');
183 pair
.Value
.Save (stream
);
185 stream
.WriteByte ((byte) '}');
188 public bool TryGetValue (string key
, out JsonValue
value)
190 foreach (JsonPair p
in AsEnumerable ())
202 map
= new Dictionary
<string, JsonValue
> ();
203 foreach (JsonPair pair
in source
)
204 map
.Add (pair
.Key
, pair
.Value
);