Author: atsushi
Date: 2008-01-30 01:11:58 -0500 (Wed, 30 Jan 2008)
New Revision: 94360

Modified:
   
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
   
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
   
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
   
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
Log:
2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>

        * JsonReader.cs : Do not consume "__type" (which is the first content
          of an object) as an element content. It must be handled as an
          attribute (it needs more changes).

        * JsonReaderTest.cs : added tests for "__type" attributes (some of
          them are not working yet).



Modified: 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
       2008-01-30 04:22:45 UTC (rev 94359)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
       2008-01-30 06:11:58 UTC (rev 94360)
@@ -1,3 +1,9 @@
+2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * JsonReader.cs : Do not consume "__type" (which is the first content
+         of an object) as an element content. It must be handled as an
+         attribute (it needs more changes).
+
 2008-01-24  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * JsonSerializationReader.cs : when deserializing primitive strings,

Modified: 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
   2008-01-30 04:22:45 UTC (rev 94359)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
   2008-01-30 06:11:58 UTC (rev 94360)
@@ -35,6 +35,7 @@
 namespace System.Runtime.Serialization.Json
 {
        // FIXME: quotas check
+       // FIXME: parse object content and handle __type as attribute.
        class JsonReader : XmlDictionaryReader, IXmlJsonReaderInitializer, 
IXmlLineInfo
        {
                class ElementInfo
@@ -58,6 +59,7 @@
                XmlNodeType current_node, attr_state; // 
Attribute(type)/Text(value)/None
                string simple_value;
                string next_element;
+               string current_runtime_type, next_object_content_name;
                ReadState read_state = ReadState.Initial;
                bool content_stored;
                bool finished;
@@ -365,6 +367,15 @@
 
                        if (elements.Count > 0 && elements.Peek ().Type == 
"array")
                                next_element = "item";
+                       else if (next_object_content_name != null) {
+                               next_element = next_object_content_name;
+                               next_object_content_name = null;
+                               if (ch != ':')
+                                       throw XmlError ("':' is expected after 
a name of an object content");
+                               SkipWhitespaces ();
+                               ReadContent (true);
+                               return true;
+                       }
 
                        switch (ch) {
                        case '{':
@@ -390,8 +401,7 @@
                        case '"':
                                string s = ReadStringLiteral ();
                                if (!objectValue && elements.Count > 0 && 
elements.Peek ().Type == "object") {
-                                       string name = s;
-                                       next_element = name;
+                                       next_element = s;
                                        SkipWhitespaces ();
                                        Expect (':');
                                        SkipWhitespaces ();
@@ -439,7 +449,25 @@
 
                void ReadStartObject ()
                {
-                       elements.Push (new ElementInfo (next_element, 
"object"));
+                       ElementInfo ei = new ElementInfo (next_element, 
"object");
+                       elements.Push (ei);
+
+                       SkipWhitespaces ();
+                       if (PeekChar () == '"') { // it isn't premise: the 
object might be empty
+                               ReadChar ();
+                               string s = ReadStringLiteral ();
+                               if (s == "__type") {
+                                       SkipWhitespaces ();
+                                       Expect (':');
+                                       SkipWhitespaces ();
+                                       Expect ('"');
+                                       current_runtime_type = 
ReadStringLiteral ();
+                                       SkipWhitespaces ();
+                                       ei.HasContent = true;
+                               }
+                               else
+                                       next_object_content_name = s;
+                       }
                }
 
                void ReadStartArray ()
@@ -549,6 +577,13 @@
                        } while (true);
                }
 
+               int PeekChar ()
+               {
+                       if (saved_char < 0)
+                               saved_char = reader.Read ();
+                       return saved_char;
+               }
+
                int ReadChar ()
                {
                        int v = saved_char >= 0 ? saved_char : reader.Read ();
@@ -565,12 +600,12 @@
                void SkipWhitespaces ()
                {
                        do {
-                               switch (reader.Peek ()) {
+                               switch (PeekChar ()) {
                                case ' ':
                                case '\t':
                                case '\r':
                                case '\n':
-                                       reader.Read ();
+                                       ReadChar ();
                                        continue;
                                default:
                                        return;

Modified: 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
  2008-01-30 04:22:45 UTC (rev 94359)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
  2008-01-30 06:11:58 UTC (rev 94360)
@@ -1,3 +1,8 @@
+2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * JsonReaderTest.cs : added tests for "__type" attributes (some of
+         them are not working yet).
+
 2008-01-24  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * JsonReaderTest.cs : test "type" attribute in several nodes.

Modified: 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
  2008-01-30 04:22:45 UTC (rev 94359)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
  2008-01-30 06:11:58 UTC (rev 94360)
@@ -741,5 +741,51 @@
                {
                        ReadToEnd (CreateReader ("[123,456,]"));
                }
+
+               [Test]
+               public void ReadObjectRuntimeTypeAsAttribute ()
+               {
+                       XmlDictionaryReader r = CreateReader 
("{\"__type\":\"System.Int32\"}");
+                       r.Read ();
+                       AssertNode (0, "root", XmlNodeType.Element, 
String.Empty, "object", r, "#1");
+                       Assert.IsTrue (r.MoveToAttribute ("type"), "#2");
+                       AssertNode (0, "type", XmlNodeType.Attribute, "object", 
"object", r, "#3");
+                       Assert.IsTrue (r.MoveToAttribute ("__type"), "#4");
+                       AssertNode (0, "__type", XmlNodeType.Attribute, 
"System.Int32", "object", r, "#5");
+                       r.Read ();
+                       Assert.AreEqual (XmlNodeType.EndElement, r.NodeType, 
"#6");
+               }
+
+               [Test]
+               public void ReadObjectRuntimeType ()
+               {
+                       XmlDictionaryReader r = CreateReader 
("{\"__type\":\"System.Int32\", \"foo\":true}");
+                       r.Read ();
+                       AssertNode (0, "root", XmlNodeType.Element, 
String.Empty, "object", r, "#1");
+                       Assert.IsTrue (r.MoveToAttribute ("type"), "#2");
+                       AssertNode (0, "type", XmlNodeType.Attribute, "object", 
"object", r, "#3");
+                       Assert.IsTrue (r.MoveToAttribute ("__type"), "#4");
+                       AssertNode (0, "__type", XmlNodeType.Attribute, 
"System.Int32", "object", r, "#5");
+                       r.Read ();
+                       Assert.AreEqual (XmlNodeType.Element, r.NodeType, "#6");
+                       Assert.AreEqual ("foo", r.LocalName, "#7");
+               }
+
+               [Test]
+               [ExpectedException (typeof (XmlException))]
+               public void ReadInvalidObjectRuntimeTypeValue ()
+               {
+                       ReadToEnd (CreateReader ("{\"__type\":true}"));
+               }
+
+               [Test]
+               public void ReadObjectRuntimeTypeIncorrectPosition ()
+               {
+                       XmlReader r = CreateReader ("{\"foo\" : false, 
\"__type\" : \"System.Int32\"}");
+                       r.Read ();
+                       // When __type is not at the first content, it is not 
regarded as an attribute. Note that it is not treated as an error.
+                       Assert.IsFalse (r.MoveToAttribute ("__type"));
+                       r.Skip ();
+               }
        }
 }
\ No newline at end of file

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to