From a8628869f48ac89a0ceb50e763a485086576a4e8 Mon Sep 17 00:00:00 2001 From: Scott Blomquist Date: Wed, 21 Mar 2012 18:16:10 -0700 Subject: [PATCH] Add check for null JsonArray elements. --- mcs/class/System.Json/System.Json/JsonValue.cs | 5 ++++- .../System.Json/Test/System.Json/JsonValueTest.cs | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mcs/class/System.Json/System.Json/JsonValue.cs b/mcs/class/System.Json/System.Json/JsonValue.cs index 4c6dc5eca7e..1d16b88e3f4 100644 --- a/mcs/class/System.Json/System.Json/JsonValue.cs +++ b/mcs/class/System.Json/System.Json/JsonValue.cs @@ -162,7 +162,10 @@ namespace System.Json foreach (JsonValue v in ((JsonArray) this)) { if (following) w.Write (", "); - v.SaveInternal (w); + if (v != null) + v.SaveInternal (w); + else + w.Write ("null"); following = true; } w.Write (']'); diff --git a/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs b/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs index 754f8556a5f..2459c1fc3ad 100644 --- a/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs +++ b/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs @@ -25,5 +25,15 @@ namespace MonoTests.System Assert.AreEqual (JsonType.String, j ["a"].JsonType, "type"); Assert.AreEqual ("b", (string) j ["a"], "value"); } + + // Test that we correctly serialize JsonArray with null elements. + [Test] + public void ToStringOnJsonArrayWithNulls () { + var j = JsonValue.Load (new StringReader ("[1,2,3,null]")); + Assert.AreEqual (4, j.Count, "itemcount"); + Assert.AreEqual (JsonType.Array, j.JsonType, "type"); + var str = j.ToString (); + Assert.AreEqual (str, "[1, 2, 3, null]"); + } } } -- 2.25.1