Use SortedDictionary in System.Json.JsonObject
[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                 // Use SortedDictionary to make result of ToString() deterministic
16                 SortedDictionary<string, JsonValue> map;
17
18                 public JsonObject (params JsonPair [] items)
19                 {
20                         map = new SortedDictionary<string, JsonValue> (StringComparer.Ordinal);
21
22                         if (items != null)
23                                 AddRange (items);
24                 }
25
26                 public JsonObject (JsonPairEnumerable items)
27                 {
28                         if (items == null)
29                                 throw new ArgumentNullException ("items");
30
31                         map = new SortedDictionary<string, JsonValue> (StringComparer.Ordinal);
32                         AddRange (items);
33                 }
34
35                 public override int Count {
36                         get { return map.Count; }
37                 }
38
39                 public IEnumerator<JsonPair> GetEnumerator ()
40                 {
41                         return map.GetEnumerator ();
42                 }
43
44                 IEnumerator IEnumerable.GetEnumerator ()
45                 {
46                         return map.GetEnumerator ();
47                 }
48
49                 public override sealed JsonValue this [string key] {
50                         get { return map [key]; }
51                         set { map [key] = value; }
52                 }
53
54                 public override JsonType JsonType {
55                         get { return JsonType.Object; }
56                 }
57
58                 public ICollection<string> Keys {
59                         get { return map.Keys; }
60                 }
61
62                 public ICollection<JsonValue> Values {
63                         get { return map.Values; }
64                 }
65
66                 public void Add (string key, JsonValue value)
67                 {
68                         if (key == null)
69                                 throw new ArgumentNullException ("key");
70
71                         map.Add (key, value);
72                 }
73
74                 public void Add (JsonPair pair)
75                 {
76                         Add (pair.Key, pair.Value);
77                 }
78
79                 public void AddRange (JsonPairEnumerable items)
80                 {
81                         if (items == null)
82                                 throw new ArgumentNullException ("items");
83
84                         foreach (var pair in items)
85                                 map.Add (pair.Key, pair.Value);
86                 }
87
88                 public void AddRange (params JsonPair [] items)
89                 {
90                         AddRange ((JsonPairEnumerable) items);
91                 }
92
93                 public void Clear ()
94                 {
95                         map.Clear ();
96                 }
97
98                 bool ICollection<JsonPair>.Contains (JsonPair item)
99                 {
100                         return (map as ICollection<JsonPair>).Contains (item);
101                 }
102
103                 bool ICollection<JsonPair>.Remove (JsonPair item)
104                 {
105                         return (map as ICollection<JsonPair>).Remove (item);
106                 }
107
108                 public override bool ContainsKey (string key)
109                 {
110                         if (key == null)
111                                 throw new ArgumentNullException ("key");
112
113                         return map.ContainsKey (key);
114                 }
115
116                 public void CopyTo (JsonPair [] array, int arrayIndex)
117                 {
118                         (map as ICollection<JsonPair>).CopyTo (array, arrayIndex);
119                 }
120
121                 public bool Remove (string key)
122                 {
123                         if (key == null)
124                                 throw new ArgumentNullException ("key");
125
126                         return map.Remove (key);
127                 }
128
129                 bool ICollection<JsonPair>.IsReadOnly {
130                         get { return false; }
131                 }
132
133                 public override void Save (Stream stream)
134                 {
135                         if (stream == null)
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');
150                                 } else
151                                         pair.Value.Save (stream);
152                         }
153                         stream.WriteByte ((byte) '}');
154                 }
155
156                 public bool TryGetValue (string key, out JsonValue value)
157                 {
158                         return map.TryGetValue (key, out value);
159                 }
160         }
161 }