JavaScriptSerializerTest.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. //
  2. // JavaScriptSerializer.cs
  3. //
  4. // Author:
  5. // Konstantin Triger <[email protected]>
  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. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using NUnit.Framework;
  33. using System.Web.Script.Serialization;
  34. using System.Reflection;
  35. using System.Collections;
  36. using System.Drawing;
  37. using ComponentModel = System.ComponentModel;
  38. using System.Globalization;
  39. using System.Threading;
  40. using System.Text.RegularExpressions;
  41. using System.ComponentModel;
  42. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  43. using System.Web.UI.WebControls;
  44. using System.Collections.ObjectModel;
  45. namespace Tests.System.Web.Script.Serialization
  46. {
  47. [TestFixture]
  48. public class JavaScriptSerializerTest
  49. {
  50. enum MyEnum
  51. {
  52. AAA,
  53. BBB,
  54. CCC
  55. }
  56. #pragma warning disable 659
  57. class bug
  58. {
  59. //public DateTime dt;
  60. //public DateTime dt1;
  61. //public DateTime dt2;
  62. public bool bb;
  63. //Hashtable hash;
  64. public void Init() {
  65. //dt = DateTime.MaxValue;
  66. //dt1 = DateTime.MinValue;
  67. //dt2 = new DateTime ((DateTime.Now.Ticks / 10000) * 10000);
  68. bb = true;
  69. //hash = new Hashtable ();
  70. //hash.Add ("mykey", 1);
  71. }
  72. public override bool Equals (object obj) {
  73. if (!(obj is bug))
  74. return false;
  75. JavaScriptSerializerTest.FieldsEqual (this, obj);
  76. return true;
  77. }
  78. }
  79. class X
  80. {
  81. int x = 5;
  82. //int y;
  83. ulong _bb;
  84. Y[] _yy;
  85. Y [] _yyy = new Y [] { new Y (), new Y () };
  86. public int z;
  87. public char ch;
  88. public char ch_null;
  89. public string str;
  90. public byte b;
  91. public sbyte sb;
  92. public short sh;
  93. public ushort ush;
  94. public int i;
  95. public uint ui;
  96. public long l;
  97. public ulong ul;
  98. public float f;
  99. public float f1;
  100. public float f2;
  101. public float f3;
  102. public float f4;
  103. public double d;
  104. public double d1;
  105. public double d2;
  106. public double d3;
  107. public double d4;
  108. public decimal de;
  109. public decimal de1;
  110. public decimal de2;
  111. public decimal de3;
  112. public decimal de4;
  113. public Guid g;
  114. public Nullable<bool> nb;
  115. public DBNull dbn;
  116. IEnumerable<int> enum_int;
  117. IEnumerable enum_int1;
  118. public Uri uri;
  119. public Dictionary<string, Y> hash;
  120. public Point point;
  121. public void Init () {
  122. //y = 6;
  123. _bb = ulong.MaxValue - 5;
  124. _yy = new Y [] { new Y (), new Y () };
  125. z = 8;
  126. ch = (char) 0xFF56;
  127. ch_null = '\0';
  128. str = "\uFF56\uFF57\uF58FF59g";
  129. b = 253;
  130. sb = -48;
  131. sh = short.MinValue + 28;
  132. ush = ushort.MaxValue - 24;
  133. i = -234235453;
  134. ui = uint.MaxValue - 234234;
  135. l = long.MinValue + 28;
  136. ul = ulong.MaxValue - 3;
  137. f = float.NaN;
  138. f1 = float.NegativeInfinity;
  139. f2 = float.PositiveInfinity;
  140. f3 = float.MinValue;
  141. f4 = float.MaxValue;
  142. d = double.NaN;
  143. d1 = double.NegativeInfinity;
  144. d2 = double.PositiveInfinity;
  145. d3 = double.MinValue;
  146. d4 = double.MaxValue;
  147. de = decimal.MinusOne;
  148. de1 = decimal.Zero;
  149. de2 = decimal.One;
  150. de3 = decimal.MinValue;
  151. de4 = decimal.MaxValue;
  152. g = new Guid (234, 2, 354, new byte [] { 1, 2, 3, 4, 5, 6, 7, 8 });
  153. nb = null;
  154. dbn = null;
  155. enum_int = new List<int> (MyEnum);
  156. enum_int1 = new ArrayList ();
  157. foreach (object obj in MyEnum1)
  158. ((ArrayList) enum_int1).Add (obj);
  159. uri = new Uri ("http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d", UriKind.RelativeOrAbsolute);
  160. hash = new Dictionary<string, Y> ();
  161. Y y = new Y ();
  162. hash ["mykey"] = y;
  163. point = new Point (150, 150);
  164. }
  165. public IEnumerable<int> MyEnum {
  166. get {
  167. yield return 1;
  168. yield return 10;
  169. yield return 345;
  170. }
  171. set {
  172. enum_int = value;
  173. }
  174. }
  175. public IEnumerable MyEnum1 {
  176. get {
  177. yield return 1;
  178. yield return 10;
  179. yield return 345;
  180. }
  181. set {
  182. enum_int1 = value;
  183. }
  184. }
  185. public int AA {
  186. get { return x; }
  187. }
  188. public Y[] AA1 {
  189. get { return _yyy; }
  190. }
  191. public ulong BB {
  192. get { return _bb; }
  193. set { _bb = value; }
  194. }
  195. public Y[] YY {
  196. get { return _yy; }
  197. set { _yy = value; }
  198. }
  199. public override bool Equals (object obj) {
  200. if (!(obj is X))
  201. return false;
  202. JavaScriptSerializerTest.FieldsEqual (this, obj);
  203. return true;
  204. }
  205. }
  206. class Y
  207. {
  208. long _bb = 10;
  209. public long BB {
  210. get { return _bb; }
  211. set { _bb = value; }
  212. }
  213. public override bool Equals (object obj) {
  214. if (!(obj is Y))
  215. return false;
  216. JavaScriptSerializerTest.FieldsEqual(this, obj);
  217. return true;
  218. }
  219. }
  220. class YY
  221. {
  222. public YY ()
  223. {
  224. Y1 = new Y ();
  225. Y2 = new Y ();
  226. }
  227. public Y Y1;
  228. public Y Y2;
  229. }
  230. [TypeConverter (typeof (MyUriConverter))]
  231. class MyUri : Uri
  232. {
  233. public MyUri (string uriString, UriKind uriKind)
  234. : base (uriString, uriKind) {
  235. }
  236. public MyUri (Uri value)
  237. : base (value.AbsoluteUri) {
  238. }
  239. }
  240. class MyUriConverter : UriTypeConverter
  241. {
  242. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  243. {
  244. return base.ConvertTo (context, culture, value, destinationType);
  245. }
  246. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  247. {
  248. Uri convertedUri = (Uri)base.ConvertFrom (context, culture, value);
  249. return new MyUri (convertedUri);
  250. }
  251. }
  252. [TypeConverter(typeof(MyPointConverter))]
  253. class MyPointContainer
  254. {
  255. public MyPointContainer ()
  256. {
  257. }
  258. public MyPointContainer (Point v)
  259. {
  260. p = v;
  261. }
  262. internal Point p;
  263. }
  264. class MyPointConverter : TypeConverter
  265. {
  266. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  267. {
  268. if (destinationType == typeof (string)) {
  269. return true;
  270. }
  271. return base.CanConvertTo (context, destinationType);
  272. }
  273. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  274. {
  275. if (destinationType == typeof (string)) {
  276. MyPointContainer pc = (MyPointContainer) value;
  277. return pc.p.X + "," + pc.p.Y;
  278. }
  279. return base.ConvertTo (context, culture, value, destinationType);
  280. }
  281. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  282. {
  283. if (sourceType == typeof (string)) {
  284. return true;
  285. }
  286. return base.CanConvertFrom (context, sourceType);
  287. }
  288. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  289. {
  290. if (value is string) {
  291. string [] v = ((string) value).Split (new char [] { ',' });
  292. return new MyPointContainer(new Point (int.Parse (v [0]), int.Parse (v [1])));
  293. }
  294. return base.ConvertFrom (context, culture, value);
  295. }
  296. }
  297. #pragma warning restore 659
  298. [Test]
  299. [Category ("NotDotNet")]
  300. public void TestDefaults () {
  301. JavaScriptSerializer ser = new JavaScriptSerializer ();
  302. Assert.AreEqual (102400, ser.MaxJsonLength);
  303. Assert.AreEqual (100, ser.RecursionLimit);
  304. //List<JavaScriptConverter> l = new List<JavaScriptConverter> ();
  305. //l.Add (new MyJavaScriptConverter ());
  306. //ser.RegisterConverters (l);
  307. //string x = ser.Serialize (new X [] { new X (), new X () });
  308. //string s = ser.Serialize (new X());
  309. //"{\"BB\":10,\"__type\":\"Tests.System.Web.Script.Serialization.JavaScriptSerializerTest+Y, Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null\"}"
  310. //X x = ser.Deserialize<X> (s);
  311. //object ddd = typeof (Y).GetMember ("BB");
  312. //object x1 = ser.Deserialize<X []> (null);
  313. //object x2 = ser.Deserialize<X []> ("");
  314. //object d = ser.Deserialize<X[]> (x);
  315. }
  316. [Test]
  317. public void TestDeserializeUnquotedKeys ()
  318. {
  319. JavaScriptSerializer ser = new JavaScriptSerializer ();
  320. IDictionary dict = ser.Deserialize <Dictionary <string, object>> ("{itemOne:\"1\",itemTwo:\"2\"}");
  321. Assert.AreEqual ("1", dict ["itemOne"], "#A1");
  322. Assert.AreEqual ("2", dict ["itemTwo"], "#A2");
  323. dict = ser.Deserialize <Dictionary <string, object>> ("{itemOne:1,itemTwo:2}");
  324. Assert.AreEqual (1, dict ["itemOne"], "#B1");
  325. Assert.AreEqual (2, dict ["itemTwo"], "#B2");
  326. }
  327. [Test]
  328. public void TestDeserialize () {
  329. JavaScriptSerializer ser = new JavaScriptSerializer ();
  330. Assert.IsNull (ser.Deserialize<X> (""));
  331. X s = new X ();
  332. s.Init ();
  333. string x = ser.Serialize (s);
  334. X n = ser.Deserialize<X> (x);
  335. Assert.AreEqual (s, n);
  336. //string json = "\\uFF56";
  337. //string result = ser.Deserialize<string> (json);
  338. //Assert.AreEqual ("\uFF56", result);
  339. //object oo = ser.DeserializeObject ("{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}");
  340. }
  341. [Test]
  342. public void TestDeserializeTypeResolver ()
  343. {
  344. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  345. X x = new X ();
  346. x.Init ();
  347. string s = ser.Serialize (x);
  348. X x2 = ser.Deserialize<X> (s);
  349. Assert.AreEqual (x, x2);
  350. }
  351. [Test]
  352. public void TestDeserializeBugs () {
  353. JavaScriptSerializer ser = new JavaScriptSerializer ();
  354. bug s = new bug ();
  355. s.Init ();
  356. string x = ser.Serialize (s);
  357. bug n = ser.Deserialize<bug> (x);
  358. Assert.AreEqual (s, n);
  359. // Should check correctness with .Net GA:
  360. //js = ser.Serialize (Color.Red);
  361. //Color ccc = ser.Deserialize<Color> (js);
  362. //string xml = @"<root><node attr=""xxx""/></root>";
  363. //XmlDocument doc = new XmlDocument ();
  364. //doc.LoadXml (xml);
  365. //string js = ser.Serialize (doc);
  366. //DataTable table = new DataTable();
  367. //table.Columns.Add ("col1", typeof (int));
  368. //table.Columns.Add ("col2", typeof (float));
  369. //table.Rows.Add (1, 1f);
  370. //table.Rows.Add (234234, 2.4f);
  371. //string js = ser.Serialize (table);
  372. }
  373. static void FieldsEqual (object expected, object actual) {
  374. Assert.AreEqual (expected.GetType (), actual.GetType ());
  375. FieldInfo [] infos = expected.GetType ().GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
  376. foreach (FieldInfo info in infos) {
  377. object value1 = info.GetValue (expected);
  378. object value2 = info.GetValue (actual);
  379. if (value1 is IEnumerable) {
  380. IEnumerator yenum = ((IEnumerable) value2).GetEnumerator ();
  381. int index = -1;
  382. foreach (object x in (IEnumerable) value1) {
  383. if (!yenum.MoveNext ())
  384. Assert.Fail (info.Name + " index:" + index);
  385. index++;
  386. if (x is DictionaryEntry) {
  387. DictionaryEntry entry = (DictionaryEntry)x;
  388. IDictionary dict = (IDictionary) value2;
  389. Assert.AreEqual (entry.Value, dict [entry.Key], info.Name + ", key:" + entry.Key);
  390. }
  391. else
  392. Assert.AreEqual (x, yenum.Current, info.Name + ", index:" + index);
  393. }
  394. Assert.IsFalse (yenum.MoveNext (), info.Name);
  395. continue;
  396. }
  397. Assert.AreEqual (value1, value2, info.Name);
  398. }
  399. }
  400. [Test]
  401. [ExpectedException (typeof (ArgumentNullException))]
  402. public void TestDeserialize1 () {
  403. JavaScriptSerializer ser = new JavaScriptSerializer ();
  404. ser.Deserialize<string> (null);
  405. }
  406. [Test]
  407. [ExpectedException (typeof (ArgumentNullException))]
  408. public void TestDeserializeNullConverter () {
  409. JavaScriptSerializer ser = new JavaScriptSerializer ();
  410. ser.RegisterConverters (null);
  411. }
  412. [Test]
  413. public void TestDeserializeConverter () {
  414. JavaScriptSerializer ser = new JavaScriptSerializer ();
  415. List<JavaScriptConverter> list = new List<JavaScriptConverter> ();
  416. list.Add (new MyJavaScriptConverter ());
  417. list.Add (new CultureInfoConverter ());
  418. ser.RegisterConverters (list);
  419. string result = ser.Serialize (new X [] { new X (), new X () });
  420. Assert.AreEqual ("{\"0\":1,\"1\":2}", result);
  421. result = ser.Serialize (Thread.CurrentThread.CurrentCulture);
  422. }
  423. [Test]
  424. public void TestDeserializeConverter1 () {
  425. JavaScriptSerializer serializer = new JavaScriptSerializer ();
  426. serializer.RegisterConverters (new JavaScriptConverter [] {new ListItemCollectionConverter()});
  427. ListBox ListBox1 = new ListBox ();
  428. ListBox1.Items.Add ("a1");
  429. ListBox1.Items.Add ("a2");
  430. ListBox1.Items.Add ("a3");
  431. string x = serializer.Serialize (ListBox1.Items);
  432. ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection> (x);
  433. Assert.AreEqual (3, recoveredList.Count);
  434. }
  435. [Test]
  436. public void TestSerialize1 () {
  437. JavaScriptSerializer ser = new JavaScriptSerializer ();
  438. Assert.AreEqual("null", ser.Serialize(null));
  439. string js = ser.Serialize (1234);
  440. Assert.AreEqual ("1234", js);
  441. Assert.AreEqual (1234, ser.Deserialize<int> (js));
  442. js = ser.Serialize (1.1);
  443. Assert.AreEqual ("1.1", js);
  444. Assert.AreEqual (1.1f, ser.Deserialize<float> (js));
  445. char [] chars = "faskjhfasd0981234".ToCharArray ();
  446. js = ser.Serialize (chars);
  447. char[] actual = ser.Deserialize<char[]> (js);
  448. Assert.AreEqual (chars.Length, actual.Length);
  449. for (int i = 0; i < chars.Length; i++)
  450. Assert.AreEqual (chars[i], actual[i]);
  451. 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{|}~""";
  452. 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{|}~";
  453. string serRes = ser.Serialize (data);
  454. Assert.AreEqual (expected, serRes);
  455. string deserRes = ser.Deserialize<string> (serRes);
  456. Assert.AreEqual (data, deserRes);
  457. }
  458. [Test]
  459. [ExpectedException (typeof (ArgumentNullException))]
  460. [Category ("NotDotNet")]
  461. public void TestSerialize2 () {
  462. JavaScriptSerializer ser = new JavaScriptSerializer ();
  463. ser.Serialize ("aaa", (StringBuilder)null);
  464. }
  465. static readonly long InitialJavaScriptDateTicks = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
  466. [Test]
  467. public void TestSerializeDate () {
  468. JavaScriptSerializer ser = new JavaScriptSerializer ();
  469. DateTime now = new DateTime (633213894056010000L);
  470. string actual = ser.Serialize (now);
  471. DateTime dateTime = now.ToUniversalTime ();
  472. long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long) 10000;
  473. object dd = ser.DeserializeObject (@"""\/Datte(" + javaScriptTicks + @")\/""");
  474. Assert.AreEqual (@"""\/Date(" + javaScriptTicks + @")\/""", actual);
  475. Assert.AreEqual (now.ToUniversalTime(), ser.DeserializeObject (actual));
  476. }
  477. [Test]
  478. public void TestSerializeEnum () {
  479. JavaScriptSerializer ser = new JavaScriptSerializer ();
  480. string result = ser.Serialize (MyEnum.BBB);
  481. Assert.AreEqual ("1", result);
  482. Assert.AreEqual (MyEnum.BBB, ser.Deserialize<MyEnum> (result));
  483. }
  484. class MyJavaScriptConverter : JavaScriptConverter
  485. {
  486. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  487. throw new Exception ("The method or operation is not implemented.");
  488. }
  489. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  490. Array a = (Array) obj;
  491. Dictionary<string, object> d = new Dictionary<string, object> ();
  492. d.Add ("0", 1);
  493. d.Add ("1", 2);
  494. return d;
  495. //throw new Exception ("The method or operation is not implemented.");
  496. }
  497. public override IEnumerable<Type> SupportedTypes {
  498. get {
  499. yield return typeof (X[]);
  500. }
  501. }
  502. }
  503. sealed class CultureInfoConverter : JavaScriptConverter
  504. {
  505. static readonly Type typeofCultureInfo = typeof (CultureInfo);
  506. public override IEnumerable<Type> SupportedTypes {
  507. get { yield return typeofCultureInfo; }
  508. }
  509. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  510. throw new NotSupportedException ();
  511. }
  512. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  513. CultureInfo ci = (CultureInfo)obj;
  514. if (ci == null)
  515. return null;
  516. Dictionary<string, object> d = new Dictionary<string, object> ();
  517. d.Add ("name", ci.Name);
  518. d.Add ("numberFormat", ci.NumberFormat);
  519. d.Add ("dateTimeFormat", ci.DateTimeFormat);
  520. return d;
  521. }
  522. }
  523. public class ListItemCollectionConverter : JavaScriptConverter
  524. {
  525. public override IEnumerable<Type> SupportedTypes {
  526. //Define the ListItemCollection as a supported type.
  527. get { return new ReadOnlyCollection<Type> (new Type [] { typeof (ListItemCollection) }); }
  528. }
  529. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  530. ListItemCollection listType = obj as ListItemCollection;
  531. if (listType != null) {
  532. // Create the representation.
  533. Dictionary<string, object> result = new Dictionary<string, object> ();
  534. ArrayList itemsList = new ArrayList ();
  535. foreach (ListItem item in listType) {
  536. //Add each entry to the dictionary.
  537. Dictionary<string, object> listDict = new Dictionary<string, object> ();
  538. listDict.Add ("Value", item.Value);
  539. listDict.Add ("Text", item.Text);
  540. itemsList.Add (listDict);
  541. }
  542. result ["List"] = itemsList;
  543. return result;
  544. }
  545. return new Dictionary<string, object> ();
  546. }
  547. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  548. if (dictionary == null)
  549. throw new ArgumentNullException ("dictionary");
  550. if (type == typeof (ListItemCollection)) {
  551. // Create the instance to deserialize into.
  552. ListItemCollection list = new ListItemCollection ();
  553. // Deserialize the ListItemCollection's items.
  554. ArrayList itemsList = (ArrayList) dictionary ["List"];
  555. for (int i = 0; i < itemsList.Count; i++)
  556. list.Add (serializer.ConvertToType<ListItem> (itemsList [i]));
  557. return list;
  558. }
  559. return null;
  560. }
  561. }
  562. [Test]
  563. public void DeserializeObject () {
  564. object o = new JavaScriptSerializer ().DeserializeObject ("{\"Numeric\":0,\"Array\":[true,false,0]}");
  565. Assert.IsNotNull (o as Dictionary<string, object>, "type");
  566. Dictionary<string, object> dictionary = (Dictionary<string, object>) o;
  567. Assert.AreEqual (0, (int) dictionary ["Numeric"], "Numeric");
  568. Assert.IsNotNull (dictionary ["Array"] as object [], "Array type");
  569. object [] array = (object []) dictionary ["Array"];
  570. Assert.AreEqual (true, (bool) array [0], "array [0]");
  571. Assert.AreEqual (false, (bool) array [1], "array [1]");
  572. Assert.AreEqual (0, (int) array [2], "array [2]");
  573. }
  574. [Test]
  575. public void DeserializeObject2 ()
  576. {
  577. JavaScriptSerializer ser = new JavaScriptSerializer ();
  578. Y y = new Y ();
  579. string s = ser.Serialize (y);
  580. object y2 = ser.DeserializeObject (s);
  581. Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
  582. }
  583. [Test]
  584. public void DeserializeObject3 ()
  585. {
  586. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver());
  587. Y y = new Y ();
  588. string s = ser.Serialize (y);
  589. object y2 = ser.DeserializeObject (s);
  590. Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
  591. }
  592. [Test]
  593. public void DeserializeObject4 ()
  594. {
  595. JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver());
  596. Y y = new Y ();
  597. string s = ser.Serialize (y);
  598. object y2 = ser.DeserializeObject (s);
  599. Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
  600. Assert.AreEqual (1, CustomResolver.ResolvedIds.Count, "ResolvedIds Count");
  601. Assert.AreEqual ("Y", CustomResolver.ResolvedIds [0], "ResolvedIds.Y");
  602. Assert.AreEqual (1, CustomResolver.ResolvedTypes.Count, "ResolvedTypes Count");
  603. Assert.AreEqual ("Y", CustomResolver.ResolvedTypes [0], "ResolvedTypes.Y");
  604. }
  605. [Test]
  606. [ExpectedException(typeof(ArgumentNullException))]
  607. public void SerializeWithResolverDeserializeWithout ()
  608. {
  609. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  610. Y y = new Y ();
  611. string s = ser.Serialize (y);
  612. ser = new JavaScriptSerializer ();
  613. object y2 = ser.DeserializeObject (s);
  614. }
  615. [Test]
  616. public void SerializeWithoutResolverDeserializeWith ()
  617. {
  618. JavaScriptSerializer ser = new JavaScriptSerializer ();
  619. Y y = new Y ();
  620. string s = ser.Serialize (y);
  621. ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  622. object y2 = ser.DeserializeObject (s);
  623. Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
  624. }
  625. class B
  626. {
  627. public int v1 = 15;
  628. public string s1 = "s1";
  629. }
  630. class D : B
  631. {
  632. public int v2 = 16;
  633. public string s2 = "s2";
  634. }
  635. class C
  636. {
  637. public B b1 = new B ();
  638. public B b2 = new D ();
  639. }
  640. [Test]
  641. public void SerializeDerivedType ()
  642. {
  643. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  644. B b = new D ();
  645. string s = ser.Serialize (b);
  646. B b2 = ser.Deserialize<B> (s);
  647. Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
  648. }
  649. [Test]
  650. public void SerializeDerivedType2 ()
  651. {
  652. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  653. B b = new D ();
  654. string s = ser.Serialize (b);
  655. B b2 = (B)ser.DeserializeObject (s);
  656. Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
  657. }
  658. [Test]
  659. public void SerializeContainedDerivedType ()
  660. {
  661. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  662. C c = new C ();
  663. string s = ser.Serialize (c);
  664. C c2 = ser.Deserialize<C> (s);
  665. Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
  666. Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
  667. }
  668. [Test]
  669. public void SerializeContainedDerivedType2 ()
  670. {
  671. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  672. C c = new C ();
  673. string s = ser.Serialize (c);
  674. C c2 = (C)ser.DeserializeObject (s);
  675. Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
  676. Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
  677. }
  678. [Test]
  679. public void SerializeWithTypeConverter ()
  680. {
  681. JavaScriptSerializer ser = new JavaScriptSerializer ();
  682. MyUri uri = new MyUri ("http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d", UriKind.RelativeOrAbsolute);
  683. string s = ser.Serialize (uri);
  684. MyUri uri2 = ser.Deserialize<MyUri> (s);
  685. Assert.AreEqual (uri, uri2);
  686. }
  687. [Test]
  688. public void SerializeWithTypeConverter2 ()
  689. {
  690. JavaScriptSerializer ser = new JavaScriptSerializer ();
  691. MyPointContainer pc = new MyPointContainer(new Point(15, 16));
  692. string s = ser.Serialize(pc);
  693. MyPointContainer pc2 = ser.Deserialize<MyPointContainer>(s);
  694. }
  695. [Test]
  696. public void MaxJsonLengthDeserializeObject ()
  697. {
  698. JavaScriptSerializer ser = new JavaScriptSerializer ();
  699. ser.MaxJsonLength = 16;
  700. object o = ser.DeserializeObject ("{s:'1234567890'}");
  701. }
  702. [Test]
  703. [ExpectedException(typeof(ArgumentException))]
  704. public void MaxJsonLengthDeserializeObjectToLong ()
  705. {
  706. JavaScriptSerializer ser = new JavaScriptSerializer ();
  707. ser.MaxJsonLength = 15;
  708. object o = ser.DeserializeObject ("{s:'1234567890'}");
  709. }
  710. [Test]
  711. public void MaxJsonLengthSerialize ()
  712. {
  713. JavaScriptSerializer ser = new JavaScriptSerializer ();
  714. ser.MaxJsonLength = 9;
  715. Y y = new Y ();
  716. string s = ser.Serialize (y);
  717. }
  718. [Test]
  719. [ExpectedException (typeof (InvalidOperationException))]
  720. public void MaxJsonLengthSerializeToLong ()
  721. {
  722. JavaScriptSerializer ser = new JavaScriptSerializer ();
  723. ser.MaxJsonLength = 8;
  724. Y y = new Y ();
  725. string s = ser.Serialize (y);
  726. }
  727. [Test]
  728. public void RecursionLimitDeserialize1 ()
  729. {
  730. JavaScriptSerializer ser = new JavaScriptSerializer ();
  731. ser.RecursionLimit = 3;
  732. YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
  733. }
  734. [Test]
  735. public void RecursionLimitDeserialize2 ()
  736. {
  737. JavaScriptSerializer ser = new JavaScriptSerializer ();
  738. ser.RecursionLimit = 2;
  739. YY yy = ser.Deserialize<YY> ("{\"Y1\":{},\"Y2\":{}}");
  740. }
  741. [Test]
  742. public void RecursionLimitDeserialize3 ()
  743. {
  744. JavaScriptSerializer ser = new JavaScriptSerializer ();
  745. ser.RecursionLimit = 1;
  746. object o = ser.DeserializeObject ("\"xxx\"");
  747. }
  748. [Test]
  749. [ExpectedException(typeof(ArgumentException))]
  750. public void RecursionLimitDeserializeToDeep ()
  751. {
  752. JavaScriptSerializer ser = new JavaScriptSerializer ();
  753. ser.RecursionLimit = 2;
  754. YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
  755. }
  756. [Test]
  757. public void RecursionLimitSerialize ()
  758. {
  759. JavaScriptSerializer ser = new JavaScriptSerializer ();
  760. ser.RecursionLimit = 3;
  761. YY yy = new YY();
  762. string s = ser.Serialize (yy);
  763. }
  764. [Test]
  765. [ExpectedException(typeof(ArgumentException))]
  766. public void RecursionLimitSerializeToDeep ()
  767. {
  768. JavaScriptSerializer ser = new JavaScriptSerializer ();
  769. ser.RecursionLimit = 2;
  770. YY yy = new YY ();
  771. string s = ser.Serialize (yy);
  772. }
  773. [Test]
  774. public void RecursionLimitSerialize2 ()
  775. {
  776. JavaScriptSerializer ser = new JavaScriptSerializer ();
  777. ser.RecursionLimit = 2;
  778. YY yy = new YY ();
  779. StringBuilder b = new StringBuilder ();
  780. bool caughtException = false;
  781. try {
  782. ser.Serialize (yy, b);
  783. }
  784. catch {
  785. caughtException = true;
  786. }
  787. Assert.IsTrue (caughtException, "RecursionLimitSerialize2 Expected an exception!");
  788. Assert.AreEqual ("{\"Y1\":{\"BB\":", b.ToString (), "RecursionLimitSerialize2");
  789. }
  790. [Test]
  791. public void SimpleTypeResolver ()
  792. {
  793. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  794. YY yy = new YY ();
  795. string s = ser.Serialize (yy);
  796. string expected = String.Format("\"__type\":\"{0}\"", yy.GetType().AssemblyQualifiedName);
  797. Assert.IsTrue (s.Contains (expected), "YY: expected {0} to contain {1}", s, expected);
  798. expected = String.Format ("\"__type\":\"{0}\"", yy.Y1.GetType ().AssemblyQualifiedName);
  799. Assert.IsTrue (s.Contains (expected), "Y: expected {0} to contain {1}", s, expected);
  800. }
  801. public class CustomResolver : JavaScriptTypeResolver
  802. {
  803. public CustomResolver ()
  804. {
  805. Reset ();
  806. }
  807. public override Type ResolveType (string id)
  808. {
  809. ResolvedIds.Add (id);
  810. switch (id) {
  811. case "YY":
  812. return typeof(YY);
  813. case "Y":
  814. return typeof (Y);
  815. case "X":
  816. return typeof (X);
  817. case "int":
  818. return typeof (int);
  819. case "long":
  820. return typeof (long);
  821. case "string":
  822. return typeof (string);
  823. case "point":
  824. return typeof(Point);
  825. }
  826. return null;
  827. }
  828. public override string ResolveTypeId (Type type)
  829. {
  830. if (type == null) {
  831. throw new ArgumentNullException ("type");
  832. }
  833. ResolvedTypes.Add (type.Name);
  834. if (type == typeof (YY))
  835. return "YY";
  836. if (type == typeof (Y))
  837. return "Y";
  838. if (type == typeof (X))
  839. return "X";
  840. if (type == typeof (int))
  841. return "int";
  842. if (type == typeof (long))
  843. return "long";
  844. if (type == typeof (string))
  845. return "string";
  846. if (type == typeof(Point))
  847. return "point";
  848. return null;
  849. }
  850. public static List<string> ResolvedTypes {
  851. get {
  852. if (resolvedTypes == null) {
  853. resolvedTypes = new List<string> ();
  854. }
  855. return resolvedTypes;
  856. }
  857. }
  858. public static List<string> ResolvedIds {
  859. get {
  860. if (resolvedIds == null) {
  861. resolvedIds = new List<string> ();
  862. }
  863. return resolvedIds;
  864. }
  865. }
  866. public static void Reset ()
  867. {
  868. resolvedIds = null;
  869. resolvedTypes = null;
  870. }
  871. private static List<string> resolvedTypes;
  872. private static List<string> resolvedIds;
  873. }
  874. [Test]
  875. [NUnit.Framework.Category ("NotWorking")]
  876. public void CustomTypeResolver ()
  877. {
  878. JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver ());
  879. X x = new X ();
  880. x.Init ();
  881. string s = ser.Serialize (x);
  882. CustomResolver.Reset ();
  883. X x1 = (X) ser.DeserializeObject (s);
  884. Assert.IsTrue (x.Equals (x1), "x != x1");
  885. CustomResolver.Reset ();
  886. X x2 = ser.Deserialize<X> (s);
  887. Assert.IsTrue (x.Equals (x2), "x != x2");
  888. }
  889. [Test]
  890. public void InfinityAndNaN ()
  891. {
  892. JavaScriptSerializer ser = new JavaScriptSerializer ();
  893. double nan = Double.NaN;
  894. string s = ser.Serialize (nan);
  895. Assert.AreEqual (s, "NaN", "#A1");
  896. nan = (double)ser.DeserializeObject (s);
  897. Assert.AreEqual (Double.NaN, nan, "#A2");
  898. nan = (double)ser.Deserialize <double> (s);
  899. Assert.AreEqual (Double.NaN, nan, "#A3");
  900. double infinity = Double.PositiveInfinity;
  901. s = ser.Serialize (infinity);
  902. Assert.AreEqual (s, "Infinity", "#B1");
  903. infinity = (double)ser.DeserializeObject (s);
  904. Assert.AreEqual (Double.PositiveInfinity, infinity, "#B2");
  905. infinity = ser.Deserialize <double> (s);
  906. Assert.AreEqual (Double.PositiveInfinity, infinity, "#B3");
  907. infinity = Double.NegativeInfinity;
  908. s = ser.Serialize (infinity);
  909. Assert.AreEqual (s, "-Infinity", "#C1");
  910. infinity = (double)ser.DeserializeObject (s);
  911. Assert.AreEqual (Double.NegativeInfinity, infinity, "#C2");
  912. infinity = ser.Deserialize <double> (s);
  913. Assert.AreEqual (Double.NegativeInfinity, infinity, "#C3");
  914. var dict = new Dictionary <string, object> ();
  915. dict.Add ("A", Double.NaN);
  916. dict.Add ("B", Double.PositiveInfinity);
  917. dict.Add ("C", Double.NegativeInfinity);
  918. s = ser.Serialize (dict);
  919. Assert.AreEqual ("{\"A\":NaN,\"B\":Infinity,\"C\":-Infinity}", s, "#D1");
  920. dict = (Dictionary <string, object>)ser.DeserializeObject (s);
  921. Assert.AreEqual (Double.NaN, dict ["A"], "#D2");
  922. Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D3");
  923. Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D4");
  924. dict = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object>> (s);
  925. Assert.AreEqual (Double.NaN, dict ["A"], "#D5");
  926. Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D6");
  927. Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D7");
  928. var arr = new ArrayList () {
  929. Double.NaN,
  930. Double.PositiveInfinity,
  931. Double.NegativeInfinity};
  932. s = ser.Serialize (arr);
  933. Assert.AreEqual ("[NaN,Infinity,-Infinity]", s, "#E1");
  934. object[] arr2 = (object[])ser.DeserializeObject (s);
  935. Assert.AreEqual (3, arr2.Length, "#E2");
  936. Assert.AreEqual (Double.NaN, arr2 [0], "#E3");
  937. Assert.AreEqual (Double.PositiveInfinity, arr2 [1], "#E4");
  938. Assert.AreEqual (Double.NegativeInfinity, arr2 [2], "#E5");
  939. arr = ser.Deserialize <ArrayList> (s);
  940. Assert.AreEqual (3, arr.Count, "#E6");
  941. Assert.AreEqual (Double.NaN, arr [0], "#E7");
  942. Assert.AreEqual (Double.PositiveInfinity, arr [1], "#E8");
  943. Assert.AreEqual (Double.NegativeInfinity, arr [2], "#E9");
  944. }
  945. [Test]
  946. public void StandalonePrimitives ()
  947. {
  948. JavaScriptSerializer ser = new JavaScriptSerializer ();
  949. object o;
  950. int i;
  951. o = ser.DeserializeObject ("1");
  952. Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A1");
  953. i = (int)o;
  954. Assert.AreEqual (1, i, "#A2");
  955. o =ser.DeserializeObject ("-1");
  956. Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A3");
  957. i = (int)o;
  958. Assert.AreEqual (-1, i, "#A4");
  959. o = ser.DeserializeObject ("2147483649");
  960. Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B1");
  961. long l = (long)o;
  962. Assert.AreEqual (2147483649, l, "#B2");
  963. o = ser.DeserializeObject ("-2147483649");
  964. Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B3");
  965. l = (long)o;
  966. Assert.AreEqual (-2147483649, l, "#B4");
  967. o = ser.DeserializeObject ("9223372036854775808");
  968. Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C1");
  969. decimal d = (decimal)o;
  970. Assert.AreEqual (9223372036854775808m, d, "#C2");
  971. o = ser.DeserializeObject ("-9223372036854775809");
  972. Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C3");
  973. d = (decimal)o;
  974. Assert.AreEqual (-9223372036854775809m, d, "#C4");
  975. o = ser.DeserializeObject ("79228162514264337593543950336");
  976. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D1");
  977. double db = (double)o;
  978. Assert.AreEqual (79228162514264337593543950336.0, db, "#D2");
  979. o = ser.DeserializeObject ("-79228162514264337593543950336");
  980. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D3");
  981. db = (double)o;
  982. Assert.AreEqual (-79228162514264337593543950336.0, db, "#D4");
  983. o = ser.DeserializeObject ("\"test string\"");
  984. Assert.AreEqual (typeof (global::System.String), o.GetType (), "#E1");
  985. string s = (string)o;
  986. Assert.AreEqual ("test string", s, "#E2");
  987. o = ser.DeserializeObject ("true");
  988. Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F1");
  989. bool b = (bool)o;
  990. Assert.AreEqual (true, b, "#F2");
  991. o = ser.DeserializeObject ("false");
  992. Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F3");
  993. b = (bool)o;
  994. Assert.AreEqual (false, b, "#F4");
  995. o = ser.DeserializeObject ("-1.7976931348623157E+308");
  996. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G1");
  997. db = (double)o;
  998. Assert.AreEqual (Double.MinValue, db, "#G2");
  999. o = ser.DeserializeObject ("1.7976931348623157E+308");
  1000. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G3");
  1001. db = (double)o;
  1002. Assert.AreEqual (Double.MaxValue, db, "#G4");
  1003. }
  1004. class SomeDict : IDictionary<string, object>
  1005. {
  1006. void IDictionary<string, object>.Add (string key, object value) {
  1007. throw new NotSupportedException ();
  1008. }
  1009. bool IDictionary<string, object>.ContainsKey (string key) {
  1010. throw new NotSupportedException ();
  1011. }
  1012. ICollection<string> IDictionary<string, object>.Keys {
  1013. get { throw new NotSupportedException (); }
  1014. }
  1015. bool IDictionary<string, object>.Remove (string key) {
  1016. throw new NotSupportedException ();
  1017. }
  1018. bool IDictionary<string, object>.TryGetValue (string key, out object value) {
  1019. throw new NotSupportedException ();
  1020. }
  1021. ICollection<object> IDictionary<string, object>.Values {
  1022. get { throw new NotSupportedException (); }
  1023. }
  1024. object IDictionary<string, object>.this [string key] {
  1025. get { throw new NotSupportedException (); }
  1026. set { throw new NotSupportedException (); }
  1027. }
  1028. void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item) {
  1029. throw new NotSupportedException ();
  1030. }
  1031. void ICollection<KeyValuePair<string, object>>.Clear () {
  1032. throw new NotSupportedException ();
  1033. }
  1034. bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item) {
  1035. throw new NotSupportedException ();
  1036. }
  1037. void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex) {
  1038. throw new NotSupportedException ();
  1039. }
  1040. int ICollection<KeyValuePair<string, object>>.Count {
  1041. get { throw new NotSupportedException (); }
  1042. }
  1043. bool ICollection<KeyValuePair<string, object>>.IsReadOnly {
  1044. get { throw new NotSupportedException (); }
  1045. }
  1046. bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item) {
  1047. throw new NotSupportedException ();
  1048. }
  1049. IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator () {
  1050. return GetEnumerator ();
  1051. }
  1052. IEnumerator IEnumerable.GetEnumerator () {
  1053. return ((IEnumerable<KeyValuePair<string, object>>) this).GetEnumerator ();
  1054. }
  1055. protected IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
  1056. yield return new KeyValuePair<string, object> ("hello", "world");
  1057. }
  1058. }
  1059. [Test] //bug #424704
  1060. public void NonGenericClassImplementingClosedGenericIDictionary ()
  1061. {
  1062. JavaScriptSerializer ser = new JavaScriptSerializer ();
  1063. SomeDict dictIn = new SomeDict ();
  1064. string s = ser.Serialize (dictIn);
  1065. Dictionary<string, object> dictOut = ser.Deserialize<Dictionary<string, object>> (s);
  1066. Assert.AreEqual (dictOut.Count, 1, "#1");
  1067. Assert.AreEqual (dictOut["hello"], "world", "#2");
  1068. }
  1069. }
  1070. }