diff options
author | Steffen Kieß <Steffen.Kiess@ipvs.uni-stuttgart.de> | 2014-07-11 18:04:42 +0400 |
---|---|---|
committer | Steffen Kieß <Steffen.Kiess@ipvs.uni-stuttgart.de> | 2014-07-11 18:13:41 +0400 |
commit | d65e93b74fd5623881e561dd0be240003a7d6c54 (patch) | |
tree | d22098b914851f5ae67b1485edfa853cb792c17a /mcs/class/System.Json | |
parent | 1a021c1e79f2b3413813f11c7ca88068cf72eecb (diff) |
Use SortedDictionary in System.Json.JsonObject
Currently JsonObject uses a Dictionary and JsonValue does not sort the keys
in SaveInternal(). As iterating over a Dictionary returns the values in
random order, this causes the JSON string to be non-deterministic.
Switching to SortedDictionary makes sure that the same object always will
produce the same JSON string.
The keys are sorted using StringComparer.Ordinal to produce the same order
in all locales.
Diffstat (limited to 'mcs/class/System.Json')
-rw-r--r-- | mcs/class/System.Json/System.Json/JsonObject.cs | 7 | ||||
-rw-r--r-- | mcs/class/System.Json/Test/System.Json/JsonValueTest.cs | 20 |
2 files changed, 24 insertions, 3 deletions
diff --git a/mcs/class/System.Json/System.Json/JsonObject.cs b/mcs/class/System.Json/System.Json/JsonObject.cs index 33177b1c5f2..91366a2243a 100644 --- a/mcs/class/System.Json/System.Json/JsonObject.cs +++ b/mcs/class/System.Json/System.Json/JsonObject.cs @@ -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); } diff --git a/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs b/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs index bc04e2d403f..15d5bd82b38 100644 --- a/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs +++ b/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs @@ -46,6 +46,26 @@ namespace MonoTests.System Assert.AreEqual (str, "[1, 2, 3, null]"); } + // Test that we correctly serialize JsonObject with null elements. + [Test] + public void ToStringOnJsonObjectWithNulls () { + var j = JsonValue.Load (new StringReader ("{\"a\":null,\"b\":2}")); + Assert.AreEqual (2, j.Count, "itemcount"); + Assert.AreEqual (JsonType.Object, j.JsonType, "type"); + var str = j.ToString (); + Assert.AreEqual (str, "{\"a\": null, \"b\": 2}"); + } + + [Test] + public void JsonObjectOrder () { + var obj = new JsonObject (); + obj["a"] = 1; + obj["c"] = 3; + obj["b"] = 2; + var str = obj.ToString (); + Assert.AreEqual (str, "{\"a\": 1, \"b\": 2, \"c\": 3}"); + } + [Test] public void QuoteEscapeBug_20869 () { |