Author: atsushi
Date: 2007-06-05 14:32:46 -0400 (Tue, 05 Jun 2007)
New Revision: 78657
Modified:
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/ChangeLog
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/JavaScriptSerializer.cs
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/ChangeLog
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/JavaScriptSerializerTest.cs
Log:
2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
* JavaScriptSerializer.cs : when JavaScriptTypeResolver is passed,
it tries to serialize objects using "__type" item.
* JavaScriptSerializerTest.cs : added tests for SimpleTypeResolver
-based serialization.
Modified:
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/ChangeLog
===================================================================
---
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/ChangeLog
2007-06-05 18:31:54 UTC (rev 78656)
+++
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/ChangeLog
2007-06-05 18:32:46 UTC (rev 78657)
@@ -1,5 +1,10 @@
2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+ * JavaScriptSerializer.cs : when JavaScriptTypeResolver is passed,
+ it tries to serialize objects using "__type" item.
+
+2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+
* SimpleTypeResolver.cs : almost hacky implementation.
JavaScriptSerializer.cs :
implemented Serialize() and DeserializeObject().
Modified:
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/JavaScriptSerializer.cs
===================================================================
---
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/JavaScriptSerializer.cs
2007-06-05 18:31:54 UTC (rev 78656)
+++
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/JavaScriptSerializer.cs
2007-06-05 18:32:46 UTC (rev 78657)
@@ -15,8 +15,8 @@
List<JavaScriptConverter> converters = new
List<JavaScriptConverter> ();
public JavaScriptSerializer ()
- : this (new SimpleTypeResolver ())
{
+ // with null type resolver.
}
public JavaScriptSerializer (JavaScriptTypeResolver resolver)
@@ -133,7 +133,25 @@
if (i == input.Length || input [i] != '}')
throw new ArgumentException
(String.Format ("Invalid JSON object format; '}}' is expected, at {0}", i));
i++;
- return dic;
+
+ // creates a resolved instance if required.
+ if (resolver == null || !dic.ContainsKey
("__type"))
+ return dic;
+ Type type = resolver.ResolveType (dic
["__type"] as string);
+ if (type == null)
+ // FIXME: could be different kind of
exception?
+ throw new InvalidOperationException
(String.Format ("Type '{0}' cannot be resolved", dic ["__type"]));
+
+ object o = Activator.CreateInstance (type,
false);
+ foreach (KeyValuePair<string,object> p in dic) {
+ if (p.Key == "__type")
+ continue;
+ PropertyInfo prop = type.GetProperty
(p.Key);
+ if (prop != null)
+ // ChangeType() is needed for
example setting double value to int property.
+ prop.SetValue (o,
Convert.ChangeType (p.Value, prop.PropertyType), empty_args);
+ }
+ return o;
default:
if ('0' <= c && c <= '9' || c == '-')
return ReadNumericLiteral (input, ref
i);
@@ -370,8 +388,23 @@
output.Append (']');
} else {
output.Append ('{');
+ bool hasItem = false;
+ if (resolver != null) {
+ string id =
resolver.ResolveTypeId (type);
+ if (id != null) {
+ hasItem = true;
+ output.Append
("\"__type\":\"").Append (type.AssemblyQualifiedName.ToString ()).Append ("\"");
+ }
+ }
foreach (PropertyInfo pi in
type.GetProperties ())
- Serialize (pi.GetValue (obj,
empty_args), output, recursion + 1);
+ if (pi.GetCustomAttributes
(typeof (ScriptIgnoreAttribute), false).Length == 0) {
+ if (hasItem)
+ output.Append
(',');
+ output.Append
('"').Append (EscapeStringLiteral (pi.Name)).Append ("\":");
+ Serialize (pi.GetValue
(obj, empty_args), output, recursion + 1);
+ hasItem = true;
+ }
+ output.Append ('}');
}
break;
}
Modified:
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/ChangeLog
===================================================================
---
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/ChangeLog
2007-06-05 18:31:54 UTC (rev 78656)
+++
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/ChangeLog
2007-06-05 18:32:46 UTC (rev 78657)
@@ -1,4 +1,9 @@
2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+ * JavaScriptSerializerTest.cs : added tests for SimpleTypeResolver
+ -based serialization.
+
+2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+
* JavaScriptSerializerTest.cs, SimpleTypeResolverTest.cs :
new tests.
Modified:
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/JavaScriptSerializerTest.cs
===================================================================
---
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/JavaScriptSerializerTest.cs
2007-06-05 18:31:54 UTC (rev 78656)
+++
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/JavaScriptSerializerTest.cs
2007-06-05 18:32:46 UTC (rev 78657)
@@ -28,6 +28,18 @@
Assert.AreEqual ("\"test\\r\\n/string\\f\"",
ser.Serialize ("test\r\n/string\f"), "#2");
}
+ [Test]
+ public void SerializeObject ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+ string s1 = @"{""P1"":""x1"",""P2"":123}";
+ Assert.AreEqual (s1, ser.Serialize (new Foo ()), "#1");
+
+ ser = new JavaScriptSerializer (new SimpleTypeResolver
());
+ string s2 = @"{""__type"":""" + typeof
(Foo).AssemblyQualifiedName + @""",""P1"":""x1"",""P2"":123}";
+ Assert.AreEqual (s2, ser.Serialize (new Foo ()), "#2");
+ }
+
#endregion
#region Deserialization
@@ -204,6 +216,61 @@
Assert.IsTrue (b [3] is JSONObject, "#9-7");
}
+ [Test]
+ public void DeserializeObjectWithResolver ()
+ {
+ Assert.IsNotNull (new SimpleTypeResolver ().ResolveType
(typeof (Foo).AssemblyQualifiedName), "premise#1");
+
+ JavaScriptSerializer ser = new JavaScriptSerializer
(new SimpleTypeResolver ());
+ string s1 = @"{""__type"":""" + typeof
(Foo).AssemblyQualifiedName + @""",""P1"":""x1"",""P2"":123}";
+ Foo f = ser.DeserializeObject (s1) as Foo;
+ Assert.IsNotNull (f, "#1:" + ser.DeserializeObject
(s1));
+
+ // put __type at last
+ string s2 = @"{""P1"":""x1"",""P2"":123,""__type"":"""
+ typeof (Foo).AssemblyQualifiedName + @"""}";
+ f = ser.DeserializeObject (s2) as Foo;
+ Assert.IsNotNull (f, "#2");
+
+ try {
+ ser.DeserializeObject
("{\"__type\":\"UnresolvableType, nonexistent\"}");
+ Assert.Fail ("#3");
+ } catch (InvalidOperationException) {
+ }
+ }
+
#endregion
+
+ #region Serialization classes
+
+ class Foo
+ {
+ public Foo ()
+ {
+ P1 = "x1";
+ P2 = 123;
+ P3 = "y1";
+ }
+
+ string p1, p3;
+ int p2;
+
+ public string P1 {
+ get { return p1; }
+ set { p1 = value; }
+ }
+
+ public int P2 {
+ get { return p2; }
+ set { p2 = value; }
+ }
+
+ [ScriptIgnore]
+ public string P3 {
+ get { return p3; }
+ set { p3 = value; }
+ }
+ }
+
+ #endregion
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches