[hygiene] simplify the code dealing with internal collections
[mono.git] / mcs / class / System.Json / System.Json / JsonObject.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.IO;
6 using System.Text;
7
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>>;
10
11 namespace System.Json
12 {
13         public class JsonObject : JsonValue, IDictionary<string, JsonValue>, ICollection<JsonPair>
14         {
15                 Dictionary<string, JsonValue> map;
16
17                 public JsonObject (params JsonPair [] items)
18                 {
19                         map = new Dictionary<string, JsonValue> ();
20
21                         if (items != null)
22                                 AddRange (items);
23                 }
24
25                 public JsonObject (JsonPairEnumerable items)
26                 {
27                         if (items == null)
28                                 throw new ArgumentNullException ("items");
29
30                         map = new Dictionary<string, JsonValue> ();
31                         AddRange (items);
32                 }
33
34                 public override int Count {
35                         get { return map.Count; }
36                 }
37
38                 public IEnumerator<JsonPair> GetEnumerator ()
39                 {
40                         return map.GetEnumerator ();
41                 }
42
43                 IEnumerator IEnumerable.GetEnumerator ()
44                 {
45                         return map.GetEnumerator ();
46                 }
47
48                 public override sealed JsonValue this [string key] {
49                         get { return map [key]; }
50                         set { map [key] = value; }
51                 }
52
53                 public override JsonType JsonType {
54                         get { return JsonType.Object; }
55                 }
56
57                 public ICollection<string> Keys {
58                         get { return map.Keys; }
59                 }
60
61                 public ICollection<JsonValue> Values {
62                         get { return map.Values; }
63                 }
64
65                 public void Add (string key, JsonValue value)
66                 {
67                         if (key == null)
68                                 throw new ArgumentNullException ("key");
69                         if (value == null)
70                                 throw new ArgumentNullException ("value");
71
72                         map.Add (key, value);
73                 }
74
75                 public void Add (JsonPair pair)
76                 {
77                         Add (pair.Key, pair.Value);
78                 }
79
80                 public void AddRange (JsonPairEnumerable items)
81                 {
82                         if (items == null)
83                                 throw new ArgumentNullException ("items");
84
85                         foreach (var pair in items)
86                                 map.Add (pair.Key, pair.Value);
87                 }
88
89                 public void AddRange (params JsonPair [] items)
90                 {
91                         AddRange ((JsonPairEnumerable) items);
92                 }
93
94                 public void Clear ()
95                 {
96                         map.Clear ();
97                 }
98
99                 bool ICollection<JsonPair>.Contains (JsonPair item)
100                 {
101                         return (map as ICollection<JsonPair>).Contains (item);
102                 }
103
104                 bool ICollection<JsonPair>.Remove (JsonPair item)
105                 {
106                         return (map as ICollection<JsonPair>).Remove (item);
107                 }
108
109                 public override bool ContainsKey (string key)
110                 {
111                         if (key == null)
112                                 throw new ArgumentNullException ("key");
113
114                         return map.ContainsKey (key);
115                 }
116
117                 public void CopyTo (JsonPair [] array, int arrayIndex)
118                 {
119                         (map as ICollection<JsonPair>).CopyTo (array, arrayIndex);
120                 }
121
122                 public bool Remove (string key)
123                 {
124                         if (key == null)
125                                 throw new ArgumentNullException ("key");
126
127                         return map.Remove (key);
128                 }
129
130                 bool ICollection<JsonPair>.IsReadOnly {
131                         get { return false; }
132                 }
133
134                 public override void Save (Stream stream)
135                 {
136                         if (stream == null)
137                                 throw new ArgumentNullException ("stream");
138                         stream.WriteByte ((byte) '{');
139                         foreach (JsonPair pair in map) {
140                                 stream.WriteByte ((byte) '"');
141                                 byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (pair.Key));
142                                 stream.Write (bytes, 0, bytes.Length);
143                                 stream.WriteByte ((byte) '"');
144                                 stream.WriteByte ((byte) ',');
145                                 stream.WriteByte ((byte) ' ');
146                                 pair.Value.Save (stream);
147                         }
148                         stream.WriteByte ((byte) '}');
149                 }
150
151                 public bool TryGetValue (string key, out JsonValue value)
152                 {
153                         return map.TryGetValue (key, out value);
154                 }
155         }
156 }