copied mono-api-diff.cs from mono-2-2 branch so new patch can be applied and history...
[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 #if NET_3_5
351                         Assert.AreEqual (2097152, ser.MaxJsonLength);
352 #else
353                         Assert.AreEqual (102400, ser.MaxJsonLength);
354 #endif
355                         Assert.AreEqual (100, ser.RecursionLimit);
356                         //List<JavaScriptConverter> l = new List<JavaScriptConverter> ();
357                         //l.Add (new MyJavaScriptConverter ());
358                         //ser.RegisterConverters (l);
359                         //string x = ser.Serialize (new X [] { new X (), new X () });
360                         //string s = ser.Serialize (new X());
361                         //"{\"BB\":10,\"__type\":\"Tests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null\"}"
362                         //X x = ser.Deserialize<X> (s);
363                         //object ddd = typeof (Y).GetMember ("BB");
364                         //object x1 = ser.Deserialize<X []> (null);
365                         //object x2 = ser.Deserialize<X []> ("");
366                         //object d = ser.Deserialize<X[]> (x);
367                 }
368
369                 [Test]
370                 public void TestDeserializeUnquotedKeys ()
371                 {
372                         JavaScriptSerializer ser = new JavaScriptSerializer ();
373                         IDictionary dict = ser.Deserialize <Dictionary <string, object>> ("{itemOne:\"1\",itemTwo:\"2\"}");
374
375                         Assert.AreEqual ("1", dict ["itemOne"], "#A1");
376                         Assert.AreEqual ("2", dict ["itemTwo"], "#A2");
377
378                         dict = ser.Deserialize <Dictionary <string, object>> ("{itemOne:1,itemTwo:2}");
379                         Assert.AreEqual (1, dict ["itemOne"], "#B1");
380                         Assert.AreEqual (2, dict ["itemTwo"], "#B2");
381                 }
382
383                 [Test]
384                 public void TestDeserializeUnquotedKeysWithSpaces ()
385                 {
386                         JavaScriptSerializer ser = new JavaScriptSerializer ();
387                         IDictionary dict = ser.Deserialize <Dictionary <string, object>> ("{ itemOne :\"1\",itemTwo:\"2\"}");
388
389                         Assert.AreEqual ("1", dict ["itemOne"], "#A1");
390                         Assert.AreEqual ("2", dict ["itemTwo"], "#A2");
391
392                         dict = ser.Deserialize <Dictionary <string, object>> ("{   itemOne   :1,   itemTwo   :2}");
393                         Assert.AreEqual (1, dict ["itemOne"], "#B1");
394                         Assert.AreEqual (2, dict ["itemTwo"], "#B2");
395                 }
396                 
397                 [Test]
398                 public void TestDeserialize () {
399                         JavaScriptSerializer ser = new JavaScriptSerializer ();
400                         Assert.IsNull (ser.Deserialize<X> (""));
401
402                         X s = new X ();
403                         s.Init ();
404                         string x = ser.Serialize (s);
405
406                         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");
407                         
408                         X n = ser.Deserialize<X> (x);
409                         Assert.AreEqual (s, n, "#A2");
410
411                         //string json = "\\uFF56";
412                         //string result = ser.Deserialize<string> (json);
413                         //Assert.AreEqual ("\uFF56", result);
414
415                         //object oo = ser.DeserializeObject ("{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}");
416                 }
417
418                 [Test]
419                 public void TestDeserializeTypeResolver () 
420                 {
421                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
422                         X x = new X ();
423                         x.Init ();
424
425                         string s = ser.Serialize (x);
426                         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");
427                         
428                         X x2 = ser.Deserialize<X> (s);
429                         Assert.AreEqual (x, x2, "#A2");
430                 }
431
432                 [Test]
433                 public void TestDeserializeBugs () {
434                         JavaScriptSerializer ser = new JavaScriptSerializer ();
435
436                         bug s = new bug ();
437                         s.Init ();
438                         string x = ser.Serialize (s);
439                         bug n = ser.Deserialize<bug> (x);
440                         Assert.AreEqual (s, n);
441
442                         // Should check correctness with .Net GA:
443                         //js = ser.Serialize (Color.Red);
444                         //Color ccc = ser.Deserialize<Color> (js);
445                         //string xml = @"<root><node attr=""xxx""/></root>";
446
447                         //XmlDocument doc = new XmlDocument ();
448                         //doc.LoadXml (xml);
449                         //string js = ser.Serialize (doc);
450                         //DataTable table = new DataTable();
451                         //table.Columns.Add ("col1", typeof (int));
452                         //table.Columns.Add ("col2", typeof (float));
453                         //table.Rows.Add (1, 1f);
454                         //table.Rows.Add (234234, 2.4f);
455
456                         //string js = ser.Serialize (table);
457                 }
458
459                 static void FieldsEqual (object expected, object actual) {
460                         Assert.AreEqual (expected.GetType (), actual.GetType ());
461                         FieldInfo [] infos = expected.GetType ().GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
462                         foreach (FieldInfo info in infos) {
463                                 object value1 = info.GetValue (expected);
464                                 object value2 = info.GetValue (actual);
465                                 if (value1 is IEnumerable) {
466                                         IEnumerator yenum = ((IEnumerable) value2).GetEnumerator ();
467                                         int index = -1;
468                                         foreach (object x in (IEnumerable) value1) {
469                                                 if (!yenum.MoveNext ())
470                                                         Assert.Fail (info.Name + " index:" + index);
471                                                 index++;
472                                                 if (x is DictionaryEntry) {
473                                                         DictionaryEntry entry = (DictionaryEntry)x;
474                                                         IDictionary dict = (IDictionary) value2;
475                                                         Assert.AreEqual (entry.Value, dict [entry.Key], info.Name + ", key:" + entry.Key);
476                                                 }
477                                                 else
478                                                         Assert.AreEqual (x, yenum.Current, info.Name + ", index:" + index);
479                                         }
480                                         Assert.IsFalse (yenum.MoveNext (), info.Name);
481                                         continue;
482                                 }
483                                 Assert.AreEqual (value1, value2, info.Name);
484                         }
485
486                 }
487
488                 [Test]
489                 [ExpectedException (typeof (ArgumentNullException))]
490                 public void TestDeserialize1 () {
491                         JavaScriptSerializer ser = new JavaScriptSerializer ();
492                         ser.Deserialize<string> (null);
493                 }
494
495                 [Test]
496                 [ExpectedException (typeof (ArgumentNullException))]
497                 public void TestDeserializeNullConverter () {
498                         JavaScriptSerializer ser = new JavaScriptSerializer ();
499                         ser.RegisterConverters (null);
500                 }
501
502                 [Test]
503                 public void TestDeserializeConverter () {
504                         JavaScriptSerializer ser = new JavaScriptSerializer ();
505                         List<JavaScriptConverter> list = new List<JavaScriptConverter> ();
506                         list.Add (new MyJavaScriptConverter ());
507                         list.Add (new CultureInfoConverter ());
508                         ser.RegisterConverters (list);
509                         string result = ser.Serialize (new X [] { new X (), new X () });
510                         Assert.AreEqual ("{\"0\":1,\"1\":2}", result);
511                         result = ser.Serialize (Thread.CurrentThread.CurrentCulture);
512                 }
513
514                 [Test]
515                 public void TestDeserializeConverter1 () {
516                         JavaScriptSerializer serializer = new JavaScriptSerializer ();
517
518                         serializer.RegisterConverters (new JavaScriptConverter [] {new ListItemCollectionConverter()});
519
520                         ListBox ListBox1 = new ListBox ();
521                         ListBox1.Items.Add ("a1");
522                         ListBox1.Items.Add ("a2");
523                         ListBox1.Items.Add ("a3");
524
525                         string x = serializer.Serialize (ListBox1.Items);
526                         ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection> (x);
527                         Assert.AreEqual (3, recoveredList.Count);
528                 }
529
530                 [Test]
531                 public void TestSerialize1 () {
532                         JavaScriptSerializer ser = new JavaScriptSerializer ();
533                         Assert.AreEqual("null", ser.Serialize(null));
534
535                         string js = ser.Serialize (1234);
536                         Assert.AreEqual ("1234", js);
537                         Assert.AreEqual (1234, ser.Deserialize<int> (js));
538                         js = ser.Serialize (1.1);
539                         Assert.AreEqual ("1.1", js);
540                         Assert.AreEqual (1.1f, ser.Deserialize<float> (js));
541                         char [] chars = "faskjhfasd0981234".ToCharArray ();
542                         js = ser.Serialize (chars);
543                         char[] actual = ser.Deserialize<char[]> (js);
544                         Assert.AreEqual (chars.Length, actual.Length);
545                         for (int i = 0; i < chars.Length; i++)
546                                 Assert.AreEqual (chars[i], actual[i]);
547
548                         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{|}~""";
549                         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{|}~";
550
551                         string serRes = ser.Serialize (data);
552                         Assert.AreEqual (expected, serRes);
553                         string deserRes = ser.Deserialize<string> (serRes);
554                         Assert.AreEqual (data, deserRes);
555                 }
556
557                 [Test]
558                 [ExpectedException (typeof (ArgumentNullException))]
559                 [Category ("NotDotNet")]
560                 public void TestSerialize2 () {
561                         JavaScriptSerializer ser = new JavaScriptSerializer ();
562                         ser.Serialize ("aaa", (StringBuilder)null);
563                 }
564
565                 static readonly long InitialJavaScriptDateTicks = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
566
567                 [Test]
568                 public void TestSerializeDate () {
569                         JavaScriptSerializer ser = new JavaScriptSerializer ();
570                         DateTime now = new DateTime (633213894056010000L);
571
572                         string actual = ser.Serialize (now);
573                         DateTime dateTime = now.ToUniversalTime ();
574                         long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long) 10000;
575
576                         object dd = ser.DeserializeObject (@"""\/Datte(" + javaScriptTicks + @")\/""");
577                         Assert.AreEqual (@"""\/Date(" + javaScriptTicks + @")\/""", actual);
578                         Assert.AreEqual (now.ToUniversalTime(), ser.DeserializeObject (actual));
579                 }
580
581                 [Test]
582                 public void TestSerializeEnum () {
583                         JavaScriptSerializer ser = new JavaScriptSerializer ();
584                         string result = ser.Serialize (MyEnum.BBB);
585                         Assert.AreEqual ("1", result);
586                         Assert.AreEqual (MyEnum.BBB, ser.Deserialize<MyEnum> (result));
587                 }
588
589                 class MyJavaScriptConverter : JavaScriptConverter
590                 {
591                         public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
592                                 throw new Exception ("The method or operation is not implemented.");
593                         }
594
595                         public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
596                                 Array a = (Array) obj;
597                                 Dictionary<string, object> d = new Dictionary<string, object> ();
598                                 d.Add ("0", 1);
599                                 d.Add ("1", 2);
600                                 return d;
601                                 //throw new Exception ("The method or operation is not implemented.");
602                         }
603
604                         public override IEnumerable<Type> SupportedTypes {
605                                 get {
606                                         yield return typeof (X[]);
607                                 }
608                         }
609                 }
610
611                 sealed class CultureInfoConverter : JavaScriptConverter
612                 {
613                         static readonly Type typeofCultureInfo = typeof (CultureInfo);
614                         public override IEnumerable<Type> SupportedTypes {
615                                 get { yield return typeofCultureInfo; }
616                         }
617
618                         public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
619                                 throw new NotSupportedException ();
620                         }
621
622                         public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
623                                 CultureInfo ci = (CultureInfo)obj;
624                                 if (ci == null)
625                                         return null;
626                                 Dictionary<string, object> d = new Dictionary<string, object> ();
627                                 d.Add ("name", ci.Name);
628                                 d.Add ("numberFormat", ci.NumberFormat);
629                                 d.Add ("dateTimeFormat", ci.DateTimeFormat);
630                                 return d;
631                         }
632                 }
633
634                 public class ListItemCollectionConverter : JavaScriptConverter
635                 {
636                         public override IEnumerable<Type> SupportedTypes {
637                                 //Define the ListItemCollection as a supported type.
638                                 get { return new ReadOnlyCollection<Type> (new Type [] { typeof (ListItemCollection) }); }
639                         }
640
641                         public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
642                                 ListItemCollection listType = obj as ListItemCollection;
643
644                                 if (listType != null) {
645                                         // Create the representation.
646                                         Dictionary<string, object> result = new Dictionary<string, object> ();
647                                         ArrayList itemsList = new ArrayList ();
648                                         foreach (ListItem item in listType) {
649                                                 //Add each entry to the dictionary.
650                                                 Dictionary<string, object> listDict = new Dictionary<string, object> ();
651                                                 listDict.Add ("Value", item.Value);
652                                                 listDict.Add ("Text", item.Text);
653                                                 itemsList.Add (listDict);
654                                         }
655                                         result ["List"] = itemsList;
656
657                                         return result;
658                                 }
659                                 return new Dictionary<string, object> ();
660                         }
661
662                         public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
663                                 if (dictionary == null)
664                                         throw new ArgumentNullException ("dictionary");
665
666                                 if (type == typeof (ListItemCollection)) {
667                                         // Create the instance to deserialize into.
668                                         ListItemCollection list = new ListItemCollection ();
669
670                                         // Deserialize the ListItemCollection's items.
671                                         ArrayList itemsList = (ArrayList) dictionary ["List"];
672                                         for (int i = 0; i < itemsList.Count; i++)
673                                                 list.Add (serializer.ConvertToType<ListItem> (itemsList [i]));
674
675                                         return list;
676                                 }
677                                 return null;
678                         }
679                 }
680
681                 [Test]
682                 public void DeserializeObject () {
683                         object o = new JavaScriptSerializer ().DeserializeObject ("{\"Numeric\":0,\"Array\":[true,false,0]}");
684                         Assert.IsNotNull (o as Dictionary<string, object>, "type");
685                         Dictionary<string, object> dictionary = (Dictionary<string, object>) o;
686                         Assert.AreEqual (0, (int) dictionary ["Numeric"], "Numeric");
687                         Assert.IsNotNull (dictionary ["Array"] as object [], "Array type");
688                         object [] array = (object []) dictionary ["Array"];
689                         Assert.AreEqual (true, (bool) array [0], "array [0]");
690                         Assert.AreEqual (false, (bool) array [1], "array [1]");
691                         Assert.AreEqual (0, (int) array [2], "array [2]");
692                 }
693
694                 [Test]
695                 public void DeserializeObject2 () 
696                 {
697                         JavaScriptSerializer ser = new JavaScriptSerializer ();
698                         Y y = new Y ();
699                         string s = ser.Serialize (y);
700                         object y2 = ser.DeserializeObject (s);
701                         Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
702                 }
703
704                 [Test]
705                 public void DeserializeObject3 () 
706                 {
707                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver());
708                         Y y = new Y ();
709                         string s = ser.Serialize (y);
710                         object y2 = ser.DeserializeObject (s);
711                         Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
712                 }
713
714                 [Test]
715                 public void DeserializeObject4 () 
716                 {
717                         JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver());
718                         Y y = new Y ();
719                         string s = ser.Serialize (y);
720                         object y2 = ser.DeserializeObject (s);
721                         Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
722                         Assert.AreEqual (1, CustomResolver.ResolvedIds.Count, "ResolvedIds Count");
723                         Assert.AreEqual ("Y", CustomResolver.ResolvedIds [0], "ResolvedIds.Y");
724                         Assert.AreEqual (1, CustomResolver.ResolvedTypes.Count, "ResolvedTypes Count");
725                         Assert.AreEqual ("Y", CustomResolver.ResolvedTypes [0], "ResolvedTypes.Y");
726                 }
727
728                 [Test]
729                 [ExpectedException(typeof(ArgumentNullException))]
730                 public void SerializeWithResolverDeserializeWithout () 
731                 {
732                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
733                         Y y = new Y ();
734                         string s = ser.Serialize (y);
735                         ser = new JavaScriptSerializer ();
736                         object y2 = ser.DeserializeObject (s);
737                 }
738
739                 [Test]
740                 public void SerializeWithoutResolverDeserializeWith ()
741                 {
742                         JavaScriptSerializer ser = new JavaScriptSerializer ();
743                         Y y = new Y ();
744                         string s = ser.Serialize (y);
745                         ser = new JavaScriptSerializer (new SimpleTypeResolver ());
746                         object y2 = ser.DeserializeObject (s);
747                         Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
748                 }
749
750                 class B
751                 {
752                         public int v1 = 15;
753                         public string s1 = "s1";
754                 }
755
756                 class D : B
757                 {
758                         public int v2 = 16;
759                         public string s2 = "s2";
760                 }
761
762                 class C
763                 {
764                         public B b1 = new B ();
765                         public B b2 = new D ();
766                 }
767
768                 [Test]
769                 public void SerializeDerivedType () 
770                 {
771                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
772                         B b = new D ();
773                         string s = ser.Serialize (b);
774                         B b2 = ser.Deserialize<B> (s);
775                         Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
776                 }
777
778                 [Test]
779                 public void SerializeDerivedType2 () 
780                 {
781                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
782                         B b = new D ();
783                         string s = ser.Serialize (b);
784                         B b2 = (B)ser.DeserializeObject (s);
785                         Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
786                 }
787
788                 [Test]
789                 public void SerializeContainedDerivedType () 
790                 {
791                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
792                         C c = new C ();
793                         string s = ser.Serialize (c);
794                         C c2 = ser.Deserialize<C> (s);
795                         Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
796                         Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
797                 }
798
799                 [Test]
800                 public void SerializeContainedDerivedType2 () 
801                 {
802                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
803                         C c = new C ();
804                         string s = ser.Serialize (c);
805                         C c2 = (C)ser.DeserializeObject (s);
806                         Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
807                         Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
808                 }
809
810                 [Test]
811                 public void SerializeWithTypeConverter () 
812                 {
813                         JavaScriptSerializer ser = new JavaScriptSerializer ();
814                         MyUri uri = new MyUri ("http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d", UriKind.RelativeOrAbsolute);
815                         string s = ser.Serialize (uri);
816                         MyUri uri2 = ser.Deserialize<MyUri> (s);
817                         Assert.AreEqual (uri, uri2);
818                 }
819
820                 [Test]
821                 public void SerializeWithTypeConverter2 () 
822                 {
823                         JavaScriptSerializer ser = new JavaScriptSerializer ();
824                         MyPointContainer pc = new MyPointContainer(new Point(15, 16));
825                         string s = ser.Serialize(pc);
826                         MyPointContainer pc2 = ser.Deserialize<MyPointContainer>(s);
827                 }
828                 
829                 [Test]
830                 public void MaxJsonLengthDeserializeObject () 
831                 {
832                         JavaScriptSerializer ser = new JavaScriptSerializer ();
833                         ser.MaxJsonLength = 16;
834                         object o = ser.DeserializeObject ("{s:'1234567890'}");
835                 }
836
837                 [Test]
838                 [ExpectedException(typeof(ArgumentException))]
839                 public void MaxJsonLengthDeserializeObjectToLong () 
840                 {
841                         JavaScriptSerializer ser = new JavaScriptSerializer ();
842                         ser.MaxJsonLength = 15;
843                         object o = ser.DeserializeObject ("{s:'1234567890'}");
844                 }
845
846                 [Test]
847                 public void MaxJsonLengthSerialize () 
848                 {
849                         JavaScriptSerializer ser = new JavaScriptSerializer ();
850                         ser.MaxJsonLength = 9;
851                         Y y = new Y ();
852                         string s = ser.Serialize (y);
853                 }
854
855                 [Test]
856                 [ExpectedException (typeof (InvalidOperationException))]
857                 public void MaxJsonLengthSerializeToLong () 
858                 {
859                         JavaScriptSerializer ser = new JavaScriptSerializer ();
860                         ser.MaxJsonLength = 8;
861                         Y y = new Y ();
862                         string s = ser.Serialize (y);
863                 }
864
865                 [Test]
866                 public void RecursionLimitDeserialize1 () 
867                 {
868                         JavaScriptSerializer ser = new JavaScriptSerializer ();
869                         ser.RecursionLimit = 3;
870                         YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
871                 }
872
873                 [Test]
874                 public void RecursionLimitDeserialize2 () 
875                 {
876                         JavaScriptSerializer ser = new JavaScriptSerializer ();
877                         ser.RecursionLimit = 2;
878                         YY yy = ser.Deserialize<YY> ("{\"Y1\":{},\"Y2\":{}}");
879                 }
880
881                 [Test]
882                 public void RecursionLimitDeserialize3 () 
883                 {
884                         JavaScriptSerializer ser = new JavaScriptSerializer ();
885                         ser.RecursionLimit = 1;
886                         object o = ser.DeserializeObject ("\"xxx\"");
887                 }
888
889                 [Test]
890                 [ExpectedException(typeof(ArgumentException))]
891                 public void RecursionLimitDeserializeToDeep () 
892                 {
893                         JavaScriptSerializer ser = new JavaScriptSerializer ();
894                         ser.RecursionLimit = 2;
895                         YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
896                 }
897
898                 [Test]
899                 public void RecursionLimitSerialize () 
900                 {
901                         JavaScriptSerializer ser = new JavaScriptSerializer ();
902                         ser.RecursionLimit = 3;
903                         YY yy = new YY();
904                         string s = ser.Serialize (yy);
905                 }
906
907                 [Test]
908                 [ExpectedException(typeof(ArgumentException))]
909                 public void RecursionLimitSerializeToDeep () 
910                 {
911                         JavaScriptSerializer ser = new JavaScriptSerializer ();
912                         ser.RecursionLimit = 2;
913                         YY yy = new YY ();
914                         string s = ser.Serialize (yy);
915                 }
916
917                 [Test]
918                 public void RecursionLimitSerialize2 () 
919                 {
920                         JavaScriptSerializer ser = new JavaScriptSerializer ();
921                         ser.RecursionLimit = 2;
922                         YY yy = new YY ();
923                         StringBuilder b = new StringBuilder ();
924                         bool caughtException = false;
925                         try {
926                                 ser.Serialize (yy, b);
927                         }
928                         catch {
929                                 caughtException = true;
930                         }
931                         Assert.IsTrue (caughtException, "RecursionLimitSerialize2 Expected an exception!");
932                         Assert.AreEqual ("{\"Y1\":{\"BB\":", b.ToString (), "RecursionLimitSerialize2");
933                 }
934
935                 [Test]
936                 public void SimpleTypeResolver () 
937                 {
938                         JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
939                         YY yy = new YY ();
940                         string s = ser.Serialize (yy);
941                         string expected = String.Format("\"__type\":\"{0}\"", yy.GetType().AssemblyQualifiedName);
942
943                         Assert.IsTrue (s.Contains (expected), "YY: expected {0} to contain {1}", s, expected);
944                         
945                         expected = String.Format ("\"__type\":\"{0}\"", yy.Y1.GetType ().AssemblyQualifiedName);
946                         Assert.IsTrue (s.Contains (expected), "Y: expected {0} to contain {1}", s, expected);
947                 }
948
949                 public class CustomResolver : JavaScriptTypeResolver
950                 {
951                         public CustomResolver () 
952                         {
953                                 Reset ();
954                         }
955
956                         public override Type ResolveType (string id) 
957                         {
958                                 ResolvedIds.Add (id);
959
960                                 switch (id) {
961                                 case "YY":
962                                         return typeof(YY);
963
964                                 case "Y":
965                                         return typeof (Y);
966
967                                 case "X":
968                                         return typeof (X);
969
970                                 case "int":
971                                         return typeof (int);
972
973                                 case "long":
974                                         return typeof (long);
975
976                                 case "string":
977                                         return typeof (string);
978
979                                 case "point":
980                                         return typeof(Point);
981                                 }
982                                 return null;
983                         }
984
985                         public override string ResolveTypeId (Type type) 
986                         {
987                                 if (type == null) {
988                                         throw new ArgumentNullException ("type");
989                                 }
990
991                                 ResolvedTypes.Add (type.Name);
992
993                                 if (type == typeof (YY))
994                                         return "YY";
995
996                                 if (type == typeof (Y))
997                                         return "Y";
998
999                                 if (type == typeof (X))
1000                                         return "X";
1001
1002                                 if (type == typeof (int))
1003                                         return "int";
1004
1005                                 if (type == typeof (long))
1006                                         return "long";
1007
1008                                 if (type == typeof (string))
1009                                         return "string";
1010
1011                                 if (type == typeof(Point))
1012                                         return "point";
1013
1014                                 return null;
1015                         }
1016
1017                         public static List<string> ResolvedTypes {
1018                                 get {
1019                                         if (resolvedTypes == null) {
1020                                                 resolvedTypes = new List<string> ();
1021                                         }
1022                                         return resolvedTypes;
1023                                 }
1024                         }
1025
1026                         public static List<string> ResolvedIds {
1027                                 get {
1028                                         if (resolvedIds == null) {
1029                                                 resolvedIds = new List<string> ();
1030                                         }
1031                                         return resolvedIds;
1032                                 }
1033                         }
1034
1035                         public static void Reset () 
1036                         {
1037                                 resolvedIds = null;
1038                                 resolvedTypes = null;
1039                         }
1040
1041                         private static List<string> resolvedTypes;
1042                         private static List<string> resolvedIds;
1043                 }
1044
1045                 [Test]
1046                 [NUnit.Framework.Category ("NotWorking")]
1047                 public void CustomTypeResolver ()
1048                 {
1049                         JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver ());
1050                         X x = new X ();
1051                         x.Init ();
1052
1053                         string s = ser.Serialize (x);
1054
1055                         CustomResolver.Reset ();
1056                         X x1 = (X) ser.DeserializeObject (s);
1057                         Assert.IsTrue (x.Equals (x1), "x != x1");
1058
1059                         CustomResolver.Reset ();
1060                         X x2 = ser.Deserialize<X> (s);
1061                         Assert.IsTrue (x.Equals (x2), "x != x2");
1062                 }
1063
1064                 [Test]
1065                 public void InfinityAndNaN ()
1066                 {
1067                         JavaScriptSerializer ser = new JavaScriptSerializer ();
1068
1069                         double nan = Double.NaN;
1070                         string s = ser.Serialize (nan);
1071                         Assert.AreEqual (s, "NaN", "#A1");
1072                         nan = (double)ser.DeserializeObject (s);
1073                         Assert.AreEqual (Double.NaN, nan, "#A2");
1074                         nan = (double)ser.Deserialize <double> (s);
1075                         Assert.AreEqual (Double.NaN, nan, "#A3");
1076                         
1077                         double infinity = Double.PositiveInfinity;
1078                         s = ser.Serialize (infinity);
1079                         Assert.AreEqual (s, "Infinity", "#B1");
1080                         infinity = (double)ser.DeserializeObject (s);
1081                         Assert.AreEqual (Double.PositiveInfinity, infinity, "#B2");
1082                         infinity = ser.Deserialize <double> (s);
1083                         Assert.AreEqual (Double.PositiveInfinity, infinity, "#B3");
1084                         
1085                         infinity = Double.NegativeInfinity;
1086                         s = ser.Serialize (infinity);
1087                         Assert.AreEqual (s, "-Infinity", "#C1");
1088                         infinity = (double)ser.DeserializeObject (s);
1089                         Assert.AreEqual (Double.NegativeInfinity, infinity, "#C2");
1090                         infinity = ser.Deserialize <double> (s);
1091                         Assert.AreEqual (Double.NegativeInfinity, infinity, "#C3");
1092
1093                         var dict = new Dictionary <string, object> ();
1094                         dict.Add ("A", Double.NaN);
1095                         dict.Add ("B", Double.PositiveInfinity);
1096                         dict.Add ("C", Double.NegativeInfinity);
1097                         s = ser.Serialize (dict);
1098                         Assert.AreEqual ("{\"A\":NaN,\"B\":Infinity,\"C\":-Infinity}", s, "#D1");
1099                         
1100                         dict = (Dictionary <string, object>)ser.DeserializeObject (s);
1101                         Assert.AreEqual (Double.NaN, dict ["A"], "#D2");
1102                         Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D3");
1103                         Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D4");
1104
1105                         dict = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object>> (s);
1106                         Assert.AreEqual (Double.NaN, dict ["A"], "#D5");
1107                         Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D6");
1108                         Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D7");
1109
1110                         var arr = new ArrayList () {
1111                                         Double.NaN,
1112                                         Double.PositiveInfinity,
1113                                         Double.NegativeInfinity};
1114                         s = ser.Serialize (arr);
1115                         Assert.AreEqual ("[NaN,Infinity,-Infinity]", s, "#E1");
1116
1117                         object[] arr2 = (object[])ser.DeserializeObject (s);
1118                         Assert.AreEqual (3, arr2.Length, "#E2");
1119                         Assert.AreEqual (Double.NaN, arr2 [0], "#E3");
1120                         Assert.AreEqual (Double.PositiveInfinity, arr2 [1], "#E4");
1121                         Assert.AreEqual (Double.NegativeInfinity, arr2 [2], "#E5");
1122
1123                         arr = ser.Deserialize <ArrayList> (s);
1124                         Assert.AreEqual (3, arr.Count, "#E6");
1125                         Assert.AreEqual (Double.NaN, arr [0], "#E7");
1126                         Assert.AreEqual (Double.PositiveInfinity, arr [1], "#E8");
1127                         Assert.AreEqual (Double.NegativeInfinity, arr [2], "#E9");
1128                 }
1129
1130                 [Test]
1131                 public void StandalonePrimitives ()
1132                 {
1133                         JavaScriptSerializer ser = new JavaScriptSerializer ();
1134
1135                         object o;
1136                         int i;
1137                         
1138                         o = ser.DeserializeObject ("1");
1139                         Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A1");
1140                         i = (int)o;
1141                         Assert.AreEqual (1, i, "#A2");
1142                         o =ser.DeserializeObject ("-1");
1143                         Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A3");
1144                         i = (int)o;
1145                         Assert.AreEqual (-1, i, "#A4");
1146                         
1147                         o = ser.DeserializeObject ("2147483649");
1148                         Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B1");
1149                         long l = (long)o;
1150                         Assert.AreEqual (2147483649, l, "#B2");
1151                         o = ser.DeserializeObject ("-2147483649");
1152                         Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B3");
1153                         l = (long)o;
1154                         Assert.AreEqual (-2147483649, l, "#B4");
1155
1156                         o = ser.DeserializeObject ("9223372036854775808");
1157                         Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C1");
1158                         decimal d = (decimal)o;
1159                         Assert.AreEqual (9223372036854775808m, d, "#C2");
1160                         o = ser.DeserializeObject ("-9223372036854775809");
1161                         Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C3");
1162                         d = (decimal)o;
1163                         Assert.AreEqual (-9223372036854775809m, d, "#C4");
1164
1165                         o = ser.DeserializeObject ("79228162514264337593543950336");
1166                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D1");
1167                         double db = (double)o;
1168                         Assert.AreEqual (79228162514264337593543950336.0, db, "#D2");
1169                         o = ser.DeserializeObject ("-79228162514264337593543950336");
1170                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D3");
1171                         db = (double)o;
1172                         Assert.AreEqual (-79228162514264337593543950336.0, db, "#D4");
1173                         
1174                         o = ser.DeserializeObject ("\"test string\"");
1175                         Assert.AreEqual (typeof (global::System.String), o.GetType (), "#E1");
1176                         string s = (string)o;
1177                         Assert.AreEqual ("test string", s, "#E2");
1178
1179                         o = ser.DeserializeObject ("true");
1180                         Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F1");
1181                         bool b = (bool)o;
1182                         Assert.AreEqual (true, b, "#F2");
1183
1184                         o = ser.DeserializeObject ("false");
1185                         Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F3");
1186                         b = (bool)o;
1187                         Assert.AreEqual (false, b, "#F4");
1188
1189                         o = ser.DeserializeObject ("-1.7976931348623157E+308");
1190                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G1");
1191                         db = (double)o;
1192                         Assert.AreEqual (Double.MinValue, db, "#G2");
1193
1194                         o = ser.DeserializeObject ("1.7976931348623157E+308");
1195                         Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G3");
1196                         db = (double)o;
1197                         Assert.AreEqual (Double.MaxValue, db, "#G4");
1198                 }
1199
1200                 class SomeDict : IDictionary<string, object>
1201                 {
1202                         void IDictionary<string, object>.Add (string key, object value) {
1203                                 throw new NotSupportedException ();
1204                         }
1205
1206                         bool IDictionary<string, object>.ContainsKey (string key) {
1207                                 throw new NotSupportedException ();
1208                         }
1209
1210                         ICollection<string> IDictionary<string, object>.Keys {
1211                                 get { throw new NotSupportedException (); }
1212                         }
1213
1214                         bool IDictionary<string, object>.Remove (string key) {
1215                                 throw new NotSupportedException ();
1216                         }
1217
1218                         bool IDictionary<string, object>.TryGetValue (string key, out object value) {
1219                                 throw new NotSupportedException ();
1220                         }
1221
1222                         ICollection<object> IDictionary<string, object>.Values {
1223                                 get { throw new NotSupportedException (); }
1224                         }
1225
1226                         object IDictionary<string, object>.this [string key] {
1227                                 get { throw new NotSupportedException (); }
1228                                 set { throw new NotSupportedException (); }
1229                         }
1230
1231                         void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item) {
1232                                 throw new NotSupportedException ();
1233                         }
1234
1235                         void ICollection<KeyValuePair<string, object>>.Clear () {
1236                                 throw new NotSupportedException ();
1237                         }
1238
1239                         bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item) {
1240                                 throw new NotSupportedException ();
1241                         }
1242
1243                         void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex) {
1244                                 throw new NotSupportedException ();
1245                         }
1246
1247                         int ICollection<KeyValuePair<string, object>>.Count {
1248                                 get { throw new NotSupportedException (); }
1249                         }
1250
1251                         bool ICollection<KeyValuePair<string, object>>.IsReadOnly {
1252                                 get { throw new NotSupportedException (); }
1253                         }
1254
1255                         bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item) {
1256                                 throw new NotSupportedException ();
1257                         }
1258
1259                         IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator () {
1260                                 return GetEnumerator ();
1261                         }
1262
1263                         IEnumerator IEnumerable.GetEnumerator () {
1264                                 return ((IEnumerable<KeyValuePair<string, object>>) this).GetEnumerator ();
1265                         }
1266
1267                         protected IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
1268                                 yield return new KeyValuePair<string, object> ("hello", "world");
1269                         }
1270                 }
1271
1272                 [Test] //bug #424704
1273                 public void NonGenericClassImplementingClosedGenericIDictionary ()
1274                 {
1275                         JavaScriptSerializer ser = new JavaScriptSerializer ();
1276
1277                         SomeDict dictIn = new SomeDict ();
1278
1279                         string s = ser.Serialize (dictIn);
1280
1281                         Dictionary<string, object> dictOut = ser.Deserialize<Dictionary<string, object>> (s);
1282                         Assert.AreEqual (dictOut.Count, 1, "#1");
1283                         Assert.AreEqual (dictOut["hello"], "world", "#2");
1284                 }
1285
1286                 [Test]
1287                 public void ConvertToIDictionary ()
1288                 {
1289                         JavaScriptSerializer jss = new JavaScriptSerializer ();
1290                         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}}";
1291                         object input = jss.Deserialize<IDictionary>(json);
1292                         IDictionary o = jss.ConvertToType<IDictionary>(input);
1293
1294                         Assert.IsTrue (o != null, "#A1");
1295                         Assert.AreEqual (typeof (Dictionary <string, object>), o.GetType (), "#A2");
1296                 }
1297
1298                 [Test]
1299                 public void ConvertToGenericIDictionary ()
1300                 {
1301                         JavaScriptSerializer jss = new JavaScriptSerializer ();
1302                         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}}";
1303
1304                         object input = jss.Deserialize<IDictionary>(json);
1305                         
1306                         IDictionary <string, object> o = jss.ConvertToType<IDictionary <string, object>>(input);
1307                         Assert.IsTrue (o != null, "#A1");
1308                         Assert.AreEqual (typeof (Dictionary <string, object>), o.GetType (), "#A2");
1309
1310                         IDictionary <object, object> o1 = jss.ConvertToType<IDictionary <object, object>>(input);
1311                         Assert.IsTrue (o1 != null, "#B1");
1312                         Assert.AreEqual (typeof (Dictionary <object, object>), o1.GetType (), "#B2");
1313                 }
1314
1315                 [Test]
1316                 [ExpectedException (typeof (InvalidOperationException))]
1317                 public void ConvertToGenericIDictionary_InvalidDefinition ()
1318                 {
1319                         JavaScriptSerializer jss = new JavaScriptSerializer ();
1320                         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}}";
1321
1322                         object input = jss.Deserialize<IDictionary>(json);
1323                         IDictionary <int, object> o = jss.ConvertToType<IDictionary <int, object>>(input);
1324                 }
1325         }
1326 }