JavaScriptSerializerTest.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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 TestDeserialize () {
  318. JavaScriptSerializer ser = new JavaScriptSerializer ();
  319. Assert.IsNull (ser.Deserialize<X> (""));
  320. X s = new X ();
  321. s.Init ();
  322. string x = ser.Serialize (s);
  323. X n = ser.Deserialize<X> (x);
  324. Assert.AreEqual (s, n);
  325. //string json = "\\uFF56";
  326. //string result = ser.Deserialize<string> (json);
  327. //Assert.AreEqual ("\uFF56", result);
  328. //object oo = ser.DeserializeObject ("{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}");
  329. }
  330. [Test]
  331. public void TestDeserializeTypeResolver ()
  332. {
  333. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  334. X x = new X ();
  335. x.Init ();
  336. string s = ser.Serialize (x);
  337. X x2 = ser.Deserialize<X> (s);
  338. Assert.AreEqual (x, x2);
  339. }
  340. [Test]
  341. public void TestDeserializeBugs () {
  342. JavaScriptSerializer ser = new JavaScriptSerializer ();
  343. bug s = new bug ();
  344. s.Init ();
  345. string x = ser.Serialize (s);
  346. bug n = ser.Deserialize<bug> (x);
  347. Assert.AreEqual (s, n);
  348. // Should check correctness with .Net GA:
  349. //js = ser.Serialize (Color.Red);
  350. //Color ccc = ser.Deserialize<Color> (js);
  351. //string xml = @"<root><node attr=""xxx""/></root>";
  352. //XmlDocument doc = new XmlDocument ();
  353. //doc.LoadXml (xml);
  354. //string js = ser.Serialize (doc);
  355. //DataTable table = new DataTable();
  356. //table.Columns.Add ("col1", typeof (int));
  357. //table.Columns.Add ("col2", typeof (float));
  358. //table.Rows.Add (1, 1f);
  359. //table.Rows.Add (234234, 2.4f);
  360. //string js = ser.Serialize (table);
  361. }
  362. static void FieldsEqual (object expected, object actual) {
  363. Assert.AreEqual (expected.GetType (), actual.GetType ());
  364. FieldInfo [] infos = expected.GetType ().GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
  365. foreach (FieldInfo info in infos) {
  366. object value1 = info.GetValue (expected);
  367. object value2 = info.GetValue (actual);
  368. if (value1 is IEnumerable) {
  369. IEnumerator yenum = ((IEnumerable) value2).GetEnumerator ();
  370. int index = -1;
  371. foreach (object x in (IEnumerable) value1) {
  372. if (!yenum.MoveNext ())
  373. Assert.Fail (info.Name + " index:" + index);
  374. index++;
  375. if (x is DictionaryEntry) {
  376. DictionaryEntry entry = (DictionaryEntry)x;
  377. IDictionary dict = (IDictionary) value2;
  378. Assert.AreEqual (entry.Value, dict [entry.Key], info.Name + ", key:" + entry.Key);
  379. }
  380. else
  381. Assert.AreEqual (x, yenum.Current, info.Name + ", index:" + index);
  382. }
  383. Assert.IsFalse (yenum.MoveNext (), info.Name);
  384. continue;
  385. }
  386. Assert.AreEqual (value1, value2, info.Name);
  387. }
  388. }
  389. [Test]
  390. [ExpectedException (typeof (ArgumentNullException))]
  391. public void TestDeserialize1 () {
  392. JavaScriptSerializer ser = new JavaScriptSerializer ();
  393. ser.Deserialize<string> (null);
  394. }
  395. [Test]
  396. [ExpectedException (typeof (ArgumentNullException))]
  397. public void TestDeserializeNullConverter () {
  398. JavaScriptSerializer ser = new JavaScriptSerializer ();
  399. ser.RegisterConverters (null);
  400. }
  401. [Test]
  402. public void TestDeserializeConverter () {
  403. JavaScriptSerializer ser = new JavaScriptSerializer ();
  404. List<JavaScriptConverter> list = new List<JavaScriptConverter> ();
  405. list.Add (new MyJavaScriptConverter ());
  406. list.Add (new CultureInfoConverter ());
  407. ser.RegisterConverters (list);
  408. string result = ser.Serialize (new X [] { new X (), new X () });
  409. Assert.AreEqual ("{\"0\":1,\"1\":2}", result);
  410. result = ser.Serialize (Thread.CurrentThread.CurrentCulture);
  411. }
  412. [Test]
  413. public void TestDeserializeConverter1 () {
  414. JavaScriptSerializer serializer = new JavaScriptSerializer ();
  415. serializer.RegisterConverters (new JavaScriptConverter [] {
  416. new ListItemCollectionConverter() });
  417. ListBox ListBox1 = new ListBox ();
  418. ListBox1.Items.Add ("a1");
  419. ListBox1.Items.Add ("a2");
  420. ListBox1.Items.Add ("a3");
  421. string x = serializer.Serialize (ListBox1.Items);
  422. ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection> (x);
  423. Assert.AreEqual (3, recoveredList.Count);
  424. }
  425. [Test]
  426. public void TestSerialize1 () {
  427. JavaScriptSerializer ser = new JavaScriptSerializer ();
  428. Assert.AreEqual("null", ser.Serialize(null));
  429. string js = ser.Serialize (1234);
  430. Assert.AreEqual ("1234", js);
  431. Assert.AreEqual (1234, ser.Deserialize<int> (js));
  432. js = ser.Serialize (1.1);
  433. Assert.AreEqual ("1.1", js);
  434. Assert.AreEqual (1.1f, ser.Deserialize<float> (js));
  435. char [] chars = "faskjhfasd0981234".ToCharArray ();
  436. js = ser.Serialize (chars);
  437. char[] actual = ser.Deserialize<char[]> (js);
  438. Assert.AreEqual (chars.Length, actual.Length);
  439. for (int i = 0; i < chars.Length; i++)
  440. Assert.AreEqual (chars[i], actual[i]);
  441. 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{|}~""";
  442. 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{|}~";
  443. string serRes = ser.Serialize (data);
  444. Assert.AreEqual (expected, serRes);
  445. string deserRes = ser.Deserialize<string> (serRes);
  446. Assert.AreEqual (data, deserRes);
  447. }
  448. [Test]
  449. [ExpectedException (typeof (ArgumentNullException))]
  450. [Category ("NotDotNet")]
  451. public void TestSerialize2 () {
  452. JavaScriptSerializer ser = new JavaScriptSerializer ();
  453. ser.Serialize ("aaa", null);
  454. }
  455. static readonly long InitialJavaScriptDateTicks = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
  456. [Test]
  457. public void TestSerializeDate () {
  458. JavaScriptSerializer ser = new JavaScriptSerializer ();
  459. DateTime now = new DateTime (633213894056010000L);
  460. string actual = ser.Serialize (now);
  461. DateTime dateTime = now.ToUniversalTime ();
  462. long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long) 10000;
  463. object dd = ser.DeserializeObject (@"""\/Datte(" + javaScriptTicks + @")\/""");
  464. Assert.AreEqual (@"""\/Date(" + javaScriptTicks + @")\/""", actual);
  465. Assert.AreEqual (now.ToUniversalTime(), ser.DeserializeObject (actual));
  466. }
  467. [Test]
  468. public void TestSerializeEnum () {
  469. JavaScriptSerializer ser = new JavaScriptSerializer ();
  470. string result = ser.Serialize (MyEnum.BBB);
  471. Assert.AreEqual ("1", result);
  472. Assert.AreEqual (MyEnum.BBB, ser.Deserialize<MyEnum> (result));
  473. }
  474. class MyJavaScriptConverter : JavaScriptConverter
  475. {
  476. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  477. throw new Exception ("The method or operation is not implemented.");
  478. }
  479. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  480. Array a = (Array) obj;
  481. Dictionary<string, object> d = new Dictionary<string, object> ();
  482. d.Add ("0", 1);
  483. d.Add ("1", 2);
  484. return d;
  485. //throw new Exception ("The method or operation is not implemented.");
  486. }
  487. public override IEnumerable<Type> SupportedTypes {
  488. get {
  489. yield return typeof (X[]);
  490. }
  491. }
  492. }
  493. sealed class CultureInfoConverter : JavaScriptConverter
  494. {
  495. static readonly Type typeofCultureInfo = typeof (CultureInfo);
  496. public override IEnumerable<Type> SupportedTypes {
  497. get { yield return typeofCultureInfo; }
  498. }
  499. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  500. throw new NotSupportedException ();
  501. }
  502. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  503. CultureInfo ci = (CultureInfo)obj;
  504. if (ci == null)
  505. return null;
  506. Dictionary<string, object> d = new Dictionary<string, object> ();
  507. d.Add ("name", ci.Name);
  508. d.Add ("numberFormat", ci.NumberFormat);
  509. d.Add ("dateTimeFormat", ci.DateTimeFormat);
  510. return d;
  511. }
  512. }
  513. public class ListItemCollectionConverter : JavaScriptConverter
  514. {
  515. public override IEnumerable<Type> SupportedTypes {
  516. //Define the ListItemCollection as a supported type.
  517. get { return new ReadOnlyCollection<Type> (new Type [] { typeof (ListItemCollection) }); }
  518. }
  519. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  520. ListItemCollection listType = obj as ListItemCollection;
  521. if (listType != null) {
  522. // Create the representation.
  523. Dictionary<string, object> result = new Dictionary<string, object> ();
  524. ArrayList itemsList = new ArrayList ();
  525. foreach (ListItem item in listType) {
  526. //Add each entry to the dictionary.
  527. Dictionary<string, object> listDict = new Dictionary<string, object> ();
  528. listDict.Add ("Value", item.Value);
  529. listDict.Add ("Text", item.Text);
  530. itemsList.Add (listDict);
  531. }
  532. result ["List"] = itemsList;
  533. return result;
  534. }
  535. return new Dictionary<string, object> ();
  536. }
  537. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  538. if (dictionary == null)
  539. throw new ArgumentNullException ("dictionary");
  540. if (type == typeof (ListItemCollection)) {
  541. // Create the instance to deserialize into.
  542. ListItemCollection list = new ListItemCollection ();
  543. // Deserialize the ListItemCollection's items.
  544. ArrayList itemsList = (ArrayList) dictionary ["List"];
  545. for (int i = 0; i < itemsList.Count; i++)
  546. list.Add (serializer.ConvertToType<ListItem> (itemsList [i]));
  547. return list;
  548. }
  549. return null;
  550. }
  551. }
  552. [Test]
  553. public void DeserializeObject () {
  554. object o = new JavaScriptSerializer ().DeserializeObject ("{\"Numeric\":0,\"Array\":[true,false,0]}");
  555. Assert.IsNotNull (o as Dictionary<string, object>, "type");
  556. Dictionary<string, object> dictionary = (Dictionary<string, object>) o;
  557. Assert.AreEqual (0, (int) dictionary ["Numeric"], "Numeric");
  558. Assert.IsNotNull (dictionary ["Array"] as object [], "Array type");
  559. object [] array = (object []) dictionary ["Array"];
  560. Assert.AreEqual (true, (bool) array [0], "array [0]");
  561. Assert.AreEqual (false, (bool) array [1], "array [1]");
  562. Assert.AreEqual (0, (int) array [2], "array [2]");
  563. }
  564. [Test]
  565. public void DeserializeObject2 ()
  566. {
  567. JavaScriptSerializer ser = new JavaScriptSerializer ();
  568. Y y = new Y ();
  569. string s = ser.Serialize (y);
  570. object y2 = ser.DeserializeObject (s);
  571. Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
  572. }
  573. [Test]
  574. public void DeserializeObject3 ()
  575. {
  576. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver());
  577. Y y = new Y ();
  578. string s = ser.Serialize (y);
  579. object y2 = ser.DeserializeObject (s);
  580. Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
  581. }
  582. [Test]
  583. public void DeserializeObject4 ()
  584. {
  585. JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver());
  586. Y y = new Y ();
  587. string s = ser.Serialize (y);
  588. object y2 = ser.DeserializeObject (s);
  589. Assert.AreEqual (typeof (Y), y2.GetType (), "DeserializeObject to Dictionary");
  590. Assert.AreEqual (1, CustomResolver.ResolvedIds.Count, "ResolvedIds Count");
  591. Assert.AreEqual ("Y", CustomResolver.ResolvedIds [0], "ResolvedIds.Y");
  592. Assert.AreEqual (1, CustomResolver.ResolvedTypes.Count, "ResolvedTypes Count");
  593. Assert.AreEqual ("Y", CustomResolver.ResolvedTypes [0], "ResolvedTypes.Y");
  594. }
  595. [Test]
  596. [ExpectedException(typeof(ArgumentNullException))]
  597. public void SerializeWithResolverDeserializeWithout ()
  598. {
  599. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  600. Y y = new Y ();
  601. string s = ser.Serialize (y);
  602. ser = new JavaScriptSerializer ();
  603. object y2 = ser.DeserializeObject (s);
  604. }
  605. [Test]
  606. public void SerializeWithoutResolverDeserializeWith ()
  607. {
  608. JavaScriptSerializer ser = new JavaScriptSerializer ();
  609. Y y = new Y ();
  610. string s = ser.Serialize (y);
  611. ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  612. object y2 = ser.DeserializeObject (s);
  613. Assert.AreEqual (typeof (Dictionary<string, object>), y2.GetType (), "DeserializeObject to Dictionary");
  614. }
  615. class B
  616. {
  617. public int v1 = 15;
  618. public string s1 = "s1";
  619. }
  620. class D : B
  621. {
  622. public int v2 = 16;
  623. public string s2 = "s2";
  624. }
  625. class C
  626. {
  627. public B b1 = new B ();
  628. public B b2 = new D ();
  629. }
  630. [Test]
  631. public void SerializeDerivedType ()
  632. {
  633. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  634. B b = new D ();
  635. string s = ser.Serialize (b);
  636. B b2 = ser.Deserialize<B> (s);
  637. Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
  638. }
  639. [Test]
  640. public void SerializeDerivedType2 ()
  641. {
  642. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  643. B b = new D ();
  644. string s = ser.Serialize (b);
  645. B b2 = (B)ser.DeserializeObject (s);
  646. Assert.AreEqual (typeof (D), b2.GetType (), "Deserialize Derived Type");
  647. }
  648. [Test]
  649. public void SerializeContainedDerivedType ()
  650. {
  651. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  652. C c = new C ();
  653. string s = ser.Serialize (c);
  654. C c2 = ser.Deserialize<C> (s);
  655. Assert.AreEqual (typeof (C), c2.GetType (), "Deserialize Derived Type");
  656. Assert.AreEqual (typeof (D), c2.b2.GetType (), "Deserialize Derived Type");
  657. }
  658. [Test]
  659. public void SerializeContainedDerivedType2 ()
  660. {
  661. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  662. C c = new C ();
  663. string s = ser.Serialize (c);
  664. C c2 = (C)ser.DeserializeObject (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 SerializeWithTypeConverter ()
  670. {
  671. JavaScriptSerializer ser = new JavaScriptSerializer ();
  672. MyUri uri = new MyUri ("http://kostat@mainsoft/adfasdf/asdfasdf.aspx/asda/ads?a=b&c=d", UriKind.RelativeOrAbsolute);
  673. string s = ser.Serialize (uri);
  674. MyUri uri2 = ser.Deserialize<MyUri> (s);
  675. Assert.AreEqual (uri, uri2);
  676. }
  677. [Test]
  678. public void SerializeWithTypeConverter2 ()
  679. {
  680. JavaScriptSerializer ser = new JavaScriptSerializer ();
  681. MyPointContainer pc = new MyPointContainer(new Point(15, 16));
  682. string s = ser.Serialize(pc);
  683. MyPointContainer pc2 = ser.Deserialize<MyPointContainer>(s);
  684. }
  685. [Test]
  686. public void MaxJsonLengthDeserializeObject ()
  687. {
  688. JavaScriptSerializer ser = new JavaScriptSerializer ();
  689. ser.MaxJsonLength = 16;
  690. object o = ser.DeserializeObject ("{s:'1234567890'}");
  691. }
  692. [Test]
  693. [ExpectedException(typeof(ArgumentException))]
  694. public void MaxJsonLengthDeserializeObjectToLong ()
  695. {
  696. JavaScriptSerializer ser = new JavaScriptSerializer ();
  697. ser.MaxJsonLength = 15;
  698. object o = ser.DeserializeObject ("{s:'1234567890'}");
  699. }
  700. [Test]
  701. public void MaxJsonLengthSerialize ()
  702. {
  703. JavaScriptSerializer ser = new JavaScriptSerializer ();
  704. ser.MaxJsonLength = 9;
  705. Y y = new Y ();
  706. string s = ser.Serialize (y);
  707. }
  708. [Test]
  709. [ExpectedException (typeof (InvalidOperationException))]
  710. public void MaxJsonLengthSerializeToLong ()
  711. {
  712. JavaScriptSerializer ser = new JavaScriptSerializer ();
  713. ser.MaxJsonLength = 8;
  714. Y y = new Y ();
  715. string s = ser.Serialize (y);
  716. }
  717. [Test]
  718. public void RecursionLimitDeserialize1 ()
  719. {
  720. JavaScriptSerializer ser = new JavaScriptSerializer ();
  721. ser.RecursionLimit = 3;
  722. YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
  723. }
  724. [Test]
  725. public void RecursionLimitDeserialize2 ()
  726. {
  727. JavaScriptSerializer ser = new JavaScriptSerializer ();
  728. ser.RecursionLimit = 2;
  729. YY yy = ser.Deserialize<YY> ("{\"Y1\":{},\"Y2\":{}}");
  730. }
  731. [Test]
  732. public void RecursionLimitDeserialize3 ()
  733. {
  734. JavaScriptSerializer ser = new JavaScriptSerializer ();
  735. ser.RecursionLimit = 1;
  736. object o = ser.DeserializeObject ("\"xxx\"");
  737. }
  738. [Test]
  739. [ExpectedException(typeof(ArgumentException))]
  740. public void RecursionLimitDeserializeToDeep ()
  741. {
  742. JavaScriptSerializer ser = new JavaScriptSerializer ();
  743. ser.RecursionLimit = 2;
  744. YY yy = ser.Deserialize<YY> ("{\"Y1\":{\"BB\":10},\"Y2\":{\"BB\":10}}");
  745. }
  746. [Test]
  747. public void RecursionLimitSerialize ()
  748. {
  749. JavaScriptSerializer ser = new JavaScriptSerializer ();
  750. ser.RecursionLimit = 3;
  751. YY yy = new YY();
  752. string s = ser.Serialize (yy);
  753. }
  754. [Test]
  755. [ExpectedException(typeof(ArgumentException))]
  756. public void RecursionLimitSerializeToDeep ()
  757. {
  758. JavaScriptSerializer ser = new JavaScriptSerializer ();
  759. ser.RecursionLimit = 2;
  760. YY yy = new YY ();
  761. string s = ser.Serialize (yy);
  762. }
  763. [Test]
  764. public void RecursionLimitSerialize2 ()
  765. {
  766. JavaScriptSerializer ser = new JavaScriptSerializer ();
  767. ser.RecursionLimit = 2;
  768. YY yy = new YY ();
  769. StringBuilder b = new StringBuilder ();
  770. bool caughtException = false;
  771. try {
  772. ser.Serialize (yy, b);
  773. }
  774. catch {
  775. caughtException = true;
  776. }
  777. Assert.IsTrue (caughtException, "RecursionLimitSerialize2 Expected an exception!");
  778. Assert.AreEqual ("{\"Y1\":{\"BB\":", b.ToString (), "RecursionLimitSerialize2");
  779. }
  780. [Test]
  781. public void SimpleTypeResolver ()
  782. {
  783. JavaScriptSerializer ser = new JavaScriptSerializer (new SimpleTypeResolver ());
  784. YY yy = new YY ();
  785. string s = ser.Serialize (yy);
  786. string expected = String.Format("\"__type\":\"{0}\"", yy.GetType().AssemblyQualifiedName);
  787. Assert.IsTrue (s.Contains (expected), "YY: expected {0} to contain {1}", s, expected);
  788. expected = String.Format ("\"__type\":\"{0}\"", yy.Y1.GetType ().AssemblyQualifiedName);
  789. Assert.IsTrue (s.Contains (expected), "Y: expected {0} to contain {1}", s, expected);
  790. }
  791. public class CustomResolver : JavaScriptTypeResolver
  792. {
  793. public CustomResolver ()
  794. {
  795. Reset ();
  796. }
  797. public override Type ResolveType (string id)
  798. {
  799. ResolvedIds.Add (id);
  800. switch (id) {
  801. case "YY":
  802. return typeof(YY);
  803. case "Y":
  804. return typeof (Y);
  805. case "X":
  806. return typeof (X);
  807. case "int":
  808. return typeof (int);
  809. case "long":
  810. return typeof (long);
  811. case "string":
  812. return typeof (string);
  813. case "point":
  814. return typeof(Point);
  815. }
  816. return null;
  817. }
  818. public override string ResolveTypeId (Type type)
  819. {
  820. if (type == null) {
  821. throw new ArgumentNullException ("type");
  822. }
  823. ResolvedTypes.Add (type.Name);
  824. if (type == typeof (YY))
  825. return "YY";
  826. if (type == typeof (Y))
  827. return "Y";
  828. if (type == typeof (X))
  829. return "X";
  830. if (type == typeof (int))
  831. return "int";
  832. if (type == typeof (long))
  833. return "long";
  834. if (type == typeof (string))
  835. return "string";
  836. if (type == typeof(Point))
  837. return "point";
  838. return null;
  839. }
  840. public static List<string> ResolvedTypes {
  841. get {
  842. if (resolvedTypes == null) {
  843. resolvedTypes = new List<string> ();
  844. }
  845. return resolvedTypes;
  846. }
  847. }
  848. public static List<string> ResolvedIds {
  849. get {
  850. if (resolvedIds == null) {
  851. resolvedIds = new List<string> ();
  852. }
  853. return resolvedIds;
  854. }
  855. }
  856. public static void Reset ()
  857. {
  858. resolvedIds = null;
  859. resolvedTypes = null;
  860. }
  861. private static List<string> resolvedTypes;
  862. private static List<string> resolvedIds;
  863. }
  864. [Test]
  865. [NUnit.Framework.Category ("NotWorking")]
  866. public void CustomTypeResolver ()
  867. {
  868. JavaScriptSerializer ser = new JavaScriptSerializer (new CustomResolver ());
  869. X x = new X ();
  870. x.Init ();
  871. string s = ser.Serialize (x);
  872. CustomResolver.Reset ();
  873. X x1 = (X) ser.DeserializeObject (s);
  874. Assert.IsTrue (x.Equals (x1), "x != x1");
  875. CustomResolver.Reset ();
  876. X x2 = ser.Deserialize<X> (s);
  877. Assert.IsTrue (x.Equals (x2), "x != x2");
  878. }
  879. [Test]
  880. public void InfinityAndNaN ()
  881. {
  882. JavaScriptSerializer ser = new JavaScriptSerializer ();
  883. double nan = Double.NaN;
  884. string s = ser.Serialize (nan);
  885. Assert.AreEqual (s, "NaN", "#A1");
  886. nan = (double)ser.DeserializeObject (s);
  887. Assert.AreEqual (Double.NaN, nan, "#A2");
  888. nan = (double)ser.Deserialize <double> (s);
  889. Assert.AreEqual (Double.NaN, nan, "#A3");
  890. double infinity = Double.PositiveInfinity;
  891. s = ser.Serialize (infinity);
  892. Assert.AreEqual (s, "Infinity", "#B1");
  893. infinity = (double)ser.DeserializeObject (s);
  894. Assert.AreEqual (Double.PositiveInfinity, infinity, "#B2");
  895. infinity = ser.Deserialize <double> (s);
  896. Assert.AreEqual (Double.PositiveInfinity, infinity, "#B3");
  897. infinity = Double.NegativeInfinity;
  898. s = ser.Serialize (infinity);
  899. Assert.AreEqual (s, "-Infinity", "#C1");
  900. infinity = (double)ser.DeserializeObject (s);
  901. Assert.AreEqual (Double.NegativeInfinity, infinity, "#C2");
  902. infinity = ser.Deserialize <double> (s);
  903. Assert.AreEqual (Double.NegativeInfinity, infinity, "#C3");
  904. var dict = new Dictionary <string, object> ();
  905. dict.Add ("A", Double.NaN);
  906. dict.Add ("B", Double.PositiveInfinity);
  907. dict.Add ("C", Double.NegativeInfinity);
  908. s = ser.Serialize (dict);
  909. Assert.AreEqual ("{\"A\":NaN,\"B\":Infinity,\"C\":-Infinity}", s, "#D1");
  910. dict = (Dictionary <string, object>)ser.DeserializeObject (s);
  911. Assert.AreEqual (Double.NaN, dict ["A"], "#D2");
  912. Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D3");
  913. Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D4");
  914. dict = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object>> (s);
  915. Assert.AreEqual (Double.NaN, dict ["A"], "#D5");
  916. Assert.AreEqual (Double.PositiveInfinity, dict ["B"], "#D6");
  917. Assert.AreEqual (Double.NegativeInfinity, dict ["C"], "#D7");
  918. var arr = new ArrayList () {
  919. Double.NaN,
  920. Double.PositiveInfinity,
  921. Double.NegativeInfinity};
  922. s = ser.Serialize (arr);
  923. Assert.AreEqual ("[NaN,Infinity,-Infinity]", s, "#E1");
  924. object[] arr2 = (object[])ser.DeserializeObject (s);
  925. Assert.AreEqual (3, arr2.Length, "#E2");
  926. Assert.AreEqual (Double.NaN, arr2 [0], "#E3");
  927. Assert.AreEqual (Double.PositiveInfinity, arr2 [1], "#E4");
  928. Assert.AreEqual (Double.NegativeInfinity, arr2 [2], "#E5");
  929. arr = ser.Deserialize <ArrayList> (s);
  930. Assert.AreEqual (3, arr.Count, "#E6");
  931. Assert.AreEqual (Double.NaN, arr [0], "#E7");
  932. Assert.AreEqual (Double.PositiveInfinity, arr [1], "#E8");
  933. Assert.AreEqual (Double.NegativeInfinity, arr [2], "#E9");
  934. }
  935. [Test]
  936. public void StandalonePrimitives ()
  937. {
  938. JavaScriptSerializer ser = new JavaScriptSerializer ();
  939. object o;
  940. int i;
  941. o = ser.DeserializeObject ("1");
  942. Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A1");
  943. i = (int)o;
  944. Assert.AreEqual (1, i, "#A2");
  945. o =ser.DeserializeObject ("-1");
  946. Assert.AreEqual (typeof (global::System.Int32), o.GetType (), "#A3");
  947. i = (int)o;
  948. Assert.AreEqual (-1, i, "#A4");
  949. o = ser.DeserializeObject ("2147483649");
  950. Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B1");
  951. long l = (long)o;
  952. Assert.AreEqual (2147483649, l, "#B2");
  953. o = ser.DeserializeObject ("-2147483649");
  954. Assert.AreEqual (typeof (global::System.Int64), o.GetType (), "#B3");
  955. l = (long)o;
  956. Assert.AreEqual (-2147483649, l, "#B4");
  957. o = ser.DeserializeObject ("9223372036854775808");
  958. Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C1");
  959. decimal d = (decimal)o;
  960. Assert.AreEqual (9223372036854775808m, d, "#C2");
  961. o = ser.DeserializeObject ("-9223372036854775809");
  962. Assert.AreEqual (typeof (global::System.Decimal), o.GetType (), "#C3");
  963. d = (decimal)o;
  964. Assert.AreEqual (-9223372036854775809m, d, "#C4");
  965. o = ser.DeserializeObject ("79228162514264337593543950336");
  966. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D1");
  967. double db = (double)o;
  968. Assert.AreEqual (79228162514264337593543950336.0, db, "#D2");
  969. o = ser.DeserializeObject ("-79228162514264337593543950336");
  970. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#D3");
  971. db = (double)o;
  972. Assert.AreEqual (-79228162514264337593543950336.0, db, "#D4");
  973. o = ser.DeserializeObject ("\"test string\"");
  974. Assert.AreEqual (typeof (global::System.String), o.GetType (), "#E1");
  975. string s = (string)o;
  976. Assert.AreEqual ("test string", s, "#E2");
  977. o = ser.DeserializeObject ("true");
  978. Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F1");
  979. bool b = (bool)o;
  980. Assert.AreEqual (true, b, "#F2");
  981. o = ser.DeserializeObject ("false");
  982. Assert.AreEqual (typeof (global::System.Boolean), o.GetType (), "#F3");
  983. b = (bool)o;
  984. Assert.AreEqual (false, b, "#F4");
  985. o = ser.DeserializeObject ("-1.7976931348623157E+308");
  986. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G1");
  987. db = (double)o;
  988. Assert.AreEqual (Double.MinValue, db, "#G2");
  989. o = ser.DeserializeObject ("1.7976931348623157E+308");
  990. Assert.AreEqual (typeof (global::System.Double), o.GetType (), "#G3");
  991. db = (double)o;
  992. Assert.AreEqual (Double.MaxValue, db, "#G4");
  993. }
  994. class SomeDict : IDictionary<string, object>
  995. {
  996. void IDictionary<string, object>.Add (string key, object value) {
  997. throw new NotSupportedException ();
  998. }
  999. bool IDictionary<string, object>.ContainsKey (string key) {
  1000. throw new NotSupportedException ();
  1001. }
  1002. ICollection<string> IDictionary<string, object>.Keys {
  1003. get { throw new NotSupportedException (); }
  1004. }
  1005. bool IDictionary<string, object>.Remove (string key) {
  1006. throw new NotSupportedException ();
  1007. }
  1008. bool IDictionary<string, object>.TryGetValue (string key, out object value) {
  1009. throw new NotSupportedException ();
  1010. }
  1011. ICollection<object> IDictionary<string, object>.Values {
  1012. get { throw new NotSupportedException (); }
  1013. }
  1014. object IDictionary<string, object>.this [string key] {
  1015. get { throw new NotSupportedException (); }
  1016. set { throw new NotSupportedException (); }
  1017. }
  1018. void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item) {
  1019. throw new NotSupportedException ();
  1020. }
  1021. void ICollection<KeyValuePair<string, object>>.Clear () {
  1022. throw new NotSupportedException ();
  1023. }
  1024. bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item) {
  1025. throw new NotSupportedException ();
  1026. }
  1027. void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex) {
  1028. throw new NotSupportedException ();
  1029. }
  1030. int ICollection<KeyValuePair<string, object>>.Count {
  1031. get { throw new NotSupportedException (); }
  1032. }
  1033. bool ICollection<KeyValuePair<string, object>>.IsReadOnly {
  1034. get { throw new NotSupportedException (); }
  1035. }
  1036. bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item) {
  1037. throw new NotSupportedException ();
  1038. }
  1039. IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator () {
  1040. return GetEnumerator ();
  1041. }
  1042. IEnumerator IEnumerable.GetEnumerator () {
  1043. return ((IEnumerable<KeyValuePair<string, object>>) this).GetEnumerator ();
  1044. }
  1045. protected IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
  1046. yield return new KeyValuePair<string, object> ("hello", "world");
  1047. }
  1048. }
  1049. [Test] //bug #424704
  1050. public void NonGenericClassImplementingClosedGenericIDictionary ()
  1051. {
  1052. JavaScriptSerializer ser = new JavaScriptSerializer ();
  1053. SomeDict dictIn = new SomeDict ();
  1054. string s = ser.Serialize (dictIn);
  1055. Dictionary<string, object> dictOut = ser.Deserialize<Dictionary<string, object>> (s);
  1056. Assert.AreEqual (dictOut.Count, 1, "#1");
  1057. Assert.AreEqual (dictOut["hello"], "world", "#2");
  1058. }
  1059. }
  1060. }