Author: atsushi
Date: 2008-01-30 02:20:11 -0500 (Wed, 30 Jan 2008)
New Revision: 94363

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

        * DataContractJsonSerializer.cs : fixed .ctor(type, knonwTypes) that
          missed to delegate knownTypes correctly.
        * JsonSerializationReader.cs : consider KnownTypes correctly.
          Take "__type" fully into consideration, not just for arrays.

        * DataContractJsonSerializerTest.cs : added deserialization tests
          for typed object with "__type".



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 07:17:03 UTC (rev 94362)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
       2008-01-30 07:20:11 UTC (rev 94363)
@@ -1,5 +1,12 @@
 2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
+       * DataContractJsonSerializer.cs : fixed .ctor(type, knonwTypes) that
+         missed to delegate knownTypes correctly.
+       * JsonSerializationReader.cs : consider KnownTypes correctly.
+         Take "__type" fully into consideration, not just for arrays.
+
+2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
        * JsonReader.cs : GetAttribute() was not still missing support for
          __type.
 

Modified: 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs
   2008-01-30 07:17:03 UTC (rev 94362)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs
   2008-01-30 07:20:11 UTC (rev 94363)
@@ -49,7 +49,7 @@
                }
 
                public DataContractJsonSerializer (Type type, IEnumerable<Type> 
knownTypes)
-                       : this (type, default_root_name, Type.EmptyTypes)
+                       : this (type, default_root_name, knownTypes)
                {
                }
 

Modified: 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
      2008-01-30 07:17:03 UTC (rev 94362)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
      2008-01-30 07:20:11 UTC (rev 94363)
@@ -130,9 +130,28 @@
                        }
                }
 
+               Type GetRuntimeType (string name)
+               {
+                       name = ToRuntimeTypeName (name);
+                       if (serializer.KnownTypes != null)
+                               foreach (Type t in serializer.KnownTypes)
+                                       if (t.FullName == name)
+                                               return t;
+                       return Type.GetType (name, false);
+               }
+
                object ReadInstanceDrivenObject ()
                {
                        string type = reader.GetAttribute ("type");
+                       if (type == "object") {
+                               string runtimeType = reader.GetAttribute 
("__type");
+                               if (runtimeType != null) {
+                                       Type t = GetRuntimeType (runtimeType);
+                                       if (t == null)
+                                               throw SerializationError 
(String.Format ("Cannot load type '{0}'", runtimeType));
+                                       return ReadObject (t);
+                               }
+                       }
                        string v = reader.ReadElementContentAsString ();
                        switch (type) {
                        case "boolean":
@@ -212,11 +231,6 @@
                                        if (!reader.IsStartElement ("item"))
                                                throw SerializationError 
(String.Format ("Expected element 'item', but found '{0}' in namespace '{1}'", 
reader.LocalName, reader.NamespaceURI));
                                        Type et = elementType == typeof 
(object) || elementType.IsAbstract ? null : elementType;
-                                       if (et == null) {
-                                               string typeName = 
reader.GetAttribute ("__type");
-                                               if (typeName != null)
-                                                       et = Type.GetType 
(ToRuntimeTypeName (typeName), false);
-                                       }
                                        object elem = ReadObject (et ?? typeof 
(object));
                                        c.Add (elem);
                                }

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 07:17:03 UTC (rev 94362)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
  2008-01-30 07:20:11 UTC (rev 94363)
@@ -1,5 +1,10 @@
 2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
+       * DataContractJsonSerializerTest.cs : added deserialization tests
+         for typed object with "__type".
+
+2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
        * JsonReaderTest.cs : test GetAttribute() for "__type".
 
 2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>

Modified: 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs
  2008-01-30 07:17:03 UTC (rev 94362)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs
  2008-01-30 07:20:11 UTC (rev 94363)
@@ -1090,9 +1090,37 @@
                                .ReadObject (XmlReader.Create (new StringReader 
(xml)), true);
                }
 
-               private object Deserialize (string xml, Type type)
+               [Test]
+               public void ReadTypedObjectJson ()
                {
-                       DataContractJsonSerializer ser = new 
DataContractJsonSerializer (type);
+                       object o = Deserialize 
(@"{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}",
 typeof (DCWithEnum));
+                       Assert.AreEqual (typeof (DCWithEnum), o.GetType ());
+               }
+
+               [Test]
+               public void ReadObjectDCArrayJson ()
+               {
+                       object o = Deserialize 
(@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}]",
+                               typeof (object []), typeof (DCWithEnum));
+                       Assert.AreEqual (typeof (object []), o.GetType (), 
"#1");
+                       object [] arr = (object []) o;
+                       Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType 
(), "#2");
+               }
+
+               [Test]
+               public void ReadObjectDCArray2Json ()
+               {
+                       object o = Deserialize 
(@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
+                               typeof (object []), typeof (DCWithEnum), typeof 
(DCSimple1));
+                       Assert.AreEqual (typeof (object []), o.GetType (), 
"#1");
+                       object [] arr = (object []) o;
+                       Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType 
(), "#2");
+                       Assert.AreEqual (typeof (DCSimple1), arr [1].GetType 
(), "#3");
+               }
+
+               private object Deserialize (string xml, Type type, params Type 
[] knownTypes)
+               {
+                       DataContractJsonSerializer ser = new 
DataContractJsonSerializer (type, knownTypes);
                        XmlReader xr = JsonReaderWriterFactory.CreateJsonReader 
(Encoding.UTF8.GetBytes (xml), new XmlDictionaryReaderQuotas ());
                        return ser.ReadObject (xr);
                }

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

Reply via email to