DataContractJsonSerializerTest.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. //
  2. // DataContractJsonSerializerTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Ankit Jain <[email protected]>
  7. //
  8. // Copyright (C) 2005-2007 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. // This test code contains tests for DataContractJsonSerializer, which is
  31. // imported from DataContractSerializerTest.cs.
  32. //
  33. using System;
  34. using System.Collections.Generic;
  35. using System.Collections.ObjectModel;
  36. using System.IO;
  37. using System.Net;
  38. using System.Runtime.Serialization;
  39. using System.Runtime.Serialization.Json;
  40. using System.Text;
  41. using System.Xml;
  42. using NUnit.Framework;
  43. namespace MonoTests.System.Runtime.Serialization.Json
  44. {
  45. [TestFixture]
  46. public class DataContractJsonSerializerTest
  47. {
  48. static readonly XmlWriterSettings settings;
  49. static DataContractJsonSerializerTest ()
  50. {
  51. settings = new XmlWriterSettings ();
  52. settings.OmitXmlDeclaration = true;
  53. }
  54. [DataContract]
  55. class Sample1
  56. {
  57. [DataMember]
  58. public string Member1;
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentNullException))]
  62. public void ConstructorTypeNull ()
  63. {
  64. new DataContractJsonSerializer (null);
  65. }
  66. [Test]
  67. public void ConstructorKnownTypesNull ()
  68. {
  69. // null knownTypes is allowed.
  70. new DataContractJsonSerializer (typeof (Sample1), (IEnumerable<Type>) null);
  71. new DataContractJsonSerializer (typeof (Sample1), "Foo", null);
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentNullException))]
  75. public void ConstructorNameNull ()
  76. {
  77. new DataContractJsonSerializer (typeof (Sample1), (string) null);
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  81. public void ConstructorNegativeMaxObjects ()
  82. {
  83. new DataContractJsonSerializer (typeof (Sample1), "Sample1",
  84. null, -1, false, null, false);
  85. }
  86. [Test]
  87. public void ConstructorMisc ()
  88. {
  89. new DataContractJsonSerializer (typeof (GlobalSample1));
  90. }
  91. [Test]
  92. public void WriteObjectContent ()
  93. {
  94. StringWriter sw = new StringWriter ();
  95. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  96. DataContractJsonSerializer ser =
  97. new DataContractJsonSerializer (typeof (string));
  98. xw.WriteStartElement ("my-element");
  99. ser.WriteObjectContent (xw, "TEST STRING");
  100. xw.WriteEndElement ();
  101. }
  102. Assert.AreEqual ("<my-element>TEST STRING</my-element>",
  103. sw.ToString ());
  104. }
  105. // int
  106. [Test]
  107. public void SerializeIntXml ()
  108. {
  109. StringWriter sw = new StringWriter ();
  110. SerializeInt (XmlWriter.Create (sw, settings));
  111. Assert.AreEqual (
  112. @"<root type=""number"">1</root>",
  113. sw.ToString ());
  114. }
  115. [Test]
  116. public void SerializeIntJson ()
  117. {
  118. MemoryStream ms = new MemoryStream ();
  119. SerializeInt (JsonReaderWriterFactory.CreateJsonWriter (ms));
  120. Assert.AreEqual (
  121. "1",
  122. Encoding.UTF8.GetString (ms.ToArray ()));
  123. }
  124. void SerializeInt (XmlWriter writer)
  125. {
  126. DataContractJsonSerializer ser =
  127. new DataContractJsonSerializer (typeof (int));
  128. using (XmlWriter w = writer) {
  129. ser.WriteObject (w, 1);
  130. }
  131. }
  132. // int, with rootName
  133. [Test]
  134. public void SerializeIntXmlWithRootName ()
  135. {
  136. StringWriter sw = new StringWriter ();
  137. SerializeIntWithRootName (XmlWriter.Create (sw, settings));
  138. Assert.AreEqual (
  139. @"<myroot type=""number"">1</myroot>",
  140. sw.ToString ());
  141. }
  142. [Test]
  143. // since JsonWriter supports only "root" as the root name, using
  144. // XmlWriter from JsonReaderWriterFactory will always fail with
  145. // an explicit rootName.
  146. [ExpectedException (typeof (SerializationException))]
  147. public void SerializeIntJsonWithRootName ()
  148. {
  149. MemoryStream ms = new MemoryStream ();
  150. SerializeIntWithRootName (JsonReaderWriterFactory.CreateJsonWriter (ms));
  151. Assert.AreEqual (
  152. "1",
  153. Encoding.UTF8.GetString (ms.ToArray ()));
  154. }
  155. void SerializeIntWithRootName (XmlWriter writer)
  156. {
  157. DataContractJsonSerializer ser =
  158. new DataContractJsonSerializer (typeof (int), "myroot");
  159. using (XmlWriter w = writer) {
  160. ser.WriteObject (w, 1);
  161. }
  162. }
  163. // pass typeof(DCEmpty), serialize int
  164. [Test]
  165. public void SerializeIntForDCEmptyXml ()
  166. {
  167. StringWriter sw = new StringWriter ();
  168. SerializeIntForDCEmpty (XmlWriter.Create (sw, settings));
  169. Assert.AreEqual (
  170. @"<root type=""number"">1</root>",
  171. sw.ToString ());
  172. }
  173. [Test]
  174. public void SerializeIntForDCEmptyJson ()
  175. {
  176. MemoryStream ms = new MemoryStream ();
  177. SerializeIntForDCEmpty (JsonReaderWriterFactory.CreateJsonWriter (ms));
  178. Assert.AreEqual (
  179. "1",
  180. Encoding.UTF8.GetString (ms.ToArray ()));
  181. }
  182. void SerializeIntForDCEmpty (XmlWriter writer)
  183. {
  184. DataContractJsonSerializer ser =
  185. new DataContractJsonSerializer (typeof (DCEmpty));
  186. using (XmlWriter w = writer) {
  187. ser.WriteObject (w, 1);
  188. }
  189. }
  190. // DCEmpty
  191. [Test]
  192. public void SerializeEmptyClassXml ()
  193. {
  194. StringWriter sw = new StringWriter ();
  195. SerializeEmptyClass (XmlWriter.Create (sw, settings));
  196. Assert.AreEqual (
  197. @"<root type=""object"" />",
  198. sw.ToString ());
  199. }
  200. [Test]
  201. public void SerializeEmptyClassJson ()
  202. {
  203. MemoryStream ms = new MemoryStream ();
  204. SerializeEmptyClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
  205. Assert.AreEqual (
  206. "{}",
  207. Encoding.UTF8.GetString (ms.ToArray ()));
  208. }
  209. void SerializeEmptyClass (XmlWriter writer)
  210. {
  211. DataContractJsonSerializer ser =
  212. new DataContractJsonSerializer (typeof (DCEmpty));
  213. using (XmlWriter w = writer) {
  214. ser.WriteObject (w, new DCEmpty ());
  215. }
  216. }
  217. // string (primitive)
  218. [Test]
  219. public void SerializePrimitiveStringXml ()
  220. {
  221. StringWriter sw = new StringWriter ();
  222. SerializePrimitiveString (XmlWriter.Create (sw, settings));
  223. Assert.AreEqual (
  224. "<root>TEST</root>",
  225. sw.ToString ());
  226. }
  227. [Test]
  228. public void SerializePrimitiveStringJson ()
  229. {
  230. MemoryStream ms = new MemoryStream ();
  231. SerializePrimitiveString (JsonReaderWriterFactory.CreateJsonWriter (ms));
  232. Assert.AreEqual (
  233. @"""TEST""",
  234. Encoding.UTF8.GetString (ms.ToArray ()));
  235. }
  236. void SerializePrimitiveString (XmlWriter writer)
  237. {
  238. XmlObjectSerializer ser =
  239. new DataContractJsonSerializer (typeof (string));
  240. using (XmlWriter w = writer) {
  241. ser.WriteObject (w, "TEST");
  242. }
  243. }
  244. // QName (primitive but ...)
  245. [Test]
  246. public void SerializePrimitiveQNameXml ()
  247. {
  248. StringWriter sw = new StringWriter ();
  249. SerializePrimitiveQName (XmlWriter.Create (sw, settings));
  250. Assert.AreEqual (
  251. "<root>foo:urn:foo</root>",
  252. sw.ToString ());
  253. }
  254. [Test]
  255. public void SerializePrimitiveQNameJson ()
  256. {
  257. MemoryStream ms = new MemoryStream ();
  258. SerializePrimitiveQName (JsonReaderWriterFactory.CreateJsonWriter (ms));
  259. Assert.AreEqual (
  260. @"""foo:urn:foo""",
  261. Encoding.UTF8.GetString (ms.ToArray ()));
  262. }
  263. void SerializePrimitiveQName (XmlWriter writer)
  264. {
  265. XmlObjectSerializer ser =
  266. new DataContractJsonSerializer (typeof (XmlQualifiedName));
  267. using (XmlWriter w = writer) {
  268. ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
  269. }
  270. }
  271. // DBNull (primitive)
  272. [Test]
  273. public void SerializeDBNullXml ()
  274. {
  275. StringWriter sw = new StringWriter ();
  276. SerializeDBNull (XmlWriter.Create (sw, settings));
  277. Assert.AreEqual (
  278. @"<root type=""object"" />",
  279. sw.ToString ());
  280. }
  281. [Test]
  282. public void SerializeDBNullJson ()
  283. {
  284. MemoryStream ms = new MemoryStream ();
  285. SerializeDBNull (JsonReaderWriterFactory.CreateJsonWriter (ms));
  286. Assert.AreEqual (
  287. "{}",
  288. Encoding.UTF8.GetString (ms.ToArray ()));
  289. }
  290. void SerializeDBNull (XmlWriter writer)
  291. {
  292. DataContractJsonSerializer ser =
  293. new DataContractJsonSerializer (typeof (DBNull));
  294. using (XmlWriter w = writer) {
  295. ser.WriteObject (w, DBNull.Value);
  296. }
  297. }
  298. // DCSimple1
  299. [Test]
  300. public void SerializeSimpleClass1Xml ()
  301. {
  302. StringWriter sw = new StringWriter ();
  303. SerializeSimpleClass1 (XmlWriter.Create (sw, settings));
  304. Assert.AreEqual (
  305. @"<root type=""object""><Foo>TEST</Foo></root>",
  306. sw.ToString ());
  307. }
  308. [Test]
  309. public void SerializeSimpleClass1Json ()
  310. {
  311. MemoryStream ms = new MemoryStream ();
  312. SerializeSimpleClass1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  313. Assert.AreEqual (
  314. @"{""Foo"":""TEST""}",
  315. Encoding.UTF8.GetString (ms.ToArray ()));
  316. }
  317. void SerializeSimpleClass1 (XmlWriter writer)
  318. {
  319. DataContractJsonSerializer ser =
  320. new DataContractJsonSerializer (typeof (DCSimple1));
  321. using (XmlWriter w = writer) {
  322. ser.WriteObject (w, new DCSimple1 ());
  323. }
  324. }
  325. // NonDC
  326. [Test]
  327. // NonDC is not a DataContract type.
  328. public void SerializeNonDCOnlyCtor ()
  329. {
  330. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
  331. }
  332. [Test]
  333. //[ExpectedException (typeof (InvalidDataContractException))]
  334. // NonDC is not a DataContract type.
  335. // UPDATE: non-DataContract types are became valid in RTM.
  336. public void SerializeNonDC ()
  337. {
  338. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
  339. using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
  340. ser.WriteObject (w, new NonDC ());
  341. }
  342. }
  343. // DCHasNonDC
  344. [Test]
  345. //[ExpectedException (typeof (InvalidDataContractException))]
  346. // DCHasNonDC itself is a DataContract type whose field is
  347. // marked as DataMember but its type is not DataContract.
  348. // UPDATE: non-DataContract types are became valid in RTM.
  349. public void SerializeDCHasNonDC ()
  350. {
  351. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasNonDC));
  352. using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
  353. ser.WriteObject (w, new DCHasNonDC ());
  354. }
  355. }
  356. // DCHasSerializable
  357. [Test]
  358. public void SerializeSimpleSerializable1Xml ()
  359. {
  360. StringWriter sw = new StringWriter ();
  361. SerializeSimpleSerializable1 (XmlWriter.Create (sw, settings));
  362. Assert.AreEqual (
  363. @"<root type=""object""><Ser type=""object""><Doh>doh!</Doh></Ser></root>",
  364. sw.ToString ());
  365. }
  366. [Test]
  367. public void SerializeSimpleSerializable1Json ()
  368. {
  369. MemoryStream ms = new MemoryStream ();
  370. SerializeSimpleSerializable1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  371. Assert.AreEqual (
  372. @"{""Ser"":{""Doh"":""doh!""}}",
  373. Encoding.UTF8.GetString (ms.ToArray ()));
  374. }
  375. // DCHasSerializable itself is DataContract and has a field
  376. // whose type is not contract but serializable.
  377. void SerializeSimpleSerializable1 (XmlWriter writer)
  378. {
  379. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasSerializable));
  380. using (XmlWriter w = writer) {
  381. ser.WriteObject (w, new DCHasSerializable ());
  382. }
  383. }
  384. [Test]
  385. public void SerializeDCWithNameXml ()
  386. {
  387. StringWriter sw = new StringWriter ();
  388. SerializeDCWithName (XmlWriter.Create (sw, settings));
  389. Assert.AreEqual (
  390. @"<root type=""object""><FooMember>value</FooMember></root>",
  391. sw.ToString ());
  392. }
  393. [Test]
  394. public void SerializeDCWithNameJson ()
  395. {
  396. MemoryStream ms = new MemoryStream ();
  397. SerializeDCWithName (JsonReaderWriterFactory.CreateJsonWriter (ms));
  398. Assert.AreEqual (
  399. @"{""FooMember"":""value""}",
  400. Encoding.UTF8.GetString (ms.ToArray ()));
  401. }
  402. void SerializeDCWithName (XmlWriter writer)
  403. {
  404. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
  405. using (XmlWriter w = writer) {
  406. ser.WriteObject (w, new DCWithName ());
  407. }
  408. }
  409. [Test]
  410. public void SerializeDCWithEmptyName1 ()
  411. {
  412. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyName));
  413. StringWriter sw = new StringWriter ();
  414. DCWithEmptyName dc = new DCWithEmptyName ();
  415. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  416. try {
  417. ser.WriteObject (w, dc);
  418. } catch (InvalidDataContractException) {
  419. return;
  420. }
  421. }
  422. Assert.Fail ("Expected InvalidDataContractException");
  423. }
  424. [Test]
  425. public void SerializeDCWithEmptyName2 ()
  426. {
  427. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
  428. StringWriter sw = new StringWriter ();
  429. /* DataContractAttribute.Name == "", not valid */
  430. DCWithEmptyName dc = new DCWithEmptyName ();
  431. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  432. try {
  433. ser.WriteObject (w, dc);
  434. } catch (InvalidDataContractException) {
  435. return;
  436. }
  437. }
  438. Assert.Fail ("Expected InvalidDataContractException");
  439. }
  440. [Test]
  441. [Category("NotWorking")]
  442. public void SerializeDCWithNullName ()
  443. {
  444. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithNullName));
  445. StringWriter sw = new StringWriter ();
  446. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  447. try {
  448. /* DataContractAttribute.Name == "", not valid */
  449. ser.WriteObject (w, new DCWithNullName ());
  450. } catch (InvalidDataContractException) {
  451. return;
  452. }
  453. }
  454. Assert.Fail ("Expected InvalidDataContractException");
  455. }
  456. [Test]
  457. public void SerializeDCWithEmptyNamespace1 ()
  458. {
  459. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyNamespace));
  460. StringWriter sw = new StringWriter ();
  461. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  462. ser.WriteObject (w, new DCWithEmptyNamespace ());
  463. }
  464. }
  465. [Test]
  466. public void SerializeWrappedClassXml ()
  467. {
  468. StringWriter sw = new StringWriter ();
  469. SerializeWrappedClass (XmlWriter.Create (sw, settings));
  470. Assert.AreEqual (
  471. @"<root type=""object"" />",
  472. sw.ToString ());
  473. }
  474. [Test]
  475. public void SerializeWrappedClassJson ()
  476. {
  477. MemoryStream ms = new MemoryStream ();
  478. SerializeWrappedClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
  479. Assert.AreEqual (
  480. "{}",
  481. Encoding.UTF8.GetString (ms.ToArray ()));
  482. }
  483. void SerializeWrappedClass (XmlWriter writer)
  484. {
  485. DataContractJsonSerializer ser =
  486. new DataContractJsonSerializer (typeof (Wrapper.DCWrapped));
  487. using (XmlWriter w = writer) {
  488. ser.WriteObject (w, new Wrapper.DCWrapped ());
  489. }
  490. }
  491. // CollectionContainer : Items must have a setter. (but became valid in RTM).
  492. [Test]
  493. public void SerializeReadOnlyCollectionMember ()
  494. {
  495. DataContractJsonSerializer ser =
  496. new DataContractJsonSerializer (typeof (CollectionContainer));
  497. StringWriter sw = new StringWriter ();
  498. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  499. ser.WriteObject (w, null);
  500. }
  501. }
  502. // DataCollectionContainer : Items must have a setter. (but became valid in RTM).
  503. [Test]
  504. public void SerializeReadOnlyDataCollectionMember ()
  505. {
  506. DataContractJsonSerializer ser =
  507. new DataContractJsonSerializer (typeof (DataCollectionContainer));
  508. StringWriter sw = new StringWriter ();
  509. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  510. ser.WriteObject (w, null);
  511. }
  512. }
  513. [Test]
  514. [Ignore ("https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=409970")]
  515. [ExpectedException (typeof (SerializationException))]
  516. public void DeserializeReadOnlyDataCollection_NullCollection ()
  517. {
  518. DataContractJsonSerializer ser =
  519. new DataContractJsonSerializer (typeof (CollectionContainer));
  520. StringWriter sw = new StringWriter ();
  521. var c = new CollectionContainer ();
  522. c.Items.Add ("foo");
  523. c.Items.Add ("bar");
  524. using (XmlWriter w = XmlWriter.Create (sw, settings))
  525. ser.WriteObject (w, c);
  526. // CollectionContainer.Items is null, so it cannot deserialize non-null collection.
  527. using (XmlReader r = XmlReader.Create (new StringReader (sw.ToString ())))
  528. c = (CollectionContainer) ser.ReadObject (r);
  529. }
  530. [Test]
  531. public void SerializeGuidXml ()
  532. {
  533. StringWriter sw = new StringWriter ();
  534. SerializeGuid (XmlWriter.Create (sw, settings));
  535. Assert.AreEqual (
  536. @"<root>00000000-0000-0000-0000-000000000000</root>",
  537. sw.ToString ());
  538. }
  539. [Test]
  540. public void SerializeGuidJson ()
  541. {
  542. MemoryStream ms = new MemoryStream ();
  543. SerializeGuid (JsonReaderWriterFactory.CreateJsonWriter (ms));
  544. Assert.AreEqual (
  545. @"""00000000-0000-0000-0000-000000000000""",
  546. Encoding.UTF8.GetString (ms.ToArray ()));
  547. }
  548. void SerializeGuid (XmlWriter writer)
  549. {
  550. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Guid));
  551. using (XmlWriter w = writer) {
  552. ser.WriteObject (w, Guid.Empty);
  553. }
  554. }
  555. [Test]
  556. public void SerializeEnumXml ()
  557. {
  558. StringWriter sw = new StringWriter ();
  559. SerializeEnum (XmlWriter.Create (sw, settings));
  560. Assert.AreEqual (
  561. @"<root type=""number"">0</root>",
  562. sw.ToString ());
  563. }
  564. [Test]
  565. public void SerializeEnumJson ()
  566. {
  567. MemoryStream ms = new MemoryStream ();
  568. SerializeEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
  569. Assert.AreEqual (
  570. "0",
  571. Encoding.UTF8.GetString (ms.ToArray ()));
  572. }
  573. void SerializeEnum (XmlWriter writer)
  574. {
  575. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
  576. using (XmlWriter w = writer) {
  577. ser.WriteObject (w, new Colors ());
  578. }
  579. }
  580. [Test]
  581. public void SerializeEnum2Xml ()
  582. {
  583. StringWriter sw = new StringWriter ();
  584. SerializeEnum2 (XmlWriter.Create (sw, settings));
  585. Assert.AreEqual (
  586. @"<root type=""number"">0</root>",
  587. sw.ToString ());
  588. }
  589. [Test]
  590. public void SerializeEnum2Json ()
  591. {
  592. MemoryStream ms = new MemoryStream ();
  593. SerializeEnum2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  594. Assert.AreEqual (
  595. "0",
  596. Encoding.UTF8.GetString (ms.ToArray ()));
  597. }
  598. void SerializeEnum2 (XmlWriter writer)
  599. {
  600. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
  601. using (XmlWriter w = writer) {
  602. ser.WriteObject (w, 0);
  603. }
  604. }
  605. [Test] // so, DataContract does not affect here.
  606. public void SerializeEnumWithDCXml ()
  607. {
  608. StringWriter sw = new StringWriter ();
  609. SerializeEnumWithDC (XmlWriter.Create (sw, settings));
  610. Assert.AreEqual (
  611. @"<root type=""number"">0</root>",
  612. sw.ToString ());
  613. }
  614. [Test]
  615. public void SerializeEnumWithDCJson ()
  616. {
  617. MemoryStream ms = new MemoryStream ();
  618. SerializeEnumWithDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
  619. Assert.AreEqual (
  620. "0",
  621. Encoding.UTF8.GetString (ms.ToArray ()));
  622. }
  623. void SerializeEnumWithDC (XmlWriter writer)
  624. {
  625. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
  626. using (XmlWriter w = writer) {
  627. ser.WriteObject (w, new ColorsWithDC ());
  628. }
  629. }
  630. [Test]
  631. public void SerializeEnumWithNoDCXml ()
  632. {
  633. StringWriter sw = new StringWriter ();
  634. SerializeEnumWithNoDC (XmlWriter.Create (sw, settings));
  635. Assert.AreEqual (
  636. @"<root type=""number"">0</root>",
  637. sw.ToString ());
  638. }
  639. [Test]
  640. public void SerializeEnumWithNoDCJson ()
  641. {
  642. MemoryStream ms = new MemoryStream ();
  643. SerializeEnumWithNoDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
  644. Assert.AreEqual (
  645. "0",
  646. Encoding.UTF8.GetString (ms.ToArray ()));
  647. }
  648. void SerializeEnumWithNoDC (XmlWriter writer)
  649. {
  650. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsEnumMemberNoDC));
  651. using (XmlWriter w = writer) {
  652. ser.WriteObject (w, new ColorsEnumMemberNoDC ());
  653. }
  654. }
  655. [Test]
  656. public void SerializeEnumWithDC2Xml ()
  657. {
  658. StringWriter sw = new StringWriter ();
  659. SerializeEnumWithDC2 (XmlWriter.Create (sw, settings));
  660. Assert.AreEqual (
  661. @"<root type=""number"">3</root>",
  662. sw.ToString ());
  663. }
  664. [Test]
  665. public void SerializeEnumWithDC2Json ()
  666. {
  667. MemoryStream ms = new MemoryStream ();
  668. SerializeEnumWithDC2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  669. Assert.AreEqual (
  670. "3",
  671. Encoding.UTF8.GetString (ms.ToArray ()));
  672. }
  673. void SerializeEnumWithDC2 (XmlWriter writer)
  674. {
  675. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
  676. using (XmlWriter w = writer) {
  677. ser.WriteObject (w, 3);
  678. }
  679. }
  680. /*
  681. [Test]
  682. [ExpectedException (typeof (SerializationException))]
  683. public void SerializeEnumWithDCInvalid ()
  684. {
  685. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
  686. StringWriter sw = new StringWriter ();
  687. ColorsWithDC cdc = ColorsWithDC.Blue;
  688. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  689. ser.WriteObject (w, cdc);
  690. }
  691. }
  692. */
  693. [Test]
  694. public void SerializeDCWithEnumXml ()
  695. {
  696. StringWriter sw = new StringWriter ();
  697. SerializeDCWithEnum (XmlWriter.Create (sw, settings));
  698. Assert.AreEqual (
  699. @"<root type=""object""><_colors type=""number"">0</_colors></root>",
  700. sw.ToString ());
  701. }
  702. [Test]
  703. public void SerializeDCWithEnumJson ()
  704. {
  705. MemoryStream ms = new MemoryStream ();
  706. SerializeDCWithEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
  707. Assert.AreEqual (
  708. @"{""_colors"":0}",
  709. Encoding.UTF8.GetString (ms.ToArray ()));
  710. }
  711. void SerializeDCWithEnum (XmlWriter writer)
  712. {
  713. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum));
  714. using (XmlWriter w = writer) {
  715. ser.WriteObject (w, new DCWithEnum ());
  716. }
  717. }
  718. [Test]
  719. public void SerializerDCArrayXml ()
  720. {
  721. StringWriter sw = new StringWriter ();
  722. SerializerDCArray (XmlWriter.Create (sw, settings));
  723. Assert.AreEqual (
  724. @"<root type=""array""><item type=""object""><_colors type=""number"">0</_colors></item><item type=""object""><_colors type=""number"">1</_colors></item></root>",
  725. sw.ToString ());
  726. }
  727. [Test]
  728. public void SerializerDCArrayJson ()
  729. {
  730. MemoryStream ms = new MemoryStream ();
  731. SerializerDCArray (JsonReaderWriterFactory.CreateJsonWriter (ms));
  732. Assert.AreEqual (
  733. @"[{""_colors"":0},{""_colors"":1}]",
  734. Encoding.UTF8.GetString (ms.ToArray ()));
  735. }
  736. void SerializerDCArray (XmlWriter writer)
  737. {
  738. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum []));
  739. DCWithEnum [] arr = new DCWithEnum [2];
  740. arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
  741. arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
  742. using (XmlWriter w = writer) {
  743. ser.WriteObject (w, arr);
  744. }
  745. }
  746. [Test]
  747. public void SerializerDCArray2Xml ()
  748. {
  749. StringWriter sw = new StringWriter ();
  750. SerializerDCArray2 (XmlWriter.Create (sw, settings));
  751. Assert.AreEqual (
  752. @"<root type=""array""><item __type=""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"" type=""object""><_colors type=""number"">0</_colors></item><item __type=""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"" type=""object""><Foo>hello</Foo></item></root>",
  753. sw.ToString ());
  754. }
  755. [Test]
  756. public void SerializerDCArray2Json ()
  757. {
  758. MemoryStream ms = new MemoryStream ();
  759. SerializerDCArray2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  760. Assert.AreEqual (
  761. @"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
  762. Encoding.UTF8.GetString (ms.ToArray ()));
  763. }
  764. void SerializerDCArray2 (XmlWriter writer)
  765. {
  766. List<Type> known = new List<Type> ();
  767. known.Add (typeof (DCWithEnum));
  768. known.Add (typeof (DCSimple1));
  769. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (object []), known);
  770. object [] arr = new object [2];
  771. arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
  772. arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
  773. using (XmlWriter w = writer) {
  774. ser.WriteObject (w, arr);
  775. }
  776. }
  777. [Test]
  778. public void SerializerDCArray3Xml ()
  779. {
  780. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
  781. StringWriter sw = new StringWriter ();
  782. int [] arr = new int [2];
  783. arr [0] = 1; arr [1] = 2;
  784. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  785. ser.WriteObject (w, arr);
  786. }
  787. Assert.AreEqual (
  788. @"<root type=""array""><item type=""number"">1</item><item type=""number"">2</item></root>",
  789. sw.ToString ());
  790. }
  791. [Test]
  792. public void SerializerDCArray3Json ()
  793. {
  794. MemoryStream ms = new MemoryStream ();
  795. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
  796. int [] arr = new int [2];
  797. arr [0] = 1; arr [1] = 2;
  798. using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
  799. ser.WriteObject (w, arr);
  800. }
  801. Assert.AreEqual (
  802. @"[1,2]",
  803. Encoding.UTF8.GetString (ms.ToArray ()));
  804. }
  805. [Test]
  806. // ... so, non-JSON XmlWriter is still accepted.
  807. public void SerializeNonDCArrayXml ()
  808. {
  809. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
  810. StringWriter sw = new StringWriter ();
  811. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  812. ser.WriteObject (xw, new SerializeNonDCArrayType ());
  813. }
  814. Assert.AreEqual (@"<root type=""object""><IPAddresses type=""array"" /></root>",
  815. sw.ToString ());
  816. }
  817. [Test]
  818. public void SerializeNonDCArrayJson ()
  819. {
  820. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
  821. MemoryStream ms = new MemoryStream ();
  822. using (XmlWriter xw = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
  823. ser.WriteObject (xw, new SerializeNonDCArrayType ());
  824. }
  825. Assert.AreEqual (@"{""IPAddresses"":[]}",
  826. Encoding.UTF8.GetString (ms.ToArray ()));
  827. }
  828. [Test]
  829. public void SerializeNonDCArrayItems ()
  830. {
  831. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
  832. StringWriter sw = new StringWriter ();
  833. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  834. SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
  835. obj.IPAddresses = new IPAddress [] {new IPAddress (new byte [] {1, 2, 3, 4})};
  836. ser.WriteObject (xw, obj);
  837. }
  838. XmlDocument doc = new XmlDocument ();
  839. doc.LoadXml (sw.ToString ());
  840. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  841. nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
  842. nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
  843. nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
  844. Assert.AreEqual (1, doc.SelectNodes ("/root/IPAddresses/item", nsmgr).Count, "#1");
  845. Assert.AreEqual ("67305985", doc.SelectSingleNode ("/root/IPAddresses/item/m_Address", nsmgr).InnerText, "#2");
  846. XmlElement el = doc.SelectSingleNode ("/root/IPAddresses/item/m_Numbers", nsmgr) as XmlElement;
  847. Assert.IsNotNull (el, "#3");
  848. Assert.AreEqual (8, el.SelectNodes ("item", nsmgr).Count, "#4");
  849. }
  850. [Test]
  851. public void MaxItemsInObjectGraph1 ()
  852. {
  853. // object count == maximum
  854. DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCEmpty), null, 1, false, null, false);
  855. s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCEmpty ());
  856. }
  857. [Test]
  858. [ExpectedException (typeof (SerializationException))]
  859. public void MaxItemsInObjectGraph2 ()
  860. {
  861. // object count > maximum
  862. DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1), null, 1, false, null, false);
  863. s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCSimple1 ());
  864. }
  865. [Test]
  866. public void DeserializeString ()
  867. {
  868. Assert.AreEqual ("ABC", Deserialize ("\"ABC\"", typeof (string)));
  869. }
  870. [Test]
  871. public void DeserializeInt ()
  872. {
  873. Assert.AreEqual (5, Deserialize ("5", typeof (int)));
  874. }
  875. [Test]
  876. public void DeserializeArray ()
  877. {
  878. int [] ret = (int []) Deserialize ("[5,6,7]", typeof (int []));
  879. Assert.AreEqual (5, ret [0], "#1");
  880. Assert.AreEqual (6, ret [1], "#2");
  881. Assert.AreEqual (7, ret [2], "#3");
  882. }
  883. [Test]
  884. public void DeserializeArrayUntyped ()
  885. {
  886. object [] ret = (object []) Deserialize ("[5,6,7]", typeof (object []));
  887. Assert.AreEqual (5, ret [0], "#1");
  888. Assert.AreEqual (6, ret [1], "#2");
  889. Assert.AreEqual (7, ret [2], "#3");
  890. }
  891. [Test]
  892. public void DeserializeMixedArray ()
  893. {
  894. object [] ret = (object []) Deserialize ("[5,\"6\",false]", typeof (object []));
  895. Assert.AreEqual (5, ret [0], "#1");
  896. Assert.AreEqual ("6", ret [1], "#2");
  897. Assert.AreEqual (false, ret [2], "#3");
  898. }
  899. [Test]
  900. [ExpectedException (typeof (SerializationException))]
  901. public void DeserializeEmptyAsString ()
  902. {
  903. // it somehow expects "root" which should have been already consumed.
  904. Deserialize ("", typeof (string));
  905. }
  906. [Test]
  907. [ExpectedException (typeof (SerializationException))]
  908. public void DeserializeEmptyAsInt ()
  909. {
  910. // it somehow expects "root" which should have been already consumed.
  911. Deserialize ("", typeof (int));
  912. }
  913. [Test]
  914. [ExpectedException (typeof (SerializationException))]
  915. public void DeserializeEmptyAsDBNull ()
  916. {
  917. // it somehow expects "root" which should have been already consumed.
  918. Deserialize ("", typeof (DBNull));
  919. }
  920. [Test]
  921. public void DeserializeEmptyObjectAsString ()
  922. {
  923. // looks like it is converted to ""
  924. Assert.AreEqual (String.Empty, Deserialize ("{}", typeof (string)));
  925. }
  926. [Test]
  927. [ExpectedException (typeof (SerializationException))]
  928. public void DeserializeEmptyObjectAsInt ()
  929. {
  930. Deserialize ("{}", typeof (int));
  931. }
  932. [Test]
  933. public void DeserializeEmptyObjectAsDBNull ()
  934. {
  935. Assert.AreEqual (DBNull.Value, Deserialize ("{}", typeof (DBNull)));
  936. }
  937. [Test]
  938. [ExpectedException (typeof (SerializationException))]
  939. public void DeserializeEnumByName ()
  940. {
  941. // enum is parsed into long
  942. Deserialize (@"""Red""", typeof (Colors));
  943. }
  944. [Test]
  945. public void DeserializeEnum2 ()
  946. {
  947. object o = Deserialize ("0", typeof (Colors));
  948. Assert.AreEqual (typeof (Colors), o.GetType (), "#de3");
  949. Colors c = (Colors) o;
  950. Assert.AreEqual (Colors.Red, c, "#de4");
  951. }
  952. [Test]
  953. [ExpectedException (typeof (SerializationException))]
  954. public void DeserializeEnumInvalid ()
  955. {
  956. Deserialize ("", typeof (Colors));
  957. }
  958. [Test]
  959. [ExpectedException (typeof (SerializationException))]
  960. [Category ("NotDotNet")] // 0.0 is an invalid Colors value.
  961. public void DeserializeEnumInvalid3 ()
  962. {
  963. //"0.0" instead of "0"
  964. Deserialize (
  965. "0.0",
  966. typeof (Colors));
  967. }
  968. [Test]
  969. public void DeserializeEnumWithDC ()
  970. {
  971. object o = Deserialize ("0", typeof (ColorsWithDC));
  972. Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
  973. ColorsWithDC cdc = (ColorsWithDC) o;
  974. Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
  975. }
  976. [Test]
  977. [ExpectedException (typeof (SerializationException))]
  978. [Category ("NotDotNet")] // 4 is an invalid Colors value.
  979. [Category ("NotWorking")]
  980. public void DeserializeEnumWithDCInvalid ()
  981. {
  982. Deserialize (
  983. "4",
  984. typeof (ColorsWithDC));
  985. }
  986. [Test]
  987. public void DeserializeDCWithEnum ()
  988. {
  989. object o = Deserialize (
  990. "{\"_colors\":0}",
  991. typeof (DCWithEnum));
  992. Assert.AreEqual (typeof (DCWithEnum), o.GetType (), "#de7");
  993. DCWithEnum dc = (DCWithEnum) o;
  994. Assert.AreEqual (Colors.Red, dc.colors, "#de8");
  995. }
  996. [Test]
  997. public void ReadObjectVerifyObjectNameFalse ()
  998. {
  999. string xml = @"<any><Member1>bar</Member1></any>";
  1000. object o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  1001. .ReadObject (XmlReader.Create (new StringReader (xml)), false);
  1002. Assert.IsTrue (o is VerifyObjectNameTestData, "#1");
  1003. string xml2 = @"<any><x:Member1 xmlns:x=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">bar</x:Member1></any>";
  1004. o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  1005. .ReadObject (XmlReader.Create (new StringReader (xml2)), false);
  1006. Assert.IsTrue (o is VerifyObjectNameTestData, "#2");
  1007. }
  1008. [Test]
  1009. [ExpectedException (typeof (SerializationException))]
  1010. public void ReadObjectVerifyObjectNameTrue ()
  1011. {
  1012. string xml = @"<any><Member1>bar</Member1></any>";
  1013. new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  1014. .ReadObject (XmlReader.Create (new StringReader (xml)), true);
  1015. }
  1016. [Test] // member name is out of scope
  1017. public void ReadObjectVerifyObjectNameTrue2 ()
  1018. {
  1019. string xml = @"<root><Member2>bar</Member2></root>";
  1020. new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  1021. .ReadObject (XmlReader.Create (new StringReader (xml)), true);
  1022. }
  1023. [Test]
  1024. public void ReadTypedObjectJson ()
  1025. {
  1026. object o = Deserialize (@"{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}", typeof (DCWithEnum));
  1027. Assert.AreEqual (typeof (DCWithEnum), o.GetType ());
  1028. }
  1029. [Test]
  1030. public void ReadObjectDCArrayJson ()
  1031. {
  1032. object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}]",
  1033. typeof (object []), typeof (DCWithEnum));
  1034. Assert.AreEqual (typeof (object []), o.GetType (), "#1");
  1035. object [] arr = (object []) o;
  1036. Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
  1037. }
  1038. [Test]
  1039. public void ReadObjectDCArray2Json ()
  1040. {
  1041. object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
  1042. typeof (object []), typeof (DCWithEnum), typeof (DCSimple1));
  1043. Assert.AreEqual (typeof (object []), o.GetType (), "#1");
  1044. object [] arr = (object []) o;
  1045. Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
  1046. Assert.AreEqual (typeof (DCSimple1), arr [1].GetType (), "#3");
  1047. }
  1048. private object Deserialize (string xml, Type type, params Type [] knownTypes)
  1049. {
  1050. DataContractJsonSerializer ser = new DataContractJsonSerializer (type, knownTypes);
  1051. XmlReader xr = JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (xml), new XmlDictionaryReaderQuotas ());
  1052. return ser.ReadObject (xr);
  1053. }
  1054. [Test]
  1055. public void IsStartObject ()
  1056. {
  1057. DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1));
  1058. Assert.IsTrue (s.IsStartObject (XmlReader.Create (new StringReader ("<root></root>"))), "#1");
  1059. Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<dummy></dummy>"))), "#2");
  1060. Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<Foo></Foo>"))), "#3");
  1061. Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<root xmlns='urn:foo'></root>"))), "#4");
  1062. }
  1063. [Test]
  1064. public void SerializeNonDC2 ()
  1065. {
  1066. var ser = new DataContractJsonSerializer (typeof (TestData));
  1067. StringWriter sw = new StringWriter ();
  1068. var obj = new TestData () { Foo = "foo", Bar = "bar", Baz = "baz" };
  1069. // XML
  1070. using (var xw = XmlWriter.Create (sw))
  1071. ser.WriteObject (xw, obj);
  1072. var s = sw.ToString ();
  1073. // since the order is not preserved, we compare only contents.
  1074. Assert.IsTrue (s.IndexOf ("<Foo>foo</Foo>") > 0, "#1-1");
  1075. Assert.IsTrue (s.IndexOf ("<Bar>bar</Bar>") > 0, "#1-2");
  1076. Assert.IsFalse (s.IndexOf ("<Baz>baz</Baz>") > 0, "#1-3");
  1077. // JSON
  1078. MemoryStream ms = new MemoryStream ();
  1079. using (var xw = JsonReaderWriterFactory.CreateJsonWriter (ms))
  1080. ser.WriteObject (ms, obj);
  1081. s = new StreamReader (new MemoryStream (ms.ToArray ())).ReadToEnd ().Replace ('"', '/');
  1082. // since the order is not preserved, we compare only contents.
  1083. Assert.IsTrue (s.IndexOf ("/Foo/:/foo/") > 0, "#2-1");
  1084. Assert.IsTrue (s.IndexOf ("/Bar/:/bar/") > 0, "#2-2");
  1085. Assert.IsFalse (s.IndexOf ("/Baz/:/baz/") > 0, "#2-3");
  1086. }
  1087. }
  1088. public class TestData
  1089. {
  1090. public string Foo { get; set; }
  1091. public string Bar { get; set; }
  1092. internal string Baz { get; set; }
  1093. }
  1094. public enum Colors {
  1095. Red, Green, Blue
  1096. }
  1097. [DataContract (Name = "_ColorsWithDC")]
  1098. public enum ColorsWithDC {
  1099. [EnumMember (Value = "_Red")]
  1100. Red,
  1101. [EnumMember]
  1102. Green,
  1103. Blue
  1104. }
  1105. public enum ColorsEnumMemberNoDC {
  1106. [EnumMember (Value = "_Red")]
  1107. Red,
  1108. [EnumMember]
  1109. Green,
  1110. Blue
  1111. }
  1112. [DataContract]
  1113. public class DCWithEnum {
  1114. [DataMember (Name = "_colors")]
  1115. public Colors colors;
  1116. }
  1117. [DataContract]
  1118. public class DCEmpty
  1119. {
  1120. // serializer doesn't touch it.
  1121. public string Foo = "TEST";
  1122. }
  1123. [DataContract]
  1124. public class DCSimple1
  1125. {
  1126. [DataMember]
  1127. public string Foo = "TEST";
  1128. }
  1129. [DataContract]
  1130. public class DCHasNonDC
  1131. {
  1132. [DataMember]
  1133. public NonDC Hoge= new NonDC ();
  1134. }
  1135. public class NonDC
  1136. {
  1137. public string Whee = "whee!";
  1138. }
  1139. [DataContract]
  1140. public class DCHasSerializable
  1141. {
  1142. [DataMember]
  1143. public SimpleSer1 Ser = new SimpleSer1 ();
  1144. }
  1145. [DataContract (Name = "Foo")]
  1146. public class DCWithName
  1147. {
  1148. [DataMember (Name = "FooMember")]
  1149. public string DMWithName = "value";
  1150. }
  1151. [DataContract (Name = "")]
  1152. public class DCWithEmptyName
  1153. {
  1154. }
  1155. [DataContract (Name = null)]
  1156. public class DCWithNullName
  1157. {
  1158. }
  1159. [DataContract (Namespace = "")]
  1160. public class DCWithEmptyNamespace
  1161. {
  1162. }
  1163. [Serializable]
  1164. public class SimpleSer1
  1165. {
  1166. public string Doh = "doh!";
  1167. }
  1168. public class Wrapper
  1169. {
  1170. [DataContract]
  1171. public class DCWrapped
  1172. {
  1173. }
  1174. }
  1175. [DataContract]
  1176. public class CollectionContainer
  1177. {
  1178. Collection<string> items = new Collection<string> ();
  1179. [DataMember]
  1180. public Collection<string> Items {
  1181. get { return items; }
  1182. }
  1183. }
  1184. [CollectionDataContract]
  1185. public class DataCollection<T> : Collection<T>
  1186. {
  1187. }
  1188. [DataContract]
  1189. public class DataCollectionContainer
  1190. {
  1191. DataCollection<string> items = new DataCollection<string> ();
  1192. [DataMember]
  1193. public DataCollection<string> Items {
  1194. get { return items; }
  1195. }
  1196. }
  1197. [DataContract]
  1198. class SerializeNonDCArrayType
  1199. {
  1200. [DataMember]
  1201. public IPAddress [] IPAddresses = new IPAddress [0];
  1202. }
  1203. [DataContract]
  1204. public class VerifyObjectNameTestData
  1205. {
  1206. [DataMember]
  1207. string Member1 = "foo";
  1208. }
  1209. }
  1210. [DataContract]
  1211. class GlobalSample1
  1212. {
  1213. }