Add check for null JsonArray elements.
authorScott Blomquist <github@scott.blomqui.st>
Thu, 22 Mar 2012 01:16:10 +0000 (18:16 -0700)
committerScott Blomquist <github@scott.blomqui.st>
Thu, 22 Mar 2012 01:20:16 +0000 (18:20 -0700)
mcs/class/System.Json/System.Json/JsonValue.cs
mcs/class/System.Json/Test/System.Json/JsonValueTest.cs

index 4c6dc5eca7eb9bef29b186d0fb17ea5012777e8b..1d16b88e3f40407f1994efc04735c38bc269fada 100644 (file)
@@ -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 (']');
index 754f8556a5fea0dbef54f21fdc055342afd69f8e..2459c1fc3ad0f27478d902ffaccc6f2ec3f0d371 100644 (file)
@@ -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]");
+               }
        }
 }