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 bool ICollection
<JsonPair
>.IsReadOnly
{
62 get { return ((ICollection<JsonPair>) map).IsReadOnly; }
65 public override sealed JsonValue
this [string key
] {
67 foreach (JsonPair pair
in AsEnumerable ())
70 throw new KeyNotFoundException (String
.Format ("key '{0}' was not found.", key
));
78 public override JsonType JsonType
{
79 get { return JsonType.Object; }
82 public ICollection
<string> Keys
{
89 public ICollection
<JsonValue
> Values
{
96 public void Add (string key
, JsonValue
value)
99 throw new ArgumentNullException ("key");
101 throw new ArgumentNullException ("value");
103 map
.Add (key
, value);
106 public void Add (JsonPair pair
)
108 Add (pair
.Key
, pair
.Value
);
111 public void AddRange (JsonPairEnumerable items
)
114 throw new ArgumentNullException ("items");
115 source
= new MergedEnumerable
<JsonPair
> (source
, items
);
119 public void AddRange (JsonPair
[] items
)
121 AddRange ((JsonPairEnumerable
) items
);
124 static readonly JsonPair
[] empty
= new JsonPair
[0];
134 bool ICollection
<JsonPair
>.Contains (JsonPair item
)
136 return ContainsKey (item
.Key
);
139 public override bool ContainsKey (string key
)
142 throw new ArgumentNullException ("key");
143 foreach (JsonPair p
in AsEnumerable ())
149 public void CopyTo (JsonPair
[] array
, int arrayIndex
)
151 foreach (JsonPair p
in AsEnumerable ())
152 array
[arrayIndex
++] = p
;
155 public bool Remove (string key
)
158 return map
.Remove (key
);
161 bool ICollection
<JsonPair
>.Remove (JsonPair item
)
163 return ((ICollection
<JsonPair
>) map
).Remove (item
);
166 public override void Save (Stream stream
)
169 throw new ArgumentNullException ("stream");
170 stream
.WriteByte ((byte) '{');
171 foreach (JsonPair pair
in this) {
172 stream
.WriteByte ((byte) '"');
173 byte [] bytes
= Encoding
.UTF8
.GetBytes (EscapeString (pair
.Key
));
174 stream
.Write (bytes
, 0, bytes
.Length
);
175 stream
.WriteByte ((byte) '"');
176 stream
.WriteByte ((byte) ',');
177 stream
.WriteByte ((byte) ' ');
178 pair
.Value
.Save (stream
);
180 stream
.WriteByte ((byte) '}');
183 public bool TryGetValue (string key
, out JsonValue
value)
185 foreach (JsonPair p
in AsEnumerable ())
197 map
= new Dictionary
<string, JsonValue
> ();
198 foreach (JsonPair pair
in source
)
199 map
.Add (pair
.Key
, pair
.Value
);