Author: atsushi
Date: 2007-06-05 10:40:24 -0400 (Tue, 05 Jun 2007)
New Revision: 78633
Added:
trunk/olive/class/System.Silverlight/System.Silverlight_test.dll.sources
trunk/olive/class/System.Silverlight/Test/
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/ChangeLog
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/JavaScriptSerializerTest.cs
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/SimpleTypeResolverTest.cs
Modified:
trunk/olive/class/System.Silverlight/
trunk/olive/class/System.Silverlight/ChangeLog
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/System.Windows.Browser.Serialization/SimpleTypeResolver.cs
Log:
2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
* SimpleTypeResolver.cs : almost hacky implementation.
JavaScriptSerializer.cs :
implemented Serialize() and DeserializeObject().
* JavaScriptSerializerTest.cs, SimpleTypeResolverTest.cs :
new tests.
* System.Silverlight_test.dll.sources : now we have some tests.
Property changes on: trunk/olive/class/System.Silverlight
___________________________________________________________________
Name: svn:ignore
+ TestResult-net_3_0.log
TestResult-net_3_0.xml
TestResult-ondotnet-net_3_0.xml
TestResult-ondotnet-net_3_0.log
System.Silverlight_test_net_3_0.dll
System.Silverlight_test_net_3_0.dll.mdb
Modified: trunk/olive/class/System.Silverlight/ChangeLog
===================================================================
--- trunk/olive/class/System.Silverlight/ChangeLog 2007-06-05 14:31:54 UTC
(rev 78632)
+++ trunk/olive/class/System.Silverlight/ChangeLog 2007-06-05 14:40:24 UTC
(rev 78633)
@@ -1,3 +1,7 @@
+2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * System.Silverlight_test.dll.sources : now we have some tests.
+
2007-06-02 Atsushi Enomoto <[EMAIL PROTECTED]>
* System.Silverlight.dll.sources :
Added: trunk/olive/class/System.Silverlight/System.Silverlight_test.dll.sources
===================================================================
--- trunk/olive/class/System.Silverlight/System.Silverlight_test.dll.sources
2007-06-05 14:31:54 UTC (rev 78632)
+++ trunk/olive/class/System.Silverlight/System.Silverlight_test.dll.sources
2007-06-05 14:40:24 UTC (rev 78633)
@@ -0,0 +1,2 @@
+System.Windows.Browser.Serialization/JavaScriptSerializerTest.cs
+System.Windows.Browser.Serialization/SimpleTypeResolverTest.cs
Modified:
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/ChangeLog
===================================================================
---
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/ChangeLog
2007-06-05 14:31:54 UTC (rev 78632)
+++
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/ChangeLog
2007-06-05 14:40:24 UTC (rev 78633)
@@ -1,3 +1,9 @@
+2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * SimpleTypeResolver.cs : almost hacky implementation.
+ JavaScriptSerializer.cs :
+ implemented Serialize() and DeserializeObject().
+
2007-06-02 Atsushi Enomoto <[EMAIL PROTECTED]>
* JavaScriptConverter.cs JavaScriptSerializer.cs
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 14:31:54 UTC (rev 78632)
+++
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/JavaScriptSerializer.cs
2007-06-05 14:40:24 UTC (rev 78633)
@@ -1,5 +1,8 @@
using System;
+using System.Collections;
using System.Collections.Generic;
+using System.Globalization;
+using System.Reflection;
using System.Text;
using System.Security.Permissions;
@@ -8,7 +11,8 @@
public class JavaScriptSerializer
{
JavaScriptTypeResolver resolver;
- int max_json_length, recursion_limit;
+ int max_json_length, recursion_limit = 100;
+ List<JavaScriptConverter> converters = new
List<JavaScriptConverter> ();
public JavaScriptSerializer ()
: this (new SimpleTypeResolver ())
@@ -28,18 +32,23 @@
throw new NotImplementedException ();
}
- [MonoTODO]
public T Deserialize<T> (string input)
{
- throw new NotImplementedException ();
+ return ConvertToType<T> (DeserializeObject (input));
}
-
- [MonoTODO]
+
+ [MonoLimitation ("no max-length check is done (is it
required?)")]
public object DeserializeObject (string input)
{
- throw new NotImplementedException ();
+ if (input == null)
+ throw new ArgumentNullException ("input");
+ int i = 0;
+ object o = DeserializeCore (input, ref i);
+ if (i != input.Length)
+ throw new ArgumentException (String.Format
("Invalid JSON primitive at {0}", i));
+ return o;
}
-
+
public int MaxJsonLength {
get { return max_json_length; }
set {
@@ -58,24 +67,367 @@
}
}
- [MonoTODO]
public void RegisterConverters
(IEnumerable<JavaScriptConverter> converters)
{
- throw new NotImplementedException ();
+ foreach (JavaScriptConverter c in converters)
+ if (!this.converters.Contains (c))
+ this.converters.Add (c);
}
- [MonoTODO]
public string Serialize (object obj)
{
StringBuilder sb = new StringBuilder ();
Serialize (obj, sb);
return sb.ToString ();
}
-
- [MonoTODO]
+
+ [MonoLimitation ("No max-length check is done yet")]
public void Serialize (object obj, StringBuilder output)
{
- throw new NotImplementedException ();
+ Serialize (obj, output, 0);
}
+
+ #region Deserialization impl.
+
+ object DeserializeCore (string input, ref int i)
+ {
+ i = SkipSpaces (input, i);
+ if (i == input.Length)
+ return null;
+ char c = input [i];
+ switch (c) {
+ case '[':
+ i++;
+ ArrayList list = new ArrayList ();
+ i = SkipSpaces (input, i);
+ while (true) {
+ list.Add (DeserializeCore (input, ref
i));
+ i = SkipSpaces (input, i);
+ if (i == input.Length || input [i] !=
',')
+ break;
+ i++;
+ }
+ if (i == input.Length || input [i] != ']')
+ throw new ArgumentException
(String.Format ("Invalid JSON array format; ']' is expected, at {0}", i));
+ i++;
+ return list.ToArray ();
+ case '{':
+ i++;
+ Dictionary<string,object> dic = new
Dictionary<string,object> ();
+ i = SkipSpaces (input, i);
+ bool repeat = false;
+ while (repeat || i < input.Length && input [i]
!= '}') {
+ string name = ReadStringLiteral (input,
ref i);
+ i = SkipSpaces (input, i);
+ if (i == input.Length || input [i] !=
':')
+ throw new ArgumentException
(String.Format ("Invalid JSON object format; ':' is expected after a name, at
{0}", i));
+ i++;
+ dic.Add (name, DeserializeCore (input,
ref i));
+ i = SkipSpaces (input, i);
+ if (i == input.Length || input [i] !=
',')
+ break;
+ repeat = true; // after comma, an item
is mandatory
+ i++;
+ i = SkipSpaces (input, i);
+ }
+ if (i == input.Length || input [i] != '}')
+ throw new ArgumentException
(String.Format ("Invalid JSON object format; '}}' is expected, at {0}", i));
+ i++;
+ return dic;
+ default:
+ if ('0' <= c && c <= '9' || c == '-')
+ return ReadNumericLiteral (input, ref
i);
+ if (c == '"')
+ return ReadStringLiteral (input, ref i);
+ break;
+ }
+ if (String.CompareOrdinal (input, i, "null", 0, 4) ==
0) {
+ i += 4;
+ return null;
+ }
+
+ throw new ArgumentException (String.Format ("Invalid
JSON format; extra token at {0}", i));
+ }
+
+ static int SkipSpaces (string s, int i)
+ {
+ while (i < s.Length) {
+ switch (s [i]) {
+ case ' ': case '\t': case '\r': case '\n':
+ i++;
+ continue;
+ }
+ return i;
+ }
+ return i;
+ }
+
+ static decimal ReadNumericLiteral (string input, ref int i)
+ {
+ bool negative = false;
+ if (input [i] == '-') {
+ negative = true;
+ i++;
+ if (i == input.Length)
+ throw new ArgumentException
(String.Format ("Invalid JSON numeric literal; extra negation at {0}", i - 1));
+ }
+ return (negative ? -1 : 1) * ReadPositiveNumericLiteral
(input, ref i);
+ }
+
+ static decimal ReadPositiveNumericLiteral (string input, ref
int i)
+ {
+ int val = 0;
+ int start = i;
+ int leadingZeroAt = i < input.Length && input [i] ==
'0' ? i : -1;
+ for (;i < input.Length; i++) {
+ if (input [i] < '0' || '9' < input [i])
+ break;
+ val = val * 10 + (input [i] - '0');
+ }
+ if (leadingZeroAt >= 0 && i > leadingZeroAt + 1)
+ throw new ArgumentException (String.Format
("Invalid JSON numeric literal; leading multiple zeros are not allowed at {0}",
leadingZeroAt));
+ if (i == input.Length)
+ return val;
+ decimal frac = 0;
+ int fdigits = 0;
+ if (input [i] == '.') {
+ i++;
+ if (i == input.Length)
+ throw new ArgumentException
(String.Format ("Invalid JSON numeric literal; extra dot at {0}", i - 1));
+ for (decimal d = 10; i < input.Length; i++) {
+ if (input [i] < '0' || '9' < input [i])
+ break;
+ frac += (input [i] - '0') / d;
+ d *= 10;
+ fdigits++;
+ }
+ if (fdigits == 0)
+ throw new ArgumentException
(String.Format ("Invalid JSON numeric literal; extra dot at {0}", i - 1));
+ }
+ frac = Decimal.Round (frac, fdigits);
+ if (i == input.Length || input [i] != 'e' && input [i]
!= 'E')
+ return val + frac;
+ i++;
+ int exp = 0;
+ if (i == input.Length)
+ throw new ArgumentException (String.Format
("Invalid JSON numeric literal; extra exponent at {0}", i - 1));
+ bool negexp = false;
+ if (input [i] == '-') {
+ i++;
+ negexp = true;
+ }
+ else if (input [i] == '+')
+ i++;
+ if (i == input.Length)
+ throw new ArgumentException (String.Format
("Invalid JSON numeric literal; extra exponent at {0}", i - 1));
+ for (;i < input.Length; i++) {
+ if (input [i] < '0' || '9' < input [i])
+ break;
+ exp = exp * 10 + (input [i] - '0');
+ }
+ // it is messy to handle exponent, so I just use
Decimal.Parse() with assured JSON format.
+ return Decimal.Parse (input.Substring (start, i -
start), NumberStyles.Any, CultureInfo.InvariantCulture);
+ }
+
+ static string ReadStringLiteral (string input, ref int i)
+ {
+ if (input [i] != '"')
+ throw new ArgumentException (String.Format
("Invalid JSON string literal format at {0}", i));
+
+ i++;
+ StringBuilder sb = null;
+ for (int start = i; i < input.Length; i++) {
+ if (input [i] == '"') {
+ string remaining = input.Substring
(start, i - start);
+ i++;
+ if (sb != null)
+ return sb.Append
(remaining).ToString ();
+ else
+ return remaining;
+ }
+ else if (input [i] != '\\')
+ continue;
+
+ // escaped expression
+ if (sb == null)
+ sb = new StringBuilder ();
+ if (i != start)
+ sb.Append (input, start, i - start);
+ i++;
+ if (i == input.Length)
+ throw new ArgumentException
(String.Format ("Invalid JSON string literal; incomplete escape sequence at
{0}", i - 1));
+ switch (input [i]) {
+ case '"':
+ case '\\':
+ case '/':
+ sb.Append (input [i]);
+ break;
+ case 'b':
+ sb.Append ('\x8');
+ break;
+ case 'f':
+ sb.Append ('\f');
+ break;
+ case 'n':
+ sb.Append ('\n');
+ break;
+ case 'r':
+ sb.Append ('\r');
+ break;
+ case 't':
+ sb.Append ('\t');
+ break;
+ case 'u':
+ ushort cp;
+ if (!ushort.TryParse (input.Substring
(i + 1, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out cp))
+ throw new ArgumentException
(String.Format ("Invalid JSON string literal; \\u format expects four digits,
at {0}", i));
+ i += 4; // another 1 is added later.
+Console.WriteLine ("[{0}]", sb);
+ sb.Append ((char) cp);
+Console.WriteLine ("[{0}]", sb);
+ break;
+ default:
+ throw new ArgumentException
(String.Format ("Invalid JSON string literal; unexpected escape character at
{0}", i));
+ }
+ start = i + 1;
+ }
+ throw new ArgumentException ("Invalid JSON string
literal; missing closing quotation");
+ }
+
+ #endregion
+
+ #region Serialization impl.
+
+ static readonly object [] empty_args = new object [0];
+
+ void Serialize (object obj, StringBuilder output, int recursion)
+ {
+ if (recursion == RecursionLimit)
+ throw new InvalidOperationException ("The
serialization process reached the recursion limit");
+
+ if (obj == null) {
+ output.Append ("null");
+ return;
+ }
+
+ Type type = obj.GetType ();
+ foreach (JavaScriptConverter c in converters)
+ foreach (Type t in c.SupportedTypes)
+ if (t == type) {
+ Serialize (c.Serialize (obj,
this), output, recursion + 1);
+ return;
+ }
+
+ switch (Type.GetTypeCode (type)) {
+ case TypeCode.Boolean:
+ output.Append (((bool) obj) ? "true" : "false");
+ break;
+ case TypeCode.Char:
+ case TypeCode.String:
+ // FIXME: escape string
+ output.Append ('"').Append (EscapeStringLiteral
(obj.ToString ())).Append ('"');
+ break;
+ case TypeCode.Byte:
+ case TypeCode.Decimal:
+ case TypeCode.Single:
+ case TypeCode.Double:
+ case TypeCode.Int16:
+ case TypeCode.Int32:
+ case TypeCode.Int64:
+ case TypeCode.UInt16:
+ case TypeCode.UInt32:
+ case TypeCode.UInt64:
+ case TypeCode.SByte:
+ output.Append (obj);
+ break;
+ case TypeCode.DateTime:
+ output.Append ("\"\\/Date(").Append
(((DateTime) obj).Ticks).Append (")\\/");
+ break;
+ case TypeCode.DBNull:
+ output.Append ("null");
+ break;
+ case TypeCode.Object:
+ case TypeCode.Empty:
+ if (obj is IDictionary<string,object>) {
+ IDictionary<string,object> map =
(IDictionary<string,object>) obj;
+ output.Append ('{');
+ foreach (KeyValuePair<string,object> p
in map) {
+ output.Append ('"').Append
(p.Key).Append ("\":");
+ Serialize (p.Value, output,
recursion + 1);
+ output.Append (',');
+ }
+ if (output [output.Length - 1] == ',')
+ output.Length--;
+ output.Append ('}');
+ } else if (obj is IEnumerable) {
+ output.Append ('[');
+ foreach (object item in (IEnumerable)
obj) {
+ Serialize (item, output,
recursion + 1);
+ output.Append (',');
+ }
+ if (output [output.Length - 1] == ',')
+ output.Length--;
+ output.Append (']');
+ } else {
+ output.Append ('{');
+ foreach (PropertyInfo pi in
type.GetProperties ())
+ Serialize (pi.GetValue (obj,
empty_args), output, recursion + 1);
+ }
+ break;
+ }
+ }
+
+ static void AppendBuffer (ref StringBuilder sb, string input,
int start, int i, string append)
+ {
+ if (sb == null)
+ sb = new StringBuilder ();
+ if (i != start)
+ sb.Append (input, start, i - start);
+ sb.Append (append);
+ }
+
+ static string EscapeStringLiteral (string input)
+ {
+ StringBuilder sb = null;
+ int i = 0, start = 0;
+ for (; i < input.Length; i++) {
+ switch (input [i]) {
+ case '"':
+ AppendBuffer (ref sb, input, start, i,
@"\""");
+ break;
+ case '\\':
+ AppendBuffer (ref sb, input, start, i,
@"\\");
+ break;
+ //case '/':
+ // AppendBuffer (ref sb, input, start, i,
@"\/");
+ // break;
+ case '\x8':
+ AppendBuffer (ref sb, input, start, i,
@"\b");
+ break;
+ case '\f':
+ AppendBuffer (ref sb, input, start, i,
@"\f");
+ break;
+ case '\n':
+ AppendBuffer (ref sb, input, start, i,
@"\n");
+ break;
+ case '\r':
+ AppendBuffer (ref sb, input, start, i,
@"\r");
+ break;
+ case '\t':
+ AppendBuffer (ref sb, input, start, i,
@"\t");
+ break;
+ default:
+ continue;
+ }
+ start = i + 1;
+ }
+ string remaining = input.Substring (start, i - start);
+ if (sb != null)
+ return sb.Append (remaining).ToString ();
+ else
+ return remaining;
+ }
+
+ #endregion
}
}
Modified:
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/SimpleTypeResolver.cs
===================================================================
---
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/SimpleTypeResolver.cs
2007-06-05 14:31:54 UTC (rev 78632)
+++
trunk/olive/class/System.Silverlight/System.Windows.Browser.Serialization/SimpleTypeResolver.cs
2007-06-05 14:40:24 UTC (rev 78633)
@@ -11,7 +11,8 @@
{
if (id == null)
throw new ArgumentNullException ("id");
- throw new NotImplementedException ();
+
+ return Type.GetType (id);
}
[MonoTODO]
@@ -19,7 +20,8 @@
{
if (type == null)
throw new ArgumentNullException ("type");
- throw new NotImplementedException ();
+
+ return type.FullName;
}
}
}
Added:
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 14:31:54 UTC (rev 78632)
+++
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/ChangeLog
2007-06-05 14:40:24 UTC (rev 78633)
@@ -0,0 +1,4 @@
+2007-06-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * JavaScriptSerializerTest.cs, SimpleTypeResolverTest.cs :
+ new tests.
Added:
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 14:31:54 UTC (rev 78632)
+++
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/JavaScriptSerializerTest.cs
2007-06-05 14:40:24 UTC (rev 78633)
@@ -0,0 +1,209 @@
+using System;
+using System.Collections.Generic;
+using System.Windows.Browser.Serialization;
+
+using NUnit.Framework;
+
+using JSONObject = System.Collections.Generic.Dictionary<string,object>;
+
+namespace MonoTests.System.Windows.Browser.Serialization
+{
+ [TestFixture]
+ public class JavaScriptSerializerTest
+ {
+ [Test]
+ public void DefaultValues ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+ Assert.AreEqual (100, ser.RecursionLimit, "#1");
+ }
+
+ #region Serialization
+
+ [Test]
+ public void SerializeStringLiteral ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+ Assert.AreEqual ("\"test string\"", ser.Serialize
("test string"), "#1");
+ Assert.AreEqual ("\"test\\r\\n/string\\f\"",
ser.Serialize ("test\r\n/string\f"), "#2");
+ }
+
+ #endregion
+
+ #region Deserialization
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void DeserializeNullArg ()
+ {
+ new JavaScriptSerializer ().DeserializeObject (null);
+ }
+
+ [Test]
+ public void DeserializeNull ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+ Assert.IsNull (ser.DeserializeObject (""), "#1");
+ Assert.IsNull (ser.DeserializeObject ("null"), "#2");
+ }
+
+ [Test]
+ [Category ("NotDotNet")]
+ public void DeserializeNumbers ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+ Assert.AreEqual (-12345, ser.DeserializeObject
("-12345"), "#2");
+
+ // they are .NET bugs.
+ try {
+ ser.DeserializeObject ("01");
+ Assert.Fail ("#3");
+ } catch (ArgumentException) {
+ }
+ try {
+ ser.DeserializeObject ("123.");
+ Assert.Fail ("#4");
+ } catch (ArgumentException) {
+ }
+ // FIXME: do not skip
+ Assert.AreEqual (-12345.000m, ser.DeserializeObject
("-12345.000"), "#5");
+ Assert.AreEqual (-12345.04689, ser.DeserializeObject
("-12345.04689"), "#6");
+ try {
+ ser.DeserializeObject ("123e");
+ Assert.Fail ("#7");
+ } catch (ArgumentException) {
+ }
+ // it is a .NET bug.
+ try {
+ ser.DeserializeObject ("123.e4");
+ Assert.Fail ("#8");
+ } catch (ArgumentException) {
+ }
+ try {
+ ser.DeserializeObject ("123.5-e4");
+ Assert.Fail ("#9");
+ } catch (ArgumentException) {
+ }
+ Assert.AreEqual (-123.5e-4, ser.DeserializeObject
("-123.5e-4"), "#10");
+ Assert.AreEqual (-123.5e4, ser.DeserializeObject
("-123.5e+4"), "#11");
+ try {
+ ser.DeserializeObject ("123.5e4.7");
+ Assert.Fail ("#12");
+ } catch (ArgumentException) {
+ }
+ }
+
+ [Test]
+ public void DeserializeString ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+ Assert.AreEqual ("test", ser.DeserializeObject
("\"test\""), "#1");
+ try {
+ ser.DeserializeObject ("'test'");
+ Assert.Fail ("#2");
+ } catch (ArgumentException) {
+ }
+ Assert.AreEqual ("test\\\r\n\t\f\u0065test",
ser.DeserializeObject ("\"test\\\\\\r\\n\\t\\f\\u0065test\""), "#3");
+// try {
+// ser.DeserializeObject ("test\\u10000");
+// Assert.Fail ("#4");
+// } catch (ArgumentException) {
+// }
+ // FIXME: not working (BTW .net is buggy too to fail
here)
+ // Assert.AreEqual ("test\u1000" + "0",
ser.DeserializeObject ("test\\u10000"), "#5");
+ }
+
+ [Test]
+ public void DeserializeArray ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+ object [] arrobj = ser.DeserializeObject ("[0, \t 1,
-2,3 ]") as object [];
+ Assert.IsNotNull (arrobj, "#1-1");
+ Assert.AreEqual (4, arrobj.Length, "#1-2");
+ Assert.AreEqual (-2, arrobj [2], "#1-3");
+
+ arrobj = ser.DeserializeObject ("[0, \"[]\",
[1,2],[3,4], {}]") as object [];
+ Assert.IsNotNull (arrobj, "#2-1");
+ Assert.AreEqual (5, arrobj.Length, "#2-2");
+ Assert.AreEqual ("[]", arrobj [1], "#2-3");
+ Assert.IsTrue (arrobj [2] is object [], "#2-4");
+ Assert.IsTrue (arrobj [4] is Dictionary<string,object>,
"#2-5");
+
+ try {
+ ser.DeserializeObject
("[\"abc\",[123,\"[]]\"]");
+ Assert.Fail ("#3");
+ } catch (ArgumentException) {
+ }
+
+ try {
+ ser.DeserializeObject ("[\"abc\" 123]");
+ Assert.Fail ("#4");
+ } catch (ArgumentException) {
+ }
+ }
+
+ [Test]
+ public void DeserializeObject ()
+ {
+ JavaScriptSerializer ser = new JavaScriptSerializer ();
+
+ JSONObject dic = ser.DeserializeObject ("{}") as
JSONObject;
+ Assert.IsNotNull (dic, "#1-1");
+ Assert.AreEqual (0, dic.Count, "#1-2");
+
+ try {
+ ser.DeserializeObject ("{1}");
+ Assert.Fail ("#2");
+ } catch (ArgumentException) {
+ }
+
+ // .NET fails to reject it.
+ try {
+ ser.DeserializeObject ("{1:2}");
+ Assert.Fail ("#3");
+ } catch (ArgumentException) {
+ }
+
+ try {
+ ser.DeserializeObject ("{\"ABC\"}");
+ Assert.Fail ("#4");
+ } catch (ArgumentException) {
+ }
+
+ try {
+ ser.DeserializeObject ("{\"ABC\":}");
+ Assert.Fail ("#5");
+ } catch (ArgumentException) {
+ }
+
+ dic = ser.DeserializeObject ("{ \"A\":123 , \"1\" :
1}") as JSONObject;
+ Assert.IsNotNull (dic, "#6-1");
+ Assert.AreEqual (2, dic.Count, "#6-2");
+
+ try {
+ ser.DeserializeObject ("{\"ABC\": 1,}");
+ Assert.Fail ("#7");
+ } catch (ArgumentException) {
+ }
+
+ try {
+ ser.DeserializeObject ("{\"A\":123 , \"1\":
{\"nest\":\"{}}\"}");
+ Assert.Fail ("#8");
+ } catch (ArgumentException) {
+ }
+
+ dic = ser.DeserializeObject ("{\"A\": {\"x\":{}},\"B\"
:[1,2,3,{\"nest\":543}]}") as JSONObject;
+ Assert.IsNotNull (dic, "#9-1");
+ Assert.AreEqual (2, dic.Count, "#9-2");
+ JSONObject a = dic ["A"] as JSONObject;
+ Assert.IsNotNull (a, "#9-3");
+ Assert.IsTrue (a ["x"] is JSONObject, "#9-4");
+ object [] b = dic ["B"] as object [];
+ Assert.IsNotNull (b, "#9-5");
+ Assert.AreEqual (4, b.Length, "#9-6");
+ Assert.IsTrue (b [3] is JSONObject, "#9-7");
+ }
+
+ #endregion
+ }
+}
Added:
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/SimpleTypeResolverTest.cs
===================================================================
---
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/SimpleTypeResolverTest.cs
2007-06-05 14:31:54 UTC (rev 78632)
+++
trunk/olive/class/System.Silverlight/Test/System.Windows.Browser.Serialization/SimpleTypeResolverTest.cs
2007-06-05 14:40:24 UTC (rev 78633)
@@ -0,0 +1,57 @@
+using System;
+using System.Windows.Browser.Serialization;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Windows.Browser.Serialization
+{
+ [TestFixture]
+ public class SimpleTypeResolverTest
+ {
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void ResolveTypeIdNull ()
+ {
+ new SimpleTypeResolver ().ResolveType (null);
+ }
+
+ [Test]
+ [Category ("NotWorking")]
+ public void ResolveTypeIdFine ()
+ {
+ SimpleTypeResolver r = new SimpleTypeResolver ();
+ Assert.AreEqual (typeof (string).AssemblyQualifiedName,
r.ResolveTypeId (typeof (string)), "#1");
+ Assert.AreEqual (typeof (double).AssemblyQualifiedName,
r.ResolveTypeId (typeof (double)), "#2");
+ Assert.AreEqual (typeof (bool).AssemblyQualifiedName,
r.ResolveTypeId (typeof (bool)), "#3");
+ Assert.AreEqual (typeof (void).AssemblyQualifiedName,
r.ResolveTypeId (typeof (void)), "#4");
+ Assert.AreEqual (typeof (Array).AssemblyQualifiedName,
r.ResolveTypeId (typeof (Array)), "#5");
+ Assert.AreEqual (typeof (object).AssemblyQualifiedName,
r.ResolveTypeId (typeof (object)), "#6");
+
+ Assert.AreEqual (typeof (float).AssemblyQualifiedName,
r.ResolveTypeId (typeof (float)), "#7");
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void ResolveTypeNull ()
+ {
+ new SimpleTypeResolver ().ResolveType (null);
+ }
+
+ [Test]
+// [Ignore ("This test is still wrong")]
+ public void ResolveTypeFine ()
+ {
+ SimpleTypeResolver r = new SimpleTypeResolver ();
+// Assert.AreEqual (typeof (string), r.ResolveType
("string"), "#1");
+ Assert.IsNull (r.ResolveType ("string"), "#1");
+ Assert.AreEqual (typeof (string), r.ResolveType
("System.String"), "#1-2");
+ /*
+ Assert.AreEqual (typeof (double), r.ResolveType
("number"), "#2");
+ Assert.AreEqual (typeof (bool), r.ResolveType
("boolean"), "#3");
+ Assert.AreEqual (typeof (void), r.ResolveType ("null"),
"#4");
+ Assert.AreEqual (typeof (Array), r.ResolveType
("array"), "#5");
+ Assert.AreEqual (typeof (object), r.ResolveType
("object"), "#6");
+ */
+ }
+ }
+}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches