Merge pull request #475 from pruiz/xamarin-bug-7408
[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
70                         map.Add (key, value);
71                 }
72
73                 public void Add (JsonPair pair)
74                 {
75                         Add (pair.Key, pair.Value);
76                 }
77
78                 public void AddRange (JsonPairEnumerable items)
79                 {
80                         if (items == null)
81                                 throw new ArgumentNullException ("items");
82
83                         foreach (var pair in items)
84                                 map.Add (pair.Key, pair.Value);
85                 }
86
87                 public void AddRange (params JsonPair [] items)
88                 {
89                         AddRange ((JsonPairEnumerable) items);
90                 }
91
92                 public void Clear ()
93                 {
94                         map.Clear ();
95                 }
96
97                 bool ICollection<JsonPair>.Contains (JsonPair item)
98                 {
99                         return (map as ICollection<JsonPair>).Contains (item);
100                 }
101
102                 bool ICollection<JsonPair>.Remove (JsonPair item)
103                 {
104                         return (map as ICollection<JsonPair>).Remove (item);
105                 }
106
107                 public override bool ContainsKey (string key)
108                 {
109                         if (key == null)
110                                 throw new ArgumentNullException ("key");
111
112                         return map.ContainsKey (key);
113                 }
114
115                 public void CopyTo (JsonPair [] array, int arrayIndex)
116                 {
117                         (map as ICollection<JsonPair>).CopyTo (array, arrayIndex);
118                 }
119
120                 public bool Remove (string key)
121                 {
122                         if (key == null)
123                                 throw new ArgumentNullException ("key");
124
125                         return map.Remove (key);
126                 }
127
128                 bool ICollection<JsonPair>.IsReadOnly {
129                         get { return false; }
130                 }
131
132                 public override void Save (Stream stream)
133                 {
134                         if (stream == null)
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');
149                                 } else
150                                         pair.Value.Save (stream);
151                         }
152                         stream.WriteByte ((byte) '}');
153                 }
154
155                 public bool TryGetValue (string key, out JsonValue value)
156                 {
157                         return map.TryGetValue (key, out value);
158                 }
159         }
160 }