2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Web.Extensions / Test / System.Web.Script.Serialization / JavaScriptSerializerTest.cs
1 //
2 // JavaScriptSerializer.cs
3 //
4 // Author:
5 //   Konstantin Triger <kostat@mainsoft.com>
6 //
7 // (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.Text;
33 using NUnit.Framework;
34 using System.Web.Script.Serialization;
35 using System.Reflection;
36 using System.Collections;
37 using System.Drawing;
38 using ComponentModel = System.ComponentModel;
39 using System.Globalization;
40 using System.Threading;
41 using System.Text.RegularExpressions;
42 using System.ComponentModel;
43 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
44 using System.Web.UI.WebControls;
45 using System.Collections.ObjectModel;
46
47
48 namespace MonoTests.System.Web.Script.Serialization
49 {
50         [TestFixture]
51         public class JavaScriptSerializerTest
52         {
53                 enum MyEnum
54                 {
55                         AAA,
56                         BBB,
57                         CCC
58                 }
59 #pragma warning disable 659
60                 class bug
61                 {
62                         //public DateTime dt;
63                         //public DateTime dt1;
64                         //public DateTime dt2;
65                         public bool bb;
66                         //Hashtable hash;
67
68                         public void Init() {
69                                 //dt = DateTime.MaxValue;
70                                 //dt1 = DateTime.MinValue;
71                                 //dt2 = new DateTime ((DateTime.Now.Ticks / 10000) * 10000);
72                                 bb = true;
73                                 //hash = new Hashtable ();
74                                 //hash.Add ("mykey", 1);
75                         }
76
77                         public override bool Equals (object obj) {
78                                 if (!(obj is bug))
79                                         return false;
80                                 JavaScriptSerializerTest.FieldsEqual (this, obj);
81                                 return true;
82                         }
83                 }
84                 class X
85                 {
86                         int x = 5;
87                         //int y;
88                         ulong _bb;
89                         Y[] _yy;
90                         Y [] _yyy = new Y [] { new Y (), new Y () };
91                         public int z;
92                         public char ch;
93                         public char ch_null;
94                         public string str;
95                         public byte b;
96                         public sbyte sb;
97                         public short sh;
98                         public ushort ush;
99                         public int i;
100                         public uint ui;
101                         public long l;
102                         public ulong ul;
103                         
104                         public float f;
105                         public float f1;
106                         public float f2;
107                         public float f3;
108                         public float f4;
109
110                         public double d;
111                         public double d1;
112                         public double d2;
113                         public double d3;
114                         public double d4;
115
116                         public decimal de;
117                         public decimal de1;
118                         public decimal de2;
119                         public decimal de3;
120                         public decimal de4;
121
122                         
123
124                         public Guid g;
125                         
126                         public Nullable<bool> nb;
127                         public DBNull dbn;
128                         IEnumerable<int> enum_int;
129                         IEnumerable enum_int1;
130                         public Uri uri;
131                         public Dictionary<string, Y> hash;
132                         public Point point;
133
134                         public void Init () {
135                                 //y = 6;
136                                 _bb = ulong.MaxValue - 5;
137                                 _yy = new Y [] { new Y (), new Y () };
138                                 z = 8;
139                                 ch = (char) 0xFF56;
140                                 ch_null = '\0';
141                                 str = "\uFF56\uFF57\uF58FF59g";
142                                 b = 253;
143                                 sb = -48;
144                                 sh = short.MinValue + 28;
145                                 ush = ushort.MaxValue - 24;
146                                 i = -234235453;
147                                 ui = uint.MaxValue - 234234;
148                                 l = long.MinValue + 28;
149                                 ul = ulong.MaxValue - 3;
150
151                                 f = float.NaN;
152                                 f1 = float.NegativeInfinity;
153                                 f2 = float.PositiveInfinity;
154                                 f3 = float.MinValue;
155                                 f4 = float.MaxValue;
156
157                                 d = double.NaN;
158                                 d1 = double.NegativeInfinity;
159                                 d2 = double.PositiveInfinity;
160                                 d3 = double.MinValue;
161                                 d4 = double.MaxValue;
162
163                                 de = decimal.MinusOne;
164                                 de1 = decimal.Zero;
165                                 de2 = decimal.One;
166                                 de3 = decimal.MinValue;
167                                 de4 = decimal.MaxValue;
168
169                                 g = new Guid (234, 2, 354, new byte [] { 1, 2, 3, 4, 5, 6, 7, 8 });
170                                 
171                                 nb = null;
172                                 dbn = null;
173
174                                 enum_int = new List<int> (MyEnum);
175                                 enum_int1 = new ArrayList ();
176                                 foreach (object obj in MyEnum1)
177                                         ((ArrayList) enum_int1).Add (obj);
178                                 uri = new Uri ("http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d", UriKind.RelativeOrAbsolute);
179
180                                 hash = new Dictionary<string, Y> ();
181                                 Y y = new Y ();
182                                 hash ["mykey"] = y;
183                                 point = new Point (150, 150);
184                         }
185
186                         public IEnumerable<int> MyEnum {
187                                 get {
188                                         yield return 1;
189                                         yield return 10;
190                                         yield return 345;
191                                 }
192
193                                 set {
194                                         enum_int = value;
195                                 }
196                         }
197
198                         public IEnumerable MyEnum1 {
199                                 get {
200                                         yield return 1;
201                                         yield return 10;
202                                         yield return 345;
203                                 }
204
205                                 set {
206                                         enum_int1 = value;
207                                 }
208                         }
209
210                         public int AA {
211                                 get { return x; }
212                         }
213
214                         public Y[] AA1 {
215                                 get { return _yyy; }
216                         }
217
218                         public ulong BB {
219                                 get { return _bb; }
220                                 set { _bb = value; }
221                         }
222
223                         public Y[] YY {
224                                 get { return _yy; }
225                                 set { _yy = value; }
226                         }
227
228                         public override bool Equals (object obj) {
229                                 if (!(obj is X))
230                                         return false;
231                                 JavaScriptSerializerTest.FieldsEqual (this, obj);
232                                 return true;
233                         }
234                 }
235
236                 class Y
237                 {
238
239                         long _bb = 10;
240
241                         public long BB {
242                                 get { return _bb; }
243                                 set { _bb = value; }
244                         }
245
246                         public override bool Equals (object obj) {
247                                 if (!(obj is Y))
248                                         return false;
249                                 JavaScriptSerializerTest.FieldsEqual(this, obj);
250                                 return true;
251                         }
252                 }
253
254                 class YY
255                 {
256                         public YY () 
257                         {
258                                 Y1 = new Y ();
259                                 Y2 = new Y ();
260                         }
261
262                         public Y Y1;
263                         public Y Y2;
264                 }
265
266                 [TypeConverter (typeof (MyUriConverter))]
267                 class MyUri : Uri
268                 {
269                         public MyUri (string uriString, UriKind uriKind)
270                                 : base (uriString, uriKind) {
271                         }
272
273                         public MyUri (Uri value)
274                                 : base (value.AbsoluteUri) {
275                         }
276                 }
277
278                 class MyUriConverter : UriTypeConverter
279                 {
280                         public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
281                         {
282                                 return base.ConvertTo (context, culture, value, destinationType);
283                         }
284
285                         public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) 
286                         {
287                                 Uri convertedUri = (Uri)base.ConvertFrom (context, culture, value);
288                                 return new MyUri (convertedUri);
289                         }
290                 }
291
292                 [TypeConverter(typeof(MyPointConverter))]
293                 class MyPointContainer
294                 {
295                         public MyPointContainer () 
296                         {
297                         }
298
299                         public MyPointContainer (Point v) 
300                         {
301                                 p = v;
302                         }
303
304                         internal Point p;
305                 }
306
307                 class MyPointConverter : TypeConverter
308                 {
309                         public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType) 
310                         {
311                                 if (destinationType == typeof (string)) {
312                                         return true;
313                                 }
314                                 return base.CanConvertTo (context, destinationType);
315                         }
316
317                         public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
318                         {
319                                 if (destinationType == typeof (string)) {
320                                         MyPointContainer pc = (MyPointContainer) value;
321                                         return pc.p.X + "," + pc.p.Y;
322                                 }
323                                 return base.ConvertTo (context, culture, value, destinationType);
324                         }
325
326                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType) 
327                         {
328                                 if (sourceType == typeof (string)) {
329                                         return true;
330                                 }
331                                 return base.CanConvertFrom (context, sourceType);
332                         }
333
334                         public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) 
335                         {
336                                 if (value is string) {
337                                         string [] v = ((string) value).Split (new char [] { ',' });
338                                         return new MyPointContainer(new Point (int.Parse (v [0]), int.Parse (v [1])));
339                                 }
340                                 return base.ConvertFrom (context, culture, value);
341                         }
342                 }
343
344 #pragma warning restore 659
345
346                 [Test]
347                 [Category ("NotDotNet")]
348                 public void TestDefaults () {
349                         JavaScriptSerializer ser = new JavaScriptSerializer ();
350                         Assert.AreEqual (102400, ser.MaxJsonLength);
351                         Assert.AreEqual (100, ser.RecursionLimit);
352                         //List<JavaScriptConverter> l = new List<JavaScriptConverter> ();
353                         //l.Add (new MyJavaScriptConverter ());
354                         //ser.RegisterConverters (l);
355                         //string x = ser.Serialize (new X [] { new X (), new X () });
356                         //string s = ser.Serialize (new X());
357                         //"{\"BB\":10,\"__type\":\"Tests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null\"}"
358                         //X x = ser.Deserialize<X> (s);
359                         //object ddd = typeof (Y).GetMember ("BB");
360                         //object x1 = ser.Deserialize<X []> (null);
361                         //object x2 = ser.Deserialize<X []> ("");
362                         //object d = ser.Deserialize<X[]> (x);
363                 }
364
365                 [Test]
366                 public void TestDeserializeUnquotedKeys ()
367                 {
368                         JavaScriptSerializer ser = new JavaScriptSerializer ();
369                         IDictionary dict = ser.Deserialize <Dictionary <string, object>> ("{itemOne:\"1\",itemTwo:\"2\"}");
370
371                         Assert.AreEqual ("1", dict ["itemOne"], "#A1");
372                         Assert.AreEqual ("2", dict ["itemTwo"], "#A2");
373
374                         dict = ser.Deserialize <Dictionary <string, object>> ("{itemOne:1,itemTwo:2}");
375                         Assert.AreEqual (1, dict ["itemOne"], "#B1");
376                         Assert.AreEqual (2, dict ["itemTwo"], "#B2");
377                 }
378
379                 [Test]
380                 public void TestDeserializeUnquotedKeysWithSpaces ()
381                 {
382                         JavaScriptSerializer ser = new JavaScriptSerializer ();
383                         IDictionary dict = ser.Deserialize <Dictionary <string, object>> ("{ itemOne :\"1\",itemTwo:\"2\"}");
384
385                         Assert.AreEqual ("1", dict ["itemOne"], "#A1");
386                         Assert.AreEqual ("2", dict ["itemTwo"], "#A2");
387
388                         dict = ser.Deserialize <Dictionary <string, object>> ("{   itemOne   :1,   itemTwo   :2}");
389                         Assert.AreEqual (1, dict ["itemOne"], "#B1");
390                         Assert.AreEqual (2, dict ["itemTwo"], "#B2");
391                 }
392                 
393                 [Test]
394                 public void TestDeserialize () {
395                         JavaScriptSerializer ser = new JavaScriptSerializer ();
396                         Assert.IsNull (ser.Deserialize<X> (""));
397
398                         X s = new X ();
399                         s.Init ();
400                         string x = ser.Serialize (s);
401
402                         Assert.AreEqual ("{\"z\":8,\"ch\":\"v\",\"ch_null\":null,\"str\":\"vwF59g\",\"b\":253,\"sb\":-48,\"sh\":-32740,\"ush\":65511,\"i\":-234235453,\"ui\":4294733061,\"l\":-9223372036854775780,\"ul\":18446744073709551612,\"f\":NaN,\"f1\":-Infinity,\"f2\":Infinity,\"f3\":-3.40282347E+38,\"f4\":3.40282347E+38,\"d\":NaN,\"d1\":-Infinity,\"d2\":Infinity,\"d3\":-1.7976931348623157E+308,\"d4\":1.7976931348623157E+308,\"de\":-1,\"de1\":0,\"de2\":1,\"de3\":-79228162514264337593543950335,\"de4\":79228162514264337593543950335,\"g\":\"000000ea-0002-0162-0102-030405060708\",\"nb\":null,\"dbn\":null,\"uri\":\"http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d\",\"hash\":{\"mykey\":{\"BB\":10}},\"point\":{\"IsEmpty\":false,\"X\":150,\"Y\":150},\"MyEnum\":[1,10,345],\"MyEnum1\":[1,10,345],\"AA\":5,\"AA1\":[{\"BB\":10},{\"BB\":10}],\"BB\":18446744073709551610,\"YY\":[{\"BB\":10},{\"BB\":10}]}", x, "#A1");
403                         
404                         X n = ser.Deserialize<X> (x);
405                         Assert.AreEqual (s, n, "#A2");
406
407                         //string json = "\\uFF56";
408                         //string result = ser.Deserialize<string> (json);
409                         //Assert.AreEqual ("\uFF56", result);
410
411                         //object oo = ser.DeserializeObject ("{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}");
412                 }
413
414                 [Test]
415                 public void TestDeserializeTypeResolver () 
416                 {
417                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
418                         X x = new X ();
419                         x.Init ();
420
421                         string s = ser.Serialize (x);
422                         Console.WriteLine (s);
423                         Assert.AreEqual ("{\"__type\":\"MonoTests.System.Web.Script.Serialization.JavaScriptSerializerTest+X, System.Web.Extensions_test_net_2_0, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\",\"z\":8,\"ch\":\"v\",\"ch_null\":null,\"str\":\"vwF59g\",\"b\":253,\"sb\":-48,\"sh\":-32740,\"ush\":65511,\"i\":-234235453,\"ui\":4294733061,\"l\":-9223372036854775780,\"ul\":18446744073709551612,\"f\":NaN,\"f1\":-Infinity,\"f2\":Infinity,\"f3\":-3.40282347E+38,\"f4\":3.40282347E+38,\"d\":NaN,\"d1\":-Infinity,\"d2\":Infinity,\"d3\":-1.7976931348623157E+308,\"d4\":1.7976931348623157E+308,\"de\":-1,\"de1\":0,\"de2\":1,\"de3\":-79228162514264337593543950335,\"de4\":79228162514264337593543950335,\"g\":\"000000ea-0002-0162-0102-030405060708\",\"nb\":null,\"dbn\":null,\"uri\":\"http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d\",\"hash\":{\"mykey\":{\"__type\":\"MonoTests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, System.Web.Extensions_test_net_2_0, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\",\"BB\":10}},\"point\":{\"__type\":\"System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\",\"IsEmpty\":false,\"X\":150,\"Y\":150},\"MyEnum\":[1,10,345],\"MyEnum1\":[1,10,345],\"AA\":5,\"AA1\":[{\"__type\":\"MonoTests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, System.Web.Extensions_test_net_2_0, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\",\"BB\":10},{\"__type\":\"MonoTests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, System.Web.Extensions_test_net_2_0, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\",\"BB\":10}],\"BB\":18446744073709551610,\"YY\":[{\"__type\":\"MonoTests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, System.Web.Extensions_test_net_2_0, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\",\"BB\":10},{\"__type\":\"MonoTests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, System.Web.Extensions_test_net_2_0, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\",\"BB\":10}]}", s, "#A1");
424                         
425                         X x2 = ser.Deserialize<X> (s);
426                         Assert.AreEqual (x, x2, "#A2");
427                 }
428
429                 [Test]
430                 public void TestDeserializeBugs () {
431                         JavaScriptSerializer ser = new JavaScriptSerializer ();
432
433                         bug s = new bug ();
434                         s.Init ();
435                         string x = ser.Serialize (s);
436                         bug n = ser.Deserialize<bug> (x);
437                         Assert.AreEqual (s, n);
438
439                         // Should check correctness with .Net GA:
440                         //js = ser.Serialize (Color.Red);
441                         //Color ccc = ser.Deserialize<Color> (js);
442                         //string xml = @"<root><node attr=""xxx""/></root>";
443
444                         //XmlDocument doc = new XmlDocument ();
445                         //doc.LoadXml (xml);
446                         //string js = ser.Serialize (doc);
447                         //DataTable table = new DataTable();
448                         //table.Columns.Add ("col1", typeof (int));
449                         //table.Columns.Add ("col2", typeof (float));
450                         //table.Rows.Add (1, 1f);
451                         //table.Rows.Add (234234, 2.4f);
452
453                         //string js = ser.Serialize (table);
454                 }
455
456                 static void FieldsEqual (object expected, object actual) {
457                         Assert.AreEqual (expected.GetType (), actual.GetType ());
458                         FieldInfo [] infos = expected.GetType ().GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
459                         foreach (FieldInfo info in infos) {
460                                 object value1 = info.GetValue (expected);
461                                 object value2 = info.GetValue (actual);
462                                 if (value1 is IEnumerable) {
463                                         IEnumerator yenum = ((IEnumerable) value2).GetEnumerator ();
464                                         int index = -1;
465                                         foreach (object x in (IEnumerable) value1) {
466                                                 if (!yenum.MoveNext ())
467                                                         Assert.Fail (info.Name + " index:" + index);
468                                                 index++;
469                                                 if (x is DictionaryEntry) {
470                                                         DictionaryEntry entry = (DictionaryEntry)x;
471                                                         IDictionary dict = (IDictionary) value2;
472                                                         Assert.AreEqual (entry.Value, dict [entry.Key], info.Name + ", key:" + entry.Key);
473                                                 }
474                                                 else
475                                                         Assert.AreEqual (x, yenum.Current, info.Name + ", index:" + index);
476                                         }
477                                         Assert.IsFalse (yenum.MoveNext (), info.Name);
478                                         continue;
479                                 }
480                                 Assert.AreEqual (value1, value2, info.Name);
481                         }
482
483                 }
484
485                 [Test]
486                 [ExpectedException (typeof (ArgumentNullException))]
487                 public void TestDeserialize1 () {
488                         JavaScriptSerializer ser = new JavaScriptSerializer ();
489                         ser.Deserialize<string> (null);
490                 }
491
492                 [Test]
493                 [ExpectedException (typeof (ArgumentNullException))]
494                 public void TestDeserializeNullConverter () {
495                         JavaScriptSerializer ser = new JavaScriptSerializer ();
496                         ser.RegisterConverters (null);
497                 }
498
499                 [Test]
500                 public void TestDeserializeConverter () {
501                         JavaScriptSerializer ser = new JavaScriptSerializer ();
502                         List<JavaScriptConverter> list = new List<JavaScriptConverter> ();
503                         list.Add (new MyJavaScriptConverter ());
504                         list.Add (new CultureInfoConverter ());
505                         ser.RegisterConverters (list);
506                         string result = ser.Serialize (new X [] { new X (), new X () });
507                         Assert.AreEqual ("{\"0\":1,\"1\":2}", result);
508                         result = ser.Serialize (Thread.CurrentThread.CurrentCulture);
509                 }
510
511                 [Test]
512                 public void TestDeserializeConverter1 () {
513                         JavaScriptSerializer serializer = new JavaScriptSerializer ();
514
515                         serializer.RegisterConverters (new JavaScriptConverter [] {new ListItemCollectionConverter()});
516
517                         ListBox ListBox1 = new ListBox ();
518                         ListBox1.Items.Add ("a1");
519                         ListBox1.Items.Add ("a2");
520                         ListBox1.Items.Add ("a3");
521
522                         string x = serializer.Serialize (ListBox1.Items);
523                         ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection> (x);
524                         Assert.AreEqual (3, recoveredList.Count);
525                 }
526
527                 [Test]
528                 public void TestSerialize1 () {
529                         JavaScriptSerializer ser = new JavaScriptSerializer ();
530                         Assert.AreEqual("null", ser.Serialize(null));
531
532                         string js = ser.Serialize (1234);
533                         Assert.AreEqual ("1234", js);
534                         Assert.AreEqual (1234, ser.Deserialize<int> (js));
535                         js = ser.Serialize (1.1);
536                         Assert.AreEqual ("1.1", js);
537                         Assert.AreEqual (1.1f, ser.Deserialize<float> (js));
538                         char [] chars = "faskjhfasd0981234".ToCharArray ();
539                         js = ser.Serialize (chars);
540                         char[] actual = ser.Deserialize<char[]> (js);
541                         Assert.AreEqual (chars.Length, actual.Length);
542                         for (int i = 0; i < chars.Length; i++)
543                                 Assert.AreEqual (chars[i], actual[i]);
544
545                         string expected = @"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\""#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~""";
546                         string data = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
547
548                         string serRes = ser.Serialize (data);
549                         Assert.AreEqual (expected, serRes);
550                         string deserRes = ser.Deserialize<string> (serRes);
551                         Assert.AreEqual (data, deserRes);
552                 }
553
554                 [Test]
555                 [ExpectedException (typeof (ArgumentNullException))]
556                 [Category ("NotDotNet")]
557                 public void TestSerialize2 () {
558                         JavaScriptSerializer ser = new JavaScriptSerializer ();
559                         ser.Serialize ("aaa", (StringBuilder)null);
560                 }
561
562                 static readonly long InitialJavaScriptDateTicks = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
563
564                 [Test]
565                 public void TestSerializeDate () {
566                         JavaScriptSerializer ser = new JavaScriptSerializer ();
567                         DateTime now = new DateTime (633213894056010000L);
568
569                         string actual = ser.Serialize (now);
570                         DateTime dateTime = now.ToUniversalTime ();
571                         long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long) 10000;
572
573                         object dd = ser.DeserializeObject (@"""\/Datte(" + javaScriptTicks + @")\/""");
574                         Assert.AreEqual (@"""\/Date(" + javaScriptTicks + @")\/""", actual);
575                         Assert.AreEqual (now.ToUniversalTime(), ser.DeserializeObject (actual));
576                 }
577
578                 [Test]
579                 public void TestSerializeEnum () {
580                         JavaScriptSerializer ser = new JavaScriptSerializer ();
581                         string result = ser.Serialize (MyEnum.BBB);
582                         Assert.AreEqual ("1", result);
583                         Assert.AreEqual (MyEnum.BBB, ser.Deserialize<MyEnum> (result));
584                 }
585
586                 class MyJavaScriptConverter : JavaScriptConverter
587                 {
588                         public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
589                                 throw new Exception ("The method or operation is not implemented.");
590                         }
591
592                         public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
593                                 Array a = (Array) obj;
594                                 Dictionary<string, object> d = new Dictionary<string, object> ();
595                                 d.Add ("0", 1);
596                                 d.Add ("1", 2);
597                                 return d;
598                                 //throw new Exception ("The method or operation is not implemented.");
599                         }
600
601                         public override IEnumerable<Type> SupportedTypes {
602                                 get {
603                                         yield return typeof (X[]);
604                                 }
605                         }
606                 }
607
608                 sealed class CultureInfoConverter : JavaScriptConverter
609                 {
610                         static readonly Type typeofCultureInfo = typeof (CultureInfo);
611                         public override IEnumerable<Type> SupportedTypes {
612                                 get { yield return typeofCultureInfo; }
613                         }
614
615                         public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
616                                 throw new NotSupportedException ();
617                         }
618
619                         public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
620                                 CultureInfo ci = (CultureInfo)obj;
621                                 if (ci == null)
622                                         return null;
623                                 Dictionary<string, object> d = new Dictionary<string, object> ();
624                                 d.Add ("name", ci.Name);
625                                 d.Add ("numberFormat", ci.NumberFormat);
626                                 d.Add ("dateTimeFormat", ci.DateTimeFormat);
627                                 return d;
628                         }
629                 }
630
631                 public class ListItemCollectionConverter : JavaScriptConverter
632                 {
633                         public override IEnumerable<Type> SupportedTypes {
634                                 //Define the ListItemCollection as a supported type.
635                                 get { return new ReadOnlyCollection<Type> (new Type [] { typeof (ListItemCollection) }); }
636                         }
637
638                         public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
639                                 ListItemCollection listType = obj as ListItemCollection;
640
641                                 if (listType != null) {
642                                         // Create the representation.
643                                         Dictionary<string, object> result = new Dictionary<string, object> ();
644                                         ArrayList itemsList = new ArrayList ();
645                                         foreach (ListItem item in listType) {
646                                                 //Add each entry to the dictionary.
647                                                 Dictionary<string, object> listDict = new Dictionary<string, object> ();
648                                                 listDict.Add ("Value", item.Value);
649                                                 listDict.Add ("Text", item.Text);
650                                                 itemsList.Add (listDict);
651                                         }
652                                         result ["List"] = itemsList;
653
654                                         return result;
655                                 }
656                                 return new Dictionary<string, object> ();
657                         }
658
659                         public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
660                                 if (dictionary == null)
661                                         throw new ArgumentNullException ("dictionary");
662
663                                 if (type == typeof (ListItemCollection)) {
664                                         // Create the instance to deserialize into.
665                                         ListItemCollection list = new ListItemCollection ();
666
667                                         // Deserialize the ListItemCollection's items.
668                                         ArrayList itemsList = (ArrayList) dictionary ["List"];
669                                         for (int i = 0; i < itemsList.Count; i++)
670                                                 list.Add (serializer.ConvertToType<ListItem> (itemsList [i]));
671
672                                         return list;
673                                 }
674                                 return null;
675                         }
676                 }
677
678                 [Test]
679                 public void DeserializeObject () {
680                         object o = new JavaScriptSerializer ().DeserializeObject ("{\"Numeric\":0,\"Array\":[true,false,0]}");
681                         Assert.IsNotNull (o as Dictionary<string, object>, "type");
682                         Dictionary<string, object> dictionary = (Dictionary<string, object>) o;
683                         Assert.AreEqual (0, (int) dictionary ["Numeric"], "Numeric");
684                         Assert.IsNotNull (dictionary ["Array"] as object [], "Array type");
685                         object [] array = (object []) dictionary ["Array"];
686                         Assert.AreEqual (true, (bool) array [0], "array [0]");
687                         Assert.AreEqual (false, (bool) array [1], "array [1]");
688                         Assert.AreEqual (0, (int) array [2], "array [2]");
689                 }
690
691                 [Test]
692                 public void DeserializeObject2 () 
693                 {
694                         JavaScriptSerializer ser = new JavaScriptSerializer ();
695                         Y y = new Y ();
696                         string s = ser.Serialize (y);
697                         object y2 = ser.DeserializeObject (s);
698                         Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
699                 }
700
701                 [Test]
702                 public void DeserializeObject3 () 
703                 {
704                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver());
705                         Y y = new Y ();
706                         string s = ser.Serialize (y);
707                         object y2 = ser.DeserializeObject (s);
708                         Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
709                 }
710
711                 [Test]
712                 public void DeserializeObject4 () 
713                 {
714                         JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver());
715                         Y y = new Y ();
716                         string s = ser.Serialize (y);
717                         object y2 = ser.DeserializeObject (s);
718                         Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
719                         Assert.AreEqual (1, CustomResolver.ResolvedIds.Count, "ResolvedIds Count");
720                         Assert.AreEqual ("Y", CustomResolver.ResolvedIds [0], "ResolvedIds.Y");
721                         Assert.AreEqual (1, CustomResolver.ResolvedTypes.Count, "ResolvedTypes Count");
722                         Assert.AreEqual ("Y", CustomResolver.ResolvedTypes [0], "ResolvedTypes.Y");
723                 }
724
725                 [Test]
726                 [ExpectedException(typeof(ArgumentNullException))]
727                 public void SerializeWithResolverDeserializeWithout () 
728                 {
729                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
730                         Y y = new Y ();
731                         string s = ser.Serialize (y);
732                         ser = new JavaScriptSerializer ();
733                         object y2 = ser.DeserializeObject (s);
734                 }
735
736                 [Test]
737                 public void SerializeWithoutResolverDeserializeWith ()
738                 {
739                         JavaScriptSerializer ser = new JavaScriptSerializer ();
740                         Y y = new Y ();
741                         string s = ser.Serialize (y);
742                         ser = new JavaScriptSerializer (new SimpleTypeResolver ());
743                         object y2 = ser.DeserializeObject (s);
744                         Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
745                 }
746
747                 class B
748                 {
749                         public int v1 = 15;
750                         public string s1 = "s1";
751                 }
752
753                 class D : B
754                 {
755                         public int v2 = 16;
756                         public string s2 = "s2";
757                 }
758
759                 class C
760                 {
761                         public B b1 = new B ();
762                         public B b2 = new D ();
763                 }
764
765                 [Test]
766                 public void SerializeDerivedType () 
767                 {
768                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
769                         B b = new D ();
770                         string s = ser.Serialize (b);
771                         B b2 = ser.Deserialize<B> (s);
772                         Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
773                 }
774
775                 [Test]
776                 public void SerializeDerivedType2 () 
777                 {
778                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
779                         B b = new D ();
780                         string s = ser.Serialize (b);
781                         B b2 = (B)ser.DeserializeObject (s);
782                         Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
783                 }
784
785                 [Test]
786                 public void SerializeContainedDerivedType () 
787                 {
788                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
789                         C c = new C ();
790                         string s = ser.Serialize (c);
791                         C c2 = ser.Deserialize<C> (s);
792                         Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
793                         Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
794                 }
795
796                 [Test]
797                 public void SerializeContainedDerivedType2 () 
798                 {
799                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
800                         C c = new C ();
801                         string s = ser.Serialize (c);
802                         C c2 = (C)ser.DeserializeObject (s);
803                         Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
804                         Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
805                 }
806
807                 [Test]
808                 public void SerializeWithTypeConverter () 
809                 {
810                         JavaScriptSerializer ser = new JavaScriptSerializer ();
811                         MyUri uri = new MyUri ("http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d", UriKind.RelativeOrAbsolute);
812                         string s = ser.Serialize (uri);
813                         MyUri uri2 = ser.Deserialize<MyUri> (s);
814                         Assert.AreEqual (uri, uri2);
815                 }
816
817                 [Test]
818                 public void SerializeWithTypeConverter2 () 
819                 {
820                         JavaScriptSerializer ser = new JavaScriptSerializer ();
821                         MyPointContainer pc = new MyPointContainer(new Point(15, 16));
822                         string s = ser.Serialize(pc);
823                         MyPointContainer pc2 = ser.Deserialize<MyPointContainer>(s);
824                 }
825                 
826                 [Test]
827                 public void MaxJsonLengthDeserializeObject () 
828                 {
829                         JavaScriptSerializer ser = new JavaScriptSerializer ();
830                         ser.MaxJsonLength = 16;
831                         object o = ser.DeserializeObject ("{s:'1234567890'}");
832                 }
833
834                 [Test]
835                 [ExpectedException(typeof(ArgumentException))]
836                 public void MaxJsonLengthDeserializeObjectToLong () 
837                 {
838                         JavaScriptSerializer ser = new JavaScriptSerializer ();
839                         ser.MaxJsonLength = 15;
840                         object o = ser.DeserializeObject ("{s:'1234567890'}");
841                 }
842
843                 [Test]
844                 public void MaxJsonLengthSerialize () 
845                 {
846                         JavaScriptSerializer ser = new JavaScriptSerializer ();
847                         ser.MaxJsonLength = 9;
848                         Y y = new Y ();
849                         string s = ser.Serialize (y);
850                 }
851
852                 [Test]
853                 [ExpectedException (typeof (InvalidOperationException))]
854                 public void MaxJsonLengthSerializeToLong () 
855                 {
856                         JavaScriptSerializer ser = new JavaScriptSerializer ();
857                         ser.MaxJsonLength = 8;
858                         Y y = new Y ();
859                         string s = ser.Serialize (y);
860                 }
861
862                 [Test]
863                 public void RecursionLimitDeserialize1 () 
864                 {
865                         JavaScriptSerializer ser = new JavaScriptSerializer ();
866                         ser.RecursionLimit = 3;
867                         YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
868                 }
869
870                 [Test]
871                 public void RecursionLimitDeserialize2 () 
872                 {
873                         JavaScriptSerializer ser = new JavaScriptSerializer ();
874                         ser.RecursionLimit = 2;
875                         YY yy = ser.Deserialize<YY> ("{\"Y1\":{},\"Y2\":{}}");
876                 }
877
878                 [Test]
879                 public void RecursionLimitDeserialize3 () 
880                 {
881                         JavaScriptSerializer ser = new JavaScriptSerializer ();
882                         ser.RecursionLimit = 1;
883                         object o = ser.DeserializeObject ("\"xxx\"");
884                 }
885
886                 [Test]
887                 [ExpectedException(typeof(ArgumentException))]
888                 public void RecursionLimitDeserializeToDeep () 
889                 {
890                         JavaScriptSerializer ser = new JavaScriptSerializer ();
891                         ser.RecursionLimit = 2;
892                         YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
893                 }
894
895                 [Test]
896                 public void RecursionLimitSerialize () 
897                 {
898                         JavaScriptSerializer ser = new JavaScriptSerializer ();
899                         ser.RecursionLimit = 3;
900                         YY yy = new YY();
901                         string s = ser.Serialize (yy);
902                 }
903
904                 [Test]
905                 [ExpectedException(typeof(ArgumentException))]
906                 public void RecursionLimitSerializeToDeep () 
907                 {
908                         JavaScriptSerializer ser = new JavaScriptSerializer ();
909                         ser.RecursionLimit = 2;
910                         YY yy = new YY ();
911                         string s = ser.Serialize (yy);
912                 }
913
914                 [Test]
915                 public void RecursionLimitSerialize2 () 
916                 {
917                         JavaScriptSerializer ser = new JavaScriptSerializer ();
918                         ser.RecursionLimit = 2;
919                         YY yy = new YY ();
920                         StringBuilder b = new StringBuilder ();
921                         bool caughtException = false;
922                         try {
923                                 ser.Serialize (yy, b);
924                         }
925                         catch {
926                                 caughtException = true;
927                         }
928                         Assert.IsTrue (caughtException, "RecursionLimitSerialize2 Expected an exception!");
929                         Assert.AreEqual ("{\"Y1\":{\"BB\":", b.ToString (), "RecursionLimitSerialize2");
930                 }
931
932                 [Test]
933                 public void SimpleTypeResolver () 
934                 {
935                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
936                         YY yy = new YY ();
937                         string s = ser.Serialize (yy);
938                         string expected = String.Format("\"__type\":\"{0}\"", yy.GetType().AssemblyQualifiedName);
939
940                         Assert.IsTrue (s.Contains (expected), "YY: expected {0} to contain {1}", s, expected);
941                         
942                         expected = String.Format ("\"__type\":\"{0}\"", yy.Y1.GetType ().AssemblyQualifiedName);
943                         Assert.IsTrue (s.Contains (expected), "Y: expected {0} to contain {1}", s, expected);
944                 }
945
946                 public class CustomResolver : JavaScriptTypeResolver
947                 {
948                         public CustomResolver () 
949                         {
950                                 Reset ();
951                         }
952
953                         public override Type ResolveType (string id) 
954                         {
955                                 ResolvedIds.Add (id);
956
957                                 switch (id) {
958                                 case "YY":
959                                         return typeof(YY);
960
961                                 case "Y":
962                                         return typeof (Y);
963
964                                 case "X":
965                                         return typeof (X);
966
967                                 case "int":
968                                         return typeof (int);
969
970                                 case "long":
971                                         return typeof (long);
972
973                                 case "string":
974                                         return typeof (string);
975
976                                 case "point":
977                                         return typeof(Point);
978                                 }
979                                 return null;
980                         }
981
982                         public override string ResolveTypeId (Type type) 
983                         {
984                                 if (type == null) {
985                                         throw new ArgumentNullException ("type");
986                                 }
987
988                                 ResolvedTypes.Add (type.Name);
989
990                                 if (type == typeof (YY))
991                                         return "YY";
992
993                                 if (type == typeof (Y))
994                                         return "Y";
995
996                                 if (type == typeof (X))
997                                         return "X";
998
999                                 if (type == typeof (int))
1000                                         return "int";
1001
1002                                 if (type == typeof (long))
1003                                         return "long";
1004
1005                                 if (type == typeof (string))
1006                                         return "string";
1007
1008                                 if (type == typeof(Point))
1009                                         return "point";
1010
1011                                 return null;
1012                         }
1013
1014                         public static List<string> ResolvedTypes {
1015                                 get {
1016                                         if (resolvedTypes == null) {
1017                                                 resolvedTypes = new List<string> ();
1018                                         }
1019                                         return resolvedTypes;
1020                                 }
1021                         }
1022
1023                         public static List<string> ResolvedIds {
1024                                 get {
1025                                         if (resolvedIds == null) {
1026                                                 resolvedIds = new List<string> ();
1027                                         }
1028                                         return resolvedIds;
1029                                 }
1030                         }
1031
1032                         public static void Reset () 
1033                         {
1034                                 resolvedIds = null;
1035                                 resolvedTypes = null;
1036                         }
1037
1038                         private static List<string> resolvedTypes;
1039                         private static List<string> resolvedIds;
1040                 }
1041
1042                 [Test]
1043                 [NUnit.Framework.Category ("NotWorking")]
1044                 public void CustomTypeResolver ()
1045                 {
1046                         JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver ());
1047                         X x = new X ();
1048                         x.Init ();
1049
1050                         string s = ser.Serialize (x);
1051
1052                         CustomResolver.Reset ();
1053                         X x1 = (X) ser.DeserializeObject (s);
1054                         Assert.IsTrue (x.Equals (x1), "x != x1");
1055
1056                         CustomResolver.Reset ();
1057                         X x2 = ser.Deserialize<X> (s);
1058                         Assert.IsTrue (x.Equals (x2), "x != x2");
1059                 }
1060
1061                 [Test]
1062                 public void InfinityAndNaN ()
1063                 {
1064                         JavaScriptSerializer ser = new JavaScriptSerializer ();
1065
1066                         double nan = Double.NaN;
1067                         string s = ser.Serialize (nan);
1068                         Assert.AreEqual (s, "NaN", "#A1");
1069                         nan = (double)ser.DeserializeObject (s);
1070                         Assert.AreEqual (Double.NaN, nan, "#A2");
1071                         nan = (double)ser.Deserialize <double> (s);
1072                         Assert.AreEqual (Double.NaN, nan, "#A3");
1073                         
1074                         double infinity = Double.PositiveInfinity;
1075                         s = ser.Serialize (infinity);
1076                         Assert.AreEqual (s, "Infinity", "#B1");
1077                         infinity = (double)ser.DeserializeObject (s);
1078                         Assert.AreEqual (Double.PositiveInfinity, infinity, "#B2");
1079                         infinity = ser.Deserialize <double> (s);
1080                         Assert.AreEqual (Double.PositiveInfinity, infinity, "#B3");
1081                         
1082                         infinity = Double.NegativeInfinity;
1083                         s = ser.Serialize (infinity);
1084                         Assert.AreEqual (s, "-Infinity", "#C1");
1085                         infinity = (double)ser.DeserializeObject (s);
1086                         Assert.AreEqual (Double.NegativeInfinity, infinity, "#C2");
1087                         infinity = ser.Deserialize <double> (s);
1088                         Assert.AreEqual (Double.NegativeInfinity, infinity, "#C3");
1089
1090                         var dict = new Dictionary <string, object> ();
1091                         dict.Add ("A", Double.NaN);
1092                         dict.Add ("B", Double.PositiveInfinity);
1093                         dict.Add ("C", Double.NegativeInfinity);
1094                         s = ser.Serialize (dict);
1095                         Assert.AreEqual ("{\"A\":NaN,\"B\":Infinity,\"C\":-Infinity}", s, "#D1");
1096                         
1097                         dict = (Dictionary <string, object>)ser.DeserializeObject (s);
1098                         Assert.AreEqual (Double.NaN, dict ["A"], "#D2");
1099                         Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D3");
1100                         Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D4");
1101
1102                         dict = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object>> (s);
1103                         Assert.AreEqual (Double.NaN, dict ["A"], "#D5");
1104                         Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D6");
1105                         Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D7");
1106
1107                         var arr = new ArrayList () {
1108                                         Double.NaN,
1109                                         Double.PositiveInfinity,
1110                                         Double.NegativeInfinity};
1111                         s = ser.Serialize (arr);
1112                         Assert.AreEqual ("[NaN,Infinity,-Infinity]", s, "#E1");
1113
1114                         object[] arr2 = (object[])ser.DeserializeObject (s);
1115                         Assert.AreEqual (3, arr2.Length, "#E2");
1116                         Assert.AreEqual (Double.NaN, arr2 [0], "#E3");
1117                         Assert.AreEqual (Double.PositiveInfinity, arr2 [1], "#E4");
1118                         Assert.AreEqual (Double.NegativeInfinity, arr2 [2], "#E5");
1119
1120                         arr = ser.Deserialize <ArrayList> (s);
1121                         Assert.AreEqual (3, arr.Count, "#E6");
1122                         Assert.AreEqual (Double.NaN, arr [0], "#E7");
1123                         Assert.AreEqual (Double.PositiveInfinity, arr [1], "#E8");
1124                         Assert.AreEqual (Double.NegativeInfinity, arr [2], "#E9");
1125                 }
1126
1127                 [Test]
1128                 public void StandalonePrimitives ()
1129                 {
1130                         JavaScriptSerializer ser = new JavaScriptSerializer ();
1131
1132                         object o;
1133                         int i;
1134                         
1135                         o = ser.DeserializeObject ("1");
1136                         Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A1");
1137                         i = (int)o;
1138                         Assert.AreEqual (1, i, "#A2");
1139                         o =ser.DeserializeObject ("-1");
1140                         Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A3");
1141                         i = (int)o;
1142                         Assert.AreEqual (-1, i, "#A4");
1143                         
1144                         o = ser.DeserializeObject ("2147483649");
1145                         Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B1");
1146                         long l = (long)o;
1147                         Assert.AreEqual (2147483649, l, "#B2");
1148                         o = ser.DeserializeObject ("-2147483649");
1149                         Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B3");
1150                         l = (long)o;
1151                         Assert.AreEqual (-2147483649, l, "#B4");
1152
1153                         o = ser.DeserializeObject ("9223372036854775808");
1154                         Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C1");
1155                         decimal d = (decimal)o;
1156                         Assert.AreEqual (9223372036854775808m, d, "#C2");
1157                         o = ser.DeserializeObject ("-9223372036854775809");
1158                         Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C3");
1159                         d = (decimal)o;
1160                         Assert.AreEqual (-9223372036854775809m, d, "#C4");
1161
1162                         o = ser.DeserializeObject ("79228162514264337593543950336");
1163                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D1");
1164                         double db = (double)o;
1165                         Assert.AreEqual (79228162514264337593543950336.0, db, "#D2");
1166                         o = ser.DeserializeObject ("-79228162514264337593543950336");
1167                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D3");
1168                         db = (double)o;
1169                         Assert.AreEqual (-79228162514264337593543950336.0, db, "#D4");
1170                         
1171                         o = ser.DeserializeObject ("\"test string\"");
1172                         Assert.AreEqual (typeof (global::System.String), o.GetType (), "#E1");
1173                         string s = (string)o;
1174                         Assert.AreEqual ("test string", s, "#E2");
1175
1176                         o = ser.DeserializeObject ("true");
1177                         Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F1");
1178                         bool b = (bool)o;
1179                         Assert.AreEqual (true, b, "#F2");
1180
1181                         o = ser.DeserializeObject ("false");
1182                         Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F3");
1183                         b = (bool)o;
1184                         Assert.AreEqual (false, b, "#F4");
1185
1186                         o = ser.DeserializeObject ("-1.7976931348623157E+308");
1187                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G1");
1188                         db = (double)o;
1189                         Assert.AreEqual (Double.MinValue, db, "#G2");
1190
1191                         o = ser.DeserializeObject ("1.7976931348623157E+308");
1192                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G3");
1193                         db = (double)o;
1194                         Assert.AreEqual (Double.MaxValue, db, "#G4");
1195                 }
1196
1197                 class SomeDict : IDictionary<string, object>
1198                 {
1199                         void IDictionary<string, object>.Add (string key, object value) {
1200                                 throw new NotSupportedException ();
1201                         }
1202
1203                         bool IDictionary<string, object>.ContainsKey (string key) {
1204                                 throw new NotSupportedException ();
1205                         }
1206
1207                         ICollection<string> IDictionary<string, object>.Keys {
1208                                 get { throw new NotSupportedException (); }
1209                         }
1210
1211                         bool IDictionary<string, object>.Remove (string key) {
1212                                 throw new NotSupportedException ();
1213                         }
1214
1215                         bool IDictionary<string, object>.TryGetValue (string key, out object value) {
1216                                 throw new NotSupportedException ();
1217                         }
1218
1219                         ICollection<object> IDictionary<string, object>.Values {
1220                                 get { throw new NotSupportedException (); }
1221                         }
1222
1223                         object IDictionary<string, object>.this [string key] {
1224                                 get { throw new NotSupportedException (); }
1225                                 set { throw new NotSupportedException (); }
1226                         }
1227
1228                         void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item) {
1229                                 throw new NotSupportedException ();
1230                         }
1231
1232                         void ICollection<KeyValuePair<string, object>>.Clear () {
1233                                 throw new NotSupportedException ();
1234                         }
1235
1236                         bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item) {
1237                                 throw new NotSupportedException ();
1238                         }
1239
1240                         void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex) {
1241                                 throw new NotSupportedException ();
1242                         }
1243
1244                         int ICollection<KeyValuePair<string, object>>.Count {
1245                                 get { throw new NotSupportedException (); }
1246                         }
1247
1248                         bool ICollection<KeyValuePair<string, object>>.IsReadOnly {
1249                                 get { throw new NotSupportedException (); }
1250                         }
1251
1252                         bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item) {
1253                                 throw new NotSupportedException ();
1254                         }
1255
1256                         IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator () {
1257                                 return GetEnumerator ();
1258                         }
1259
1260                         IEnumerator IEnumerable.GetEnumerator () {
1261                                 return ((IEnumerable<KeyValuePair<string, object>>) this).GetEnumerator ();
1262                         }
1263
1264                         protected IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
1265                                 yield return new KeyValuePair<string, object> ("hello", "world");
1266                         }
1267                 }
1268
1269                 [Test] //bug #424704
1270                 public void NonGenericClassImplementingClosedGenericIDictionary ()
1271                 {
1272                         JavaScriptSerializer ser = new JavaScriptSerializer ();
1273
1274                         SomeDict dictIn = new SomeDict ();
1275
1276                         string s = ser.Serialize (dictIn);
1277
1278                         Dictionary<string, object> dictOut = ser.Deserialize<Dictionary<string, object>> (s);
1279                         Assert.AreEqual (dictOut.Count, 1, "#1");
1280                         Assert.AreEqual (dictOut["hello"], "world", "#2");
1281                 }
1282
1283                 [Test]
1284                 public void ConvertToIDictionary ()
1285                 {
1286                         JavaScriptSerializer jss = new JavaScriptSerializer ();
1287                         string json = "{\"node\":{\"Text\":\"Root Node\",\"Value\":null,\"ExpandMode\":3,\"NavigateUrl\":null,\"PostBack\":true,\"DisabledCssClass\":null,\"SelectedCssClass\":null,\"HoveredCssClass\":null,\"ImageUrl\":null,\"HoveredImageUrl\":null,\"DisabledImageUrl\":null,\"ExpandedImageUrl\":null,\"ContextMenuID\":\"\"},\"context\":{\"NumberOfNodes\":1000}}";
1288                         object input = jss.Deserialize<IDictionary>(json);
1289                         IDictionary o = jss.ConvertToType<IDictionary>(input);
1290
1291                         Assert.IsTrue (o != null, "#A1");
1292                         Assert.AreEqual (typeof (Dictionary <string, object>), o.GetType (), "#A2");
1293                 }
1294
1295                 [Test]
1296                 public void ConvertToGenericIDictionary ()
1297                 {
1298                         JavaScriptSerializer jss = new JavaScriptSerializer ();
1299                         string json = "{\"node\":{\"Text\":\"Root Node\",\"Value\":null,\"ExpandMode\":3,\"NavigateUrl\":null,\"PostBack\":true,\"DisabledCssClass\":null,\"SelectedCssClass\":null,\"HoveredCssClass\":null,\"ImageUrl\":null,\"HoveredImageUrl\":null,\"DisabledImageUrl\":null,\"ExpandedImageUrl\":null,\"ContextMenuID\":\"\"},\"context\":{\"NumberOfNodes\":1000}}";
1300
1301                         object input = jss.Deserialize<IDictionary>(json);
1302                         
1303                         IDictionary <string, object> o = jss.ConvertToType<IDictionary <string, object>>(input);
1304                         Assert.IsTrue (o != null, "#A1");
1305                         Assert.AreEqual (typeof (Dictionary <string, object>), o.GetType (), "#A2");
1306
1307                         IDictionary <object, object> o1 = jss.ConvertToType<IDictionary <object, object>>(input);
1308                         Assert.IsTrue (o1 != null, "#B1");
1309                         Assert.AreEqual (typeof (Dictionary <object, object>), o1.GetType (), "#B2");
1310                 }
1311
1312                 [Test]
1313                 [ExpectedException (typeof (InvalidOperationException))]
1314                 public void ConvertToGenericIDictionary_InvalidDefinition ()
1315                 {
1316                         JavaScriptSerializer jss = new JavaScriptSerializer ();
1317                         string json = "{\"node\":{\"Text\":\"Root Node\",\"Value\":null,\"ExpandMode\":3,\"NavigateUrl\":null,\"PostBack\":true,\"DisabledCssClass\":null,\"SelectedCssClass\":null,\"HoveredCssClass\":null,\"ImageUrl\":null,\"HoveredImageUrl\":null,\"DisabledImageUrl\":null,\"ExpandedImageUrl\":null,\"ContextMenuID\":\"\"},\"context\":{\"NumberOfNodes\":1000}}";
1318
1319                         object input = jss.Deserialize<IDictionary>(json);
1320                         IDictionary <int, object> o = jss.ConvertToType<IDictionary <int, object>>(input);
1321                 }
1322         }
1323 }