null JSON type must be handled in JsonWriter.
authorAtsushi Eno <atsushi@ximian.com>
Sat, 26 Mar 2011 12:27:19 +0000 (21:27 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Sat, 26 Mar 2011 12:27:19 +0000 (21:27 +0900)
mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonWriter.cs
mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonWriterTest.cs

index d0570cb31c3341f3ddcff4e324987e9e9002b2ae..960db6c0e16bc47a33a61d513f440e5cfbed0125 100644 (file)
@@ -39,6 +39,7 @@ namespace System.Runtime.Serialization.Json
                enum ElementType
                {
                        None,
+                       Null,
                        Object,
                        Array,
                        String,
@@ -194,6 +195,8 @@ namespace System.Runtime.Serialization.Json
                                        break;
                                case ElementType.String:
                                        throw new XmlException ("Mixed content is not allowed in this XmlDictionaryWriter");
+                               case ElementType.Null:
+                                       throw new XmlException ("Current type is null and writing element inside null is not allowed");
                                case ElementType.None:
                                        throw new XmlException ("Before writing a child element, an element needs 'type' attribute to indicate whether the element is a JSON array or a JSON object in this XmlDictionaryWriter");
                                }
@@ -310,6 +313,11 @@ namespace System.Runtime.Serialization.Json
                                        element_kinds.Pop ();
                                        element_kinds.Push (ElementType.String);
                                        break;
+                               case "null":
+                                       element_kinds.Pop ();
+                                       element_kinds.Push (ElementType.Null);
+                                       OutputString ("null");
+                                       break;
                                default:
                                        throw new XmlException (String.Format ("Unexpected type attribute value '{0}'", attr_value));
                                }
@@ -359,7 +367,8 @@ namespace System.Runtime.Serialization.Json
                        else if (text == null) {
                                no_string_yet = false;
                                is_null = true;
-                               OutputString ("null");
+                               if (element_kinds.Peek () != ElementType.Null)
+                                       OutputString ("null");
                        } else {
                                switch (element_kinds.Peek ()) {
                                case ElementType.String:
index 9101f5f9bb22ac20a9f49fa308d6d06b6e626aee..4ea004a34e86c7b60d01da5951b7069d8ff636d5 100644 (file)
@@ -591,5 +591,16 @@ namespace MonoTests.System.Runtime.Serialization.Json
                        w.Close ();
                        Assert.AreEqual ("\"\\/my date\\/\"", ResultString);
                }
+
+               [Test]
+               public void WriteNullType ()
+               {
+                       w.WriteStartElement ("root");
+                       w.WriteAttributeString ("type", "object");
+                       w.WriteStartElement ("foo");
+                       w.WriteAttributeString ("type", "null");
+                       w.Close ();
+                       Assert.AreEqual ("{\"foo\":null}", ResultString);
+               }
        }
 }