Merge pull request #2424 from alexrp/task-unobserved-exceptions
[mono.git] / mcs / class / System.Json / System.Json / JsonObject.cs
index 6b6373d84d58d21d54c25cd5e229033f4d52ef07..91366a2243a766eac337d7bc7739dfa3df5035be 100644 (file)
@@ -12,11 +12,12 @@ namespace System.Json
 {
        public class JsonObject : JsonValue, IDictionary<string, JsonValue>, ICollection<JsonPair>
        {
-               Dictionary<string, JsonValue> map;
+               // Use SortedDictionary to make result of ToString() deterministic
+               SortedDictionary<string, JsonValue> map;
 
                public JsonObject (params JsonPair [] items)
                {
-                       map = new Dictionary<string, JsonValue> ();
+                       map = new SortedDictionary<string, JsonValue> (StringComparer.Ordinal);
 
                        if (items != null)
                                AddRange (items);
@@ -27,7 +28,7 @@ namespace System.Json
                        if (items == null)
                                throw new ArgumentNullException ("items");
 
-                       map = new Dictionary<string, JsonValue> ();
+                       map = new SortedDictionary<string, JsonValue> (StringComparer.Ordinal);
                        AddRange (items);
                }
 
@@ -66,8 +67,6 @@ namespace System.Json
                {
                        if (key == null)
                                throw new ArgumentNullException ("key");
-                       if (value == null)
-                               throw new ArgumentNullException ("value");
 
                        map.Add (key, value);
                }
@@ -143,7 +142,13 @@ namespace System.Json
                                stream.WriteByte ((byte) '"');
                                stream.WriteByte ((byte) ',');
                                stream.WriteByte ((byte) ' ');
-                               pair.Value.Save (stream);
+                               if (pair.Value == null) {
+                                       stream.WriteByte ((byte) 'n');
+                                       stream.WriteByte ((byte) 'u');
+                                       stream.WriteByte ((byte) 'l');
+                                       stream.WriteByte ((byte) 'l');
+                               } else
+                                       pair.Value.Save (stream);
                        }
                        stream.WriteByte ((byte) '}');
                }