2 // JavaScriptSerializer.cs
5 // Konstantin Triger <kostat@mainsoft.com>
7 // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
33 using NUnit.Framework;
34 using System.Web.Script.Serialization;
35 using System.Reflection;
36 using System.Collections;
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;
48 namespace Tests.System.Web.Script.Serialization
51 public class JavaScriptSerializerTest
59 #pragma warning disable 659
63 //public DateTime dt1;
64 //public DateTime dt2;
69 //dt = DateTime.MaxValue;
70 //dt1 = DateTime.MinValue;
71 //dt2 = new DateTime ((DateTime.Now.Ticks / 10000) * 10000);
73 //hash = new Hashtable ();
74 //hash.Add ("mykey", 1);
77 public override bool Equals (object obj) {
80 JavaScriptSerializerTest.FieldsEqual (this, obj);
90 Y [] _yyy = new Y [] { new Y (), new Y () };
126 public Nullable<bool> nb;
128 IEnumerable<int> enum_int;
129 IEnumerable enum_int1;
131 public Dictionary<string, Y> hash;
134 public void Init () {
136 _bb = ulong.MaxValue - 5;
137 _yy = new Y [] { new Y (), new Y () };
141 str = "\uFF56\uFF57\uF58FF59g";
144 sh = short.MinValue + 28;
145 ush = ushort.MaxValue - 24;
147 ui = uint.MaxValue - 234234;
148 l = long.MinValue + 28;
149 ul = ulong.MaxValue - 3;
152 f1 = float.NegativeInfinity;
153 f2 = float.PositiveInfinity;
158 d1 = double.NegativeInfinity;
159 d2 = double.PositiveInfinity;
160 d3 = double.MinValue;
161 d4 = double.MaxValue;
163 de = decimal.MinusOne;
166 de3 = decimal.MinValue;
167 de4 = decimal.MaxValue;
169 g = new Guid (234, 2, 354, new byte [] { 1, 2, 3, 4, 5, 6, 7, 8 });
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);
180 hash = new Dictionary<string, Y> ();
183 point = new Point (150, 150);
186 public IEnumerable<int> MyEnum {
198 public IEnumerable MyEnum1 {
228 public override bool Equals (object obj) {
231 JavaScriptSerializerTest.FieldsEqual (this, obj);
246 public override bool Equals (object obj) {
249 JavaScriptSerializerTest.FieldsEqual(this, obj);
266 [TypeConverter (typeof (MyUriConverter))]
269 public MyUri (string uriString, UriKind uriKind)
270 : base (uriString, uriKind) {
273 public MyUri (Uri value)
274 : base (value.AbsoluteUri) {
278 class MyUriConverter : UriTypeConverter
280 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
282 return base.ConvertTo (context, culture, value, destinationType);
285 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
287 Uri convertedUri = (Uri)base.ConvertFrom (context, culture, value);
288 return new MyUri (convertedUri);
292 [TypeConverter(typeof(MyPointConverter))]
293 class MyPointContainer
295 public MyPointContainer ()
299 public MyPointContainer (Point v)
307 class MyPointConverter : TypeConverter
309 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
311 if (destinationType == typeof (string)) {
314 return base.CanConvertTo (context, destinationType);
317 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
319 if (destinationType == typeof (string)) {
320 MyPointContainer pc = (MyPointContainer) value;
321 return pc.p.X + "," + pc.p.Y;
323 return base.ConvertTo (context, culture, value, destinationType);
326 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
328 if (sourceType == typeof (string)) {
331 return base.CanConvertFrom (context, sourceType);
334 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
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])));
340 return base.ConvertFrom (context, culture, value);
344 #pragma warning restore 659
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);
366 public void TestDeserialize () {
367 JavaScriptSerializer ser = new JavaScriptSerializer ();
368 Assert.IsNull (ser.Deserialize<X> (""));
372 string x = ser.Serialize (s);
373 X n = ser.Deserialize<X> (x);
374 Assert.AreEqual (s, n);
376 //string json = "\\uFF56";
377 //string result = ser.Deserialize<string> (json);
378 //Assert.AreEqual ("\uFF56", result);
380 //object oo = ser.DeserializeObject ("{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}");
384 public void TestDeserializeTypeResolver ()
386 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
390 string s = ser.Serialize (x);
391 X x2 = ser.Deserialize<X> (s);
392 Assert.AreEqual (x, x2);
396 public void TestDeserializeBugs () {
397 JavaScriptSerializer ser = new JavaScriptSerializer ();
401 string x = ser.Serialize (s);
402 bug n = ser.Deserialize<bug> (x);
403 Assert.AreEqual (s, n);
405 // Should check correctness with .Net GA:
406 //js = ser.Serialize (Color.Red);
407 //Color ccc = ser.Deserialize<Color> (js);
408 //string xml = @"<root><node attr=""xxx""/></root>";
410 //XmlDocument doc = new XmlDocument ();
412 //string js = ser.Serialize (doc);
413 //DataTable table = new DataTable();
414 //table.Columns.Add ("col1", typeof (int));
415 //table.Columns.Add ("col2", typeof (float));
416 //table.Rows.Add (1, 1f);
417 //table.Rows.Add (234234, 2.4f);
419 //string js = ser.Serialize (table);
422 static void FieldsEqual (object expected, object actual) {
423 Assert.AreEqual (expected.GetType (), actual.GetType ());
424 FieldInfo [] infos = expected.GetType ().GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
425 foreach (FieldInfo info in infos) {
426 object value1 = info.GetValue (expected);
427 object value2 = info.GetValue (actual);
428 if (value1 is IEnumerable) {
429 IEnumerator yenum = ((IEnumerable) value2).GetEnumerator ();
431 foreach (object x in (IEnumerable) value1) {
432 if (!yenum.MoveNext ())
433 Assert.Fail (info.Name + " index:" + index);
435 if (x is DictionaryEntry) {
436 DictionaryEntry entry = (DictionaryEntry)x;
437 IDictionary dict = (IDictionary) value2;
438 Assert.AreEqual (entry.Value, dict [entry.Key], info.Name + ", key:" + entry.Key);
441 Assert.AreEqual (x, yenum.Current, info.Name + ", index:" + index);
443 Assert.IsFalse (yenum.MoveNext (), info.Name);
446 Assert.AreEqual (value1, value2, info.Name);
452 [ExpectedException (typeof (ArgumentNullException))]
453 public void TestDeserialize1 () {
454 JavaScriptSerializer ser = new JavaScriptSerializer ();
455 ser.Deserialize<string> (null);
459 [ExpectedException (typeof (ArgumentNullException))]
460 public void TestDeserializeNullConverter () {
461 JavaScriptSerializer ser = new JavaScriptSerializer ();
462 ser.RegisterConverters (null);
466 public void TestDeserializeConverter () {
467 JavaScriptSerializer ser = new JavaScriptSerializer ();
468 List<JavaScriptConverter> list = new List<JavaScriptConverter> ();
469 list.Add (new MyJavaScriptConverter ());
470 list.Add (new CultureInfoConverter ());
471 ser.RegisterConverters (list);
472 string result = ser.Serialize (new X [] { new X (), new X () });
473 Assert.AreEqual ("{\"0\":1,\"1\":2}", result);
474 result = ser.Serialize (Thread.CurrentThread.CurrentCulture);
478 public void TestDeserializeConverter1 () {
479 JavaScriptSerializer serializer = new JavaScriptSerializer ();
481 serializer.RegisterConverters (new JavaScriptConverter [] {
482 new ListItemCollectionConverter() });
484 ListBox ListBox1 = new ListBox ();
485 ListBox1.Items.Add ("a1");
486 ListBox1.Items.Add ("a2");
487 ListBox1.Items.Add ("a3");
489 string x = serializer.Serialize (ListBox1.Items);
490 ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection> (x);
491 Assert.AreEqual (3, recoveredList.Count);
495 public void TestSerialize1 () {
496 JavaScriptSerializer ser = new JavaScriptSerializer ();
497 Assert.AreEqual("null", ser.Serialize(null));
499 string js = ser.Serialize (1234);
500 Assert.AreEqual ("1234", js);
501 Assert.AreEqual (1234, ser.Deserialize<int> (js));
502 js = ser.Serialize (1.1);
503 Assert.AreEqual ("1.1", js);
504 Assert.AreEqual (1.1f, ser.Deserialize<float> (js));
505 char [] chars = "faskjhfasd0981234".ToCharArray ();
506 js = ser.Serialize (chars);
507 char[] actual = ser.Deserialize<char[]> (js);
508 Assert.AreEqual (chars.Length, actual.Length);
509 for (int i = 0; i < chars.Length; i++)
510 Assert.AreEqual (chars[i], actual[i]);
512 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{|}~""";
513 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{|}~";
515 string serRes = ser.Serialize (data);
516 Assert.AreEqual (expected, serRes);
517 string deserRes = ser.Deserialize<string> (serRes);
518 Assert.AreEqual (data, deserRes);
522 [ExpectedException (typeof (ArgumentNullException))]
523 [Category ("NotDotNet")]
524 public void TestSerialize2 () {
525 JavaScriptSerializer ser = new JavaScriptSerializer ();
526 ser.Serialize ("aaa", null);
529 static readonly long InitialJavaScriptDateTicks = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
532 public void TestSerializeDate () {
533 JavaScriptSerializer ser = new JavaScriptSerializer ();
534 DateTime now = new DateTime (633213894056010000L);
536 string actual = ser.Serialize (now);
537 DateTime dateTime = now.ToUniversalTime ();
538 long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long) 10000;
540 object dd = ser.DeserializeObject (@"""\/Datte(" + javaScriptTicks + @")\/""");
541 Assert.AreEqual (@"""\/Date(" + javaScriptTicks + @")\/""", actual);
542 Assert.AreEqual (now.ToUniversalTime(), ser.DeserializeObject (actual));
546 public void TestSerializeEnum () {
547 JavaScriptSerializer ser = new JavaScriptSerializer ();
548 string result = ser.Serialize (MyEnum.BBB);
549 Assert.AreEqual ("1", result);
550 Assert.AreEqual (MyEnum.BBB, ser.Deserialize<MyEnum> (result));
553 class MyJavaScriptConverter : JavaScriptConverter
555 public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
556 throw new Exception ("The method or operation is not implemented.");
559 public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
560 Array a = (Array) obj;
561 Dictionary<string, object> d = new Dictionary<string, object> ();
565 //throw new Exception ("The method or operation is not implemented.");
568 public override IEnumerable<Type> SupportedTypes {
570 yield return typeof (X[]);
575 sealed class CultureInfoConverter : JavaScriptConverter
577 static readonly Type typeofCultureInfo = typeof (CultureInfo);
578 public override IEnumerable<Type> SupportedTypes {
579 get { yield return typeofCultureInfo; }
582 public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
583 throw new NotSupportedException ();
586 public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
587 CultureInfo ci = (CultureInfo)obj;
590 Dictionary<string, object> d = new Dictionary<string, object> ();
591 d.Add ("name", ci.Name);
592 d.Add ("numberFormat", ci.NumberFormat);
593 d.Add ("dateTimeFormat", ci.DateTimeFormat);
598 public class ListItemCollectionConverter : JavaScriptConverter
600 public override IEnumerable<Type> SupportedTypes {
601 //Define the ListItemCollection as a supported type.
602 get { return new ReadOnlyCollection<Type> (new Type [] { typeof (ListItemCollection) }); }
605 public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
606 ListItemCollection listType = obj as ListItemCollection;
608 if (listType != null) {
609 // Create the representation.
610 Dictionary<string, object> result = new Dictionary<string, object> ();
611 ArrayList itemsList = new ArrayList ();
612 foreach (ListItem item in listType) {
613 //Add each entry to the dictionary.
614 Dictionary<string, object> listDict = new Dictionary<string, object> ();
615 listDict.Add ("Value", item.Value);
616 listDict.Add ("Text", item.Text);
617 itemsList.Add (listDict);
619 result ["List"] = itemsList;
623 return new Dictionary<string, object> ();
626 public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
627 if (dictionary == null)
628 throw new ArgumentNullException ("dictionary");
630 if (type == typeof (ListItemCollection)) {
631 // Create the instance to deserialize into.
632 ListItemCollection list = new ListItemCollection ();
634 // Deserialize the ListItemCollection's items.
635 ArrayList itemsList = (ArrayList) dictionary ["List"];
636 for (int i = 0; i < itemsList.Count; i++)
637 list.Add (serializer.ConvertToType<ListItem> (itemsList [i]));
646 public void DeserializeObject () {
647 object o = new JavaScriptSerializer ().DeserializeObject ("{\"Numeric\":0,\"Array\":[true,false,0]}");
648 Assert.IsNotNull (o as Dictionary<string, object>, "type");
649 Dictionary<string, object> dictionary = (Dictionary<string, object>) o;
650 Assert.AreEqual (0, (int) dictionary ["Numeric"], "Numeric");
651 Assert.IsNotNull (dictionary ["Array"] as object [], "Array type");
652 object [] array = (object []) dictionary ["Array"];
653 Assert.AreEqual (true, (bool) array [0], "array [0]");
654 Assert.AreEqual (false, (bool) array [1], "array [1]");
655 Assert.AreEqual (0, (int) array [2], "array [2]");
659 public void DeserializeObject2 ()
661 JavaScriptSerializer ser = new JavaScriptSerializer ();
663 string s = ser.Serialize (y);
664 object y2 = ser.DeserializeObject (s);
665 Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
669 public void DeserializeObject3 ()
671 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver());
673 string s = ser.Serialize (y);
674 object y2 = ser.DeserializeObject (s);
675 Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
679 public void DeserializeObject4 ()
681 JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver());
683 string s = ser.Serialize (y);
684 object y2 = ser.DeserializeObject (s);
685 Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
686 Assert.AreEqual (1, CustomResolver.ResolvedIds.Count, "ResolvedIds Count");
687 Assert.AreEqual ("Y", CustomResolver.ResolvedIds [0], "ResolvedIds.Y");
688 Assert.AreEqual (1, CustomResolver.ResolvedTypes.Count, "ResolvedTypes Count");
689 Assert.AreEqual ("Y", CustomResolver.ResolvedTypes [0], "ResolvedTypes.Y");
693 [ExpectedException(typeof(ArgumentNullException))]
694 public void SerializeWithResolverDeserializeWithout ()
696 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
698 string s = ser.Serialize (y);
699 ser = new JavaScriptSerializer ();
700 object y2 = ser.DeserializeObject (s);
704 public void SerializeWithoutResolverDeserializeWith ()
706 JavaScriptSerializer ser = new JavaScriptSerializer ();
708 string s = ser.Serialize (y);
709 ser = new JavaScriptSerializer (new SimpleTypeResolver ());
710 object y2 = ser.DeserializeObject (s);
711 Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
717 public string s1 = "s1";
723 public string s2 = "s2";
728 public B b1 = new B ();
729 public B b2 = new D ();
733 public void SerializeDerivedType ()
735 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
737 string s = ser.Serialize (b);
738 B b2 = ser.Deserialize<B> (s);
739 Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
743 public void SerializeDerivedType2 ()
745 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
747 string s = ser.Serialize (b);
748 B b2 = (B)ser.DeserializeObject (s);
749 Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
753 public void SerializeContainedDerivedType ()
755 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
757 string s = ser.Serialize (c);
758 C c2 = ser.Deserialize<C> (s);
759 Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
760 Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
764 public void SerializeContainedDerivedType2 ()
766 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
768 string s = ser.Serialize (c);
769 C c2 = (C)ser.DeserializeObject (s);
770 Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
771 Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
775 public void SerializeWithTypeConverter ()
777 JavaScriptSerializer ser = new JavaScriptSerializer ();
778 MyUri uri = new MyUri ("http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d", UriKind.RelativeOrAbsolute);
779 string s = ser.Serialize (uri);
780 MyUri uri2 = ser.Deserialize<MyUri> (s);
781 Assert.AreEqual (uri, uri2);
785 public void SerializeWithTypeConverter2 ()
787 JavaScriptSerializer ser = new JavaScriptSerializer ();
788 MyPointContainer pc = new MyPointContainer(new Point(15, 16));
789 string s = ser.Serialize(pc);
790 MyPointContainer pc2 = ser.Deserialize<MyPointContainer>(s);
794 public void MaxJsonLengthDeserializeObject ()
796 JavaScriptSerializer ser = new JavaScriptSerializer ();
797 ser.MaxJsonLength = 16;
798 object o = ser.DeserializeObject ("{s:'1234567890'}");
802 [ExpectedException(typeof(ArgumentException))]
803 public void MaxJsonLengthDeserializeObjectToLong ()
805 JavaScriptSerializer ser = new JavaScriptSerializer ();
806 ser.MaxJsonLength = 15;
807 object o = ser.DeserializeObject ("{s:'1234567890'}");
811 public void MaxJsonLengthSerialize ()
813 JavaScriptSerializer ser = new JavaScriptSerializer ();
814 ser.MaxJsonLength = 9;
816 string s = ser.Serialize (y);
820 [ExpectedException (typeof (InvalidOperationException))]
821 public void MaxJsonLengthSerializeToLong ()
823 JavaScriptSerializer ser = new JavaScriptSerializer ();
824 ser.MaxJsonLength = 8;
826 string s = ser.Serialize (y);
830 public void RecursionLimitDeserialize1 ()
832 JavaScriptSerializer ser = new JavaScriptSerializer ();
833 ser.RecursionLimit = 3;
834 YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
838 public void RecursionLimitDeserialize2 ()
840 JavaScriptSerializer ser = new JavaScriptSerializer ();
841 ser.RecursionLimit = 2;
842 YY yy = ser.Deserialize<YY> ("{\"Y1\":{},\"Y2\":{}}");
846 public void RecursionLimitDeserialize3 ()
848 JavaScriptSerializer ser = new JavaScriptSerializer ();
849 ser.RecursionLimit = 1;
850 object o = ser.DeserializeObject ("\"xxx\"");
854 [ExpectedException(typeof(ArgumentException))]
855 public void RecursionLimitDeserializeToDeep ()
857 JavaScriptSerializer ser = new JavaScriptSerializer ();
858 ser.RecursionLimit = 2;
859 YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
863 public void RecursionLimitSerialize ()
865 JavaScriptSerializer ser = new JavaScriptSerializer ();
866 ser.RecursionLimit = 3;
868 string s = ser.Serialize (yy);
872 [ExpectedException(typeof(ArgumentException))]
873 public void RecursionLimitSerializeToDeep ()
875 JavaScriptSerializer ser = new JavaScriptSerializer ();
876 ser.RecursionLimit = 2;
878 string s = ser.Serialize (yy);
882 public void RecursionLimitSerialize2 ()
884 JavaScriptSerializer ser = new JavaScriptSerializer ();
885 ser.RecursionLimit = 2;
887 StringBuilder b = new StringBuilder ();
888 bool caughtException = false;
890 ser.Serialize (yy, b);
893 caughtException = true;
895 Assert.IsTrue (caughtException, "RecursionLimitSerialize2 Expected an exception!");
896 Assert.AreEqual ("{\"Y1\":{\"BB\":", b.ToString (), "RecursionLimitSerialize2");
900 public void SimpleTypeResolver ()
902 JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
904 string s = ser.Serialize (yy);
905 string expected = String.Format("\"__type\":\"{0}\"", yy.GetType().AssemblyQualifiedName);
907 Assert.IsTrue (s.Contains (expected), "YY: expected {0} to contain {1}", s, expected);
909 expected = String.Format ("\"__type\":\"{0}\"", yy.Y1.GetType ().AssemblyQualifiedName);
910 Assert.IsTrue (s.Contains (expected), "Y: expected {0} to contain {1}", s, expected);
913 public class CustomResolver : JavaScriptTypeResolver
915 public CustomResolver ()
920 public override Type ResolveType (string id)
922 ResolvedIds.Add (id);
938 return typeof (long);
941 return typeof (string);
944 return typeof(Point);
949 public override string ResolveTypeId (Type type)
952 throw new ArgumentNullException ("type");
955 ResolvedTypes.Add (type.Name);
957 if (type == typeof (YY))
960 if (type == typeof (Y))
963 if (type == typeof (X))
966 if (type == typeof (int))
969 if (type == typeof (long))
972 if (type == typeof (string))
975 if (type == typeof(Point))
981 public static List<string> ResolvedTypes {
983 if (resolvedTypes == null) {
984 resolvedTypes = new List<string> ();
986 return resolvedTypes;
990 public static List<string> ResolvedIds {
992 if (resolvedIds == null) {
993 resolvedIds = new List<string> ();
999 public static void Reset ()
1002 resolvedTypes = null;
1005 private static List<string> resolvedTypes;
1006 private static List<string> resolvedIds;
1010 [NUnit.Framework.Category ("NotWorking")]
1011 public void CustomTypeResolver ()
1013 JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver ());
1017 string s = ser.Serialize (x);
1019 CustomResolver.Reset ();
1020 X x1 = (X) ser.DeserializeObject (s);
1021 Assert.IsTrue (x.Equals (x1), "x != x1");
1023 CustomResolver.Reset ();
1024 X x2 = ser.Deserialize<X> (s);
1025 Assert.IsTrue (x.Equals (x2), "x != x2");