DataContractJsonSerializerTest.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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. public void SerializeNonDC ()
  336. {
  337. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (NonDC));
  338. using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
  339. ser.WriteObject (w, new NonDC ());
  340. }
  341. }
  342. // DCHasNonDC
  343. [Test]
  344. [ExpectedException (typeof (InvalidDataContractException))]
  345. // DCHasNonDC itself is a DataContract type whose field is
  346. // marked as DataMember but its type is not DataContract.
  347. public void SerializeDCHasNonDC ()
  348. {
  349. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasNonDC));
  350. using (XmlWriter w = XmlWriter.Create (TextWriter.Null, settings)) {
  351. ser.WriteObject (w, new DCHasNonDC ());
  352. }
  353. }
  354. // DCHasSerializable
  355. [Test]
  356. public void SerializeSimpleSerializable1Xml ()
  357. {
  358. StringWriter sw = new StringWriter ();
  359. SerializeSimpleSerializable1 (XmlWriter.Create (sw, settings));
  360. Assert.AreEqual (
  361. @"<root type=""object""><Ser type=""object""><Doh>doh!</Doh></Ser></root>",
  362. sw.ToString ());
  363. }
  364. [Test]
  365. public void SerializeSimpleSerializable1Json ()
  366. {
  367. MemoryStream ms = new MemoryStream ();
  368. SerializeSimpleSerializable1 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  369. Assert.AreEqual (
  370. @"{""Ser"":{""Doh"":""doh!""}}",
  371. Encoding.UTF8.GetString (ms.ToArray ()));
  372. }
  373. // DCHasSerializable itself is DataContract and has a field
  374. // whose type is not contract but serializable.
  375. void SerializeSimpleSerializable1 (XmlWriter writer)
  376. {
  377. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCHasSerializable));
  378. using (XmlWriter w = writer) {
  379. ser.WriteObject (w, new DCHasSerializable ());
  380. }
  381. }
  382. [Test]
  383. public void SerializeDCWithNameXml ()
  384. {
  385. StringWriter sw = new StringWriter ();
  386. SerializeDCWithName (XmlWriter.Create (sw, settings));
  387. Assert.AreEqual (
  388. @"<root type=""object""><FooMember>value</FooMember></root>",
  389. sw.ToString ());
  390. }
  391. [Test]
  392. public void SerializeDCWithNameJson ()
  393. {
  394. MemoryStream ms = new MemoryStream ();
  395. SerializeDCWithName (JsonReaderWriterFactory.CreateJsonWriter (ms));
  396. Assert.AreEqual (
  397. @"{""FooMember"":""value""}",
  398. Encoding.UTF8.GetString (ms.ToArray ()));
  399. }
  400. void SerializeDCWithName (XmlWriter writer)
  401. {
  402. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
  403. using (XmlWriter w = writer) {
  404. ser.WriteObject (w, new DCWithName ());
  405. }
  406. }
  407. [Test]
  408. public void SerializeDCWithEmptyName1 ()
  409. {
  410. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyName));
  411. StringWriter sw = new StringWriter ();
  412. DCWithEmptyName dc = new DCWithEmptyName ();
  413. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  414. try {
  415. ser.WriteObject (w, dc);
  416. } catch (InvalidDataContractException) {
  417. return;
  418. }
  419. }
  420. Assert.Fail ("Expected InvalidDataContractException");
  421. }
  422. [Test]
  423. public void SerializeDCWithEmptyName2 ()
  424. {
  425. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithName));
  426. StringWriter sw = new StringWriter ();
  427. /* DataContractAttribute.Name == "", not valid */
  428. DCWithEmptyName dc = new DCWithEmptyName ();
  429. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  430. try {
  431. ser.WriteObject (w, dc);
  432. } catch (InvalidDataContractException) {
  433. return;
  434. }
  435. }
  436. Assert.Fail ("Expected InvalidDataContractException");
  437. }
  438. [Test]
  439. [Category("NotWorking")]
  440. public void SerializeDCWithNullName ()
  441. {
  442. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithNullName));
  443. StringWriter sw = new StringWriter ();
  444. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  445. try {
  446. /* DataContractAttribute.Name == "", not valid */
  447. ser.WriteObject (w, new DCWithNullName ());
  448. } catch (InvalidDataContractException) {
  449. return;
  450. }
  451. }
  452. Assert.Fail ("Expected InvalidDataContractException");
  453. }
  454. [Test]
  455. public void SerializeDCWithEmptyNamespace1 ()
  456. {
  457. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEmptyNamespace));
  458. StringWriter sw = new StringWriter ();
  459. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  460. ser.WriteObject (w, new DCWithEmptyNamespace ());
  461. }
  462. }
  463. [Test]
  464. public void SerializeWrappedClassXml ()
  465. {
  466. StringWriter sw = new StringWriter ();
  467. SerializeWrappedClass (XmlWriter.Create (sw, settings));
  468. Assert.AreEqual (
  469. @"<root type=""object"" />",
  470. sw.ToString ());
  471. }
  472. [Test]
  473. public void SerializeWrappedClassJson ()
  474. {
  475. MemoryStream ms = new MemoryStream ();
  476. SerializeWrappedClass (JsonReaderWriterFactory.CreateJsonWriter (ms));
  477. Assert.AreEqual (
  478. "{}",
  479. Encoding.UTF8.GetString (ms.ToArray ()));
  480. }
  481. void SerializeWrappedClass (XmlWriter writer)
  482. {
  483. DataContractJsonSerializer ser =
  484. new DataContractJsonSerializer (typeof (Wrapper.DCWrapped));
  485. using (XmlWriter w = writer) {
  486. ser.WriteObject (w, new Wrapper.DCWrapped ());
  487. }
  488. }
  489. // CollectionContainer : Items must have a setter.
  490. [Test]
  491. [ExpectedException (typeof (InvalidDataContractException))]
  492. public void SerializeReadOnlyCollectionMember ()
  493. {
  494. DataContractJsonSerializer ser =
  495. new DataContractJsonSerializer (typeof (CollectionContainer));
  496. StringWriter sw = new StringWriter ();
  497. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  498. ser.WriteObject (w, null);
  499. }
  500. }
  501. // DataCollectionContainer : Items must have a setter.
  502. [Test]
  503. [ExpectedException (typeof (InvalidDataContractException))]
  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. public void SerializeGuidXml ()
  515. {
  516. StringWriter sw = new StringWriter ();
  517. SerializeGuid (XmlWriter.Create (sw, settings));
  518. Assert.AreEqual (
  519. @"<root>00000000-0000-0000-0000-000000000000</root>",
  520. sw.ToString ());
  521. }
  522. [Test]
  523. public void SerializeGuidJson ()
  524. {
  525. MemoryStream ms = new MemoryStream ();
  526. SerializeGuid (JsonReaderWriterFactory.CreateJsonWriter (ms));
  527. Assert.AreEqual (
  528. @"""00000000-0000-0000-0000-000000000000""",
  529. Encoding.UTF8.GetString (ms.ToArray ()));
  530. }
  531. void SerializeGuid (XmlWriter writer)
  532. {
  533. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Guid));
  534. using (XmlWriter w = writer) {
  535. ser.WriteObject (w, Guid.Empty);
  536. }
  537. }
  538. [Test]
  539. public void SerializeEnumXml ()
  540. {
  541. StringWriter sw = new StringWriter ();
  542. SerializeEnum (XmlWriter.Create (sw, settings));
  543. Assert.AreEqual (
  544. @"<root type=""number"">0</root>",
  545. sw.ToString ());
  546. }
  547. [Test]
  548. public void SerializeEnumJson ()
  549. {
  550. MemoryStream ms = new MemoryStream ();
  551. SerializeEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
  552. Assert.AreEqual (
  553. "0",
  554. Encoding.UTF8.GetString (ms.ToArray ()));
  555. }
  556. void SerializeEnum (XmlWriter writer)
  557. {
  558. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
  559. using (XmlWriter w = writer) {
  560. ser.WriteObject (w, new Colors ());
  561. }
  562. }
  563. [Test]
  564. public void SerializeEnum2Xml ()
  565. {
  566. StringWriter sw = new StringWriter ();
  567. SerializeEnum2 (XmlWriter.Create (sw, settings));
  568. Assert.AreEqual (
  569. @"<root type=""number"">0</root>",
  570. sw.ToString ());
  571. }
  572. [Test]
  573. public void SerializeEnum2Json ()
  574. {
  575. MemoryStream ms = new MemoryStream ();
  576. SerializeEnum2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  577. Assert.AreEqual (
  578. "0",
  579. Encoding.UTF8.GetString (ms.ToArray ()));
  580. }
  581. void SerializeEnum2 (XmlWriter writer)
  582. {
  583. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (Colors));
  584. using (XmlWriter w = writer) {
  585. ser.WriteObject (w, 0);
  586. }
  587. }
  588. [Test] // so, DataContract does not affect here.
  589. public void SerializeEnumWithDCXml ()
  590. {
  591. StringWriter sw = new StringWriter ();
  592. SerializeEnumWithDC (XmlWriter.Create (sw, settings));
  593. Assert.AreEqual (
  594. @"<root type=""number"">0</root>",
  595. sw.ToString ());
  596. }
  597. [Test]
  598. public void SerializeEnumWithDCJson ()
  599. {
  600. MemoryStream ms = new MemoryStream ();
  601. SerializeEnumWithDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
  602. Assert.AreEqual (
  603. "0",
  604. Encoding.UTF8.GetString (ms.ToArray ()));
  605. }
  606. void SerializeEnumWithDC (XmlWriter writer)
  607. {
  608. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
  609. using (XmlWriter w = writer) {
  610. ser.WriteObject (w, new ColorsWithDC ());
  611. }
  612. }
  613. [Test]
  614. public void SerializeEnumWithNoDCXml ()
  615. {
  616. StringWriter sw = new StringWriter ();
  617. SerializeEnumWithNoDC (XmlWriter.Create (sw, settings));
  618. Assert.AreEqual (
  619. @"<root type=""number"">0</root>",
  620. sw.ToString ());
  621. }
  622. [Test]
  623. public void SerializeEnumWithNoDCJson ()
  624. {
  625. MemoryStream ms = new MemoryStream ();
  626. SerializeEnumWithNoDC (JsonReaderWriterFactory.CreateJsonWriter (ms));
  627. Assert.AreEqual (
  628. "0",
  629. Encoding.UTF8.GetString (ms.ToArray ()));
  630. }
  631. void SerializeEnumWithNoDC (XmlWriter writer)
  632. {
  633. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsEnumMemberNoDC));
  634. using (XmlWriter w = writer) {
  635. ser.WriteObject (w, new ColorsEnumMemberNoDC ());
  636. }
  637. }
  638. [Test]
  639. public void SerializeEnumWithDC2Xml ()
  640. {
  641. StringWriter sw = new StringWriter ();
  642. SerializeEnumWithDC2 (XmlWriter.Create (sw, settings));
  643. Assert.AreEqual (
  644. @"<root type=""number"">3</root>",
  645. sw.ToString ());
  646. }
  647. [Test]
  648. public void SerializeEnumWithDC2Json ()
  649. {
  650. MemoryStream ms = new MemoryStream ();
  651. SerializeEnumWithDC2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  652. Assert.AreEqual (
  653. "3",
  654. Encoding.UTF8.GetString (ms.ToArray ()));
  655. }
  656. void SerializeEnumWithDC2 (XmlWriter writer)
  657. {
  658. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
  659. using (XmlWriter w = writer) {
  660. ser.WriteObject (w, 3);
  661. }
  662. }
  663. /*
  664. [Test]
  665. [ExpectedException (typeof (SerializationException))]
  666. public void SerializeEnumWithDCInvalid ()
  667. {
  668. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (ColorsWithDC));
  669. StringWriter sw = new StringWriter ();
  670. ColorsWithDC cdc = ColorsWithDC.Blue;
  671. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  672. ser.WriteObject (w, cdc);
  673. }
  674. }
  675. */
  676. [Test]
  677. public void SerializeDCWithEnumXml ()
  678. {
  679. StringWriter sw = new StringWriter ();
  680. SerializeDCWithEnum (XmlWriter.Create (sw, settings));
  681. Assert.AreEqual (
  682. @"<root type=""object""><_colors type=""number"">0</_colors></root>",
  683. sw.ToString ());
  684. }
  685. [Test]
  686. public void SerializeDCWithEnumJson ()
  687. {
  688. MemoryStream ms = new MemoryStream ();
  689. SerializeDCWithEnum (JsonReaderWriterFactory.CreateJsonWriter (ms));
  690. Assert.AreEqual (
  691. @"{""_colors"":0}",
  692. Encoding.UTF8.GetString (ms.ToArray ()));
  693. }
  694. void SerializeDCWithEnum (XmlWriter writer)
  695. {
  696. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum));
  697. using (XmlWriter w = writer) {
  698. ser.WriteObject (w, new DCWithEnum ());
  699. }
  700. }
  701. [Test]
  702. public void SerializerDCArrayXml ()
  703. {
  704. StringWriter sw = new StringWriter ();
  705. SerializerDCArray (XmlWriter.Create (sw, settings));
  706. Assert.AreEqual (
  707. @"<root type=""array""><item type=""object""><_colors type=""number"">0</_colors></item><item type=""object""><_colors type=""number"">1</_colors></item></root>",
  708. sw.ToString ());
  709. }
  710. [Test]
  711. public void SerializerDCArrayJson ()
  712. {
  713. MemoryStream ms = new MemoryStream ();
  714. SerializerDCArray (JsonReaderWriterFactory.CreateJsonWriter (ms));
  715. Assert.AreEqual (
  716. @"[{""_colors"":0},{""_colors"":1}]",
  717. Encoding.UTF8.GetString (ms.ToArray ()));
  718. }
  719. void SerializerDCArray (XmlWriter writer)
  720. {
  721. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (DCWithEnum []));
  722. DCWithEnum [] arr = new DCWithEnum [2];
  723. arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
  724. arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
  725. using (XmlWriter w = writer) {
  726. ser.WriteObject (w, arr);
  727. }
  728. }
  729. [Test]
  730. public void SerializerDCArray2Xml ()
  731. {
  732. StringWriter sw = new StringWriter ();
  733. SerializerDCArray2 (XmlWriter.Create (sw, settings));
  734. Assert.AreEqual (
  735. @"<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>",
  736. sw.ToString ());
  737. }
  738. [Test]
  739. public void SerializerDCArray2Json ()
  740. {
  741. MemoryStream ms = new MemoryStream ();
  742. SerializerDCArray2 (JsonReaderWriterFactory.CreateJsonWriter (ms));
  743. Assert.AreEqual (
  744. @"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
  745. Encoding.UTF8.GetString (ms.ToArray ()));
  746. }
  747. void SerializerDCArray2 (XmlWriter writer)
  748. {
  749. List<Type> known = new List<Type> ();
  750. known.Add (typeof (DCWithEnum));
  751. known.Add (typeof (DCSimple1));
  752. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (object []), known);
  753. object [] arr = new object [2];
  754. arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
  755. arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
  756. using (XmlWriter w = writer) {
  757. ser.WriteObject (w, arr);
  758. }
  759. }
  760. [Test]
  761. public void SerializerDCArray3Xml ()
  762. {
  763. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
  764. StringWriter sw = new StringWriter ();
  765. int [] arr = new int [2];
  766. arr [0] = 1; arr [1] = 2;
  767. using (XmlWriter w = XmlWriter.Create (sw, settings)) {
  768. ser.WriteObject (w, arr);
  769. }
  770. Assert.AreEqual (
  771. @"<root type=""array""><item type=""number"">1</item><item type=""number"">2</item></root>",
  772. sw.ToString ());
  773. }
  774. [Test]
  775. public void SerializerDCArray3Json ()
  776. {
  777. MemoryStream ms = new MemoryStream ();
  778. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (int []));
  779. int [] arr = new int [2];
  780. arr [0] = 1; arr [1] = 2;
  781. using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
  782. ser.WriteObject (w, arr);
  783. }
  784. Assert.AreEqual (
  785. @"[1,2]",
  786. Encoding.UTF8.GetString (ms.ToArray ()));
  787. }
  788. [Test]
  789. // ... so, non-JSON XmlWriter is still accepted.
  790. public void SerializeNonDCArrayXml ()
  791. {
  792. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
  793. StringWriter sw = new StringWriter ();
  794. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  795. ser.WriteObject (xw, new SerializeNonDCArrayType ());
  796. }
  797. Assert.AreEqual (@"<root type=""object""><IPAddresses type=""array"" /></root>",
  798. sw.ToString ());
  799. }
  800. [Test]
  801. public void SerializeNonDCArrayJson ()
  802. {
  803. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
  804. MemoryStream ms = new MemoryStream ();
  805. using (XmlWriter xw = JsonReaderWriterFactory.CreateJsonWriter (ms)) {
  806. ser.WriteObject (xw, new SerializeNonDCArrayType ());
  807. }
  808. Assert.AreEqual (@"{""IPAddresses"":[]}",
  809. Encoding.UTF8.GetString (ms.ToArray ()));
  810. }
  811. [Test]
  812. public void SerializeNonDCArrayItems ()
  813. {
  814. DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (SerializeNonDCArrayType));
  815. StringWriter sw = new StringWriter ();
  816. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  817. SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
  818. obj.IPAddresses = new IPAddress [] {new IPAddress (new byte [] {1, 2, 3, 4})};
  819. ser.WriteObject (xw, obj);
  820. }
  821. XmlDocument doc = new XmlDocument ();
  822. doc.LoadXml (sw.ToString ());
  823. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  824. nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
  825. nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
  826. nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
  827. Assert.AreEqual (1, doc.SelectNodes ("/root/IPAddresses/item", nsmgr).Count, "#1");
  828. Assert.AreEqual ("67305985", doc.SelectSingleNode ("/root/IPAddresses/item/m_Address", nsmgr).InnerText, "#2");
  829. XmlElement el = doc.SelectSingleNode ("/root/IPAddresses/item/m_Numbers", nsmgr) as XmlElement;
  830. Assert.IsNotNull (el, "#3");
  831. Assert.AreEqual (8, el.SelectNodes ("item", nsmgr).Count, "#4");
  832. }
  833. [Test]
  834. public void MaxItemsInObjectGraph1 ()
  835. {
  836. // object count == maximum
  837. DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCEmpty), null, 1, false, null, false);
  838. s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCEmpty ());
  839. }
  840. [Test]
  841. [ExpectedException (typeof (SerializationException))]
  842. public void MaxItemsInObjectGraph2 ()
  843. {
  844. // object count > maximum
  845. DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1), null, 1, false, null, false);
  846. s.WriteObject (XmlWriter.Create (TextWriter.Null), new DCSimple1 ());
  847. }
  848. [Test]
  849. public void DeserializeString ()
  850. {
  851. Assert.AreEqual ("ABC", Deserialize ("\"ABC\"", typeof (string)));
  852. }
  853. [Test]
  854. public void DeserializeInt ()
  855. {
  856. Assert.AreEqual (5, Deserialize ("5", typeof (int)));
  857. }
  858. [Test]
  859. public void DeserializeArray ()
  860. {
  861. int [] ret = (int []) Deserialize ("[5,6,7]", typeof (int []));
  862. Assert.AreEqual (5, ret [0], "#1");
  863. Assert.AreEqual (6, ret [1], "#2");
  864. Assert.AreEqual (7, ret [2], "#3");
  865. }
  866. [Test]
  867. public void DeserializeArrayUntyped ()
  868. {
  869. object [] ret = (object []) Deserialize ("[5,6,7]", typeof (object []));
  870. Assert.AreEqual (5, ret [0], "#1");
  871. Assert.AreEqual (6, ret [1], "#2");
  872. Assert.AreEqual (7, ret [2], "#3");
  873. }
  874. [Test]
  875. public void DeserializeMixedArray ()
  876. {
  877. object [] ret = (object []) Deserialize ("[5,\"6\",false]", typeof (object []));
  878. Assert.AreEqual (5, ret [0], "#1");
  879. Assert.AreEqual ("6", ret [1], "#2");
  880. Assert.AreEqual (false, ret [2], "#3");
  881. }
  882. [Test]
  883. [ExpectedException (typeof (SerializationException))]
  884. public void DeserializeEmptyAsString ()
  885. {
  886. // it somehow expects "root" which should have been already consumed.
  887. Deserialize ("", typeof (string));
  888. }
  889. [Test]
  890. [ExpectedException (typeof (SerializationException))]
  891. public void DeserializeEmptyAsInt ()
  892. {
  893. // it somehow expects "root" which should have been already consumed.
  894. Deserialize ("", typeof (int));
  895. }
  896. [Test]
  897. [ExpectedException (typeof (SerializationException))]
  898. public void DeserializeEmptyAsDBNull ()
  899. {
  900. // it somehow expects "root" which should have been already consumed.
  901. Deserialize ("", typeof (DBNull));
  902. }
  903. [Test]
  904. public void DeserializeEmptyObjectAsString ()
  905. {
  906. // looks like it is converted to ""
  907. Assert.AreEqual (String.Empty, Deserialize ("{}", typeof (string)));
  908. }
  909. [Test]
  910. [ExpectedException (typeof (SerializationException))]
  911. public void DeserializeEmptyObjectAsInt ()
  912. {
  913. Deserialize ("{}", typeof (int));
  914. }
  915. [Test]
  916. public void DeserializeEmptyObjectAsDBNull ()
  917. {
  918. Assert.AreEqual (DBNull.Value, Deserialize ("{}", typeof (DBNull)));
  919. }
  920. [Test]
  921. [ExpectedException (typeof (SerializationException))]
  922. public void DeserializeEnumByName ()
  923. {
  924. // enum is parsed into long
  925. Deserialize (@"""Red""", typeof (Colors));
  926. }
  927. [Test]
  928. public void DeserializeEnum2 ()
  929. {
  930. object o = Deserialize ("0", typeof (Colors));
  931. Assert.AreEqual (typeof (Colors), o.GetType (), "#de3");
  932. Colors c = (Colors) o;
  933. Assert.AreEqual (Colors.Red, c, "#de4");
  934. }
  935. [Test]
  936. [ExpectedException (typeof (SerializationException))]
  937. public void DeserializeEnumInvalid ()
  938. {
  939. Deserialize ("", typeof (Colors));
  940. }
  941. [Test]
  942. [ExpectedException (typeof (SerializationException))]
  943. [Category ("NotDotNet")] // 0.0 is an invalid Colors value.
  944. public void DeserializeEnumInvalid3 ()
  945. {
  946. //"0.0" instead of "0"
  947. Deserialize (
  948. "0.0",
  949. typeof (Colors));
  950. }
  951. [Test]
  952. public void DeserializeEnumWithDC ()
  953. {
  954. object o = Deserialize ("0", typeof (ColorsWithDC));
  955. Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
  956. ColorsWithDC cdc = (ColorsWithDC) o;
  957. Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
  958. }
  959. [Test]
  960. [ExpectedException (typeof (SerializationException))]
  961. [Category ("NotDotNet")] // 4 is an invalid Colors value.
  962. [Category ("NotWorking")]
  963. public void DeserializeEnumWithDCInvalid ()
  964. {
  965. Deserialize (
  966. "4",
  967. typeof (ColorsWithDC));
  968. }
  969. [Test]
  970. public void DeserializeDCWithEnum ()
  971. {
  972. object o = Deserialize (
  973. "{\"_colors\":0}",
  974. typeof (DCWithEnum));
  975. Assert.AreEqual (typeof (DCWithEnum), o.GetType (), "#de7");
  976. DCWithEnum dc = (DCWithEnum) o;
  977. Assert.AreEqual (Colors.Red, dc.colors, "#de8");
  978. }
  979. [Test]
  980. public void ReadObjectVerifyObjectNameFalse ()
  981. {
  982. string xml = @"<any><Member1>bar</Member1></any>";
  983. object o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  984. .ReadObject (XmlReader.Create (new StringReader (xml)), false);
  985. Assert.IsTrue (o is VerifyObjectNameTestData, "#1");
  986. string xml2 = @"<any><x:Member1 xmlns:x=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">bar</x:Member1></any>";
  987. o = new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  988. .ReadObject (XmlReader.Create (new StringReader (xml2)), false);
  989. Assert.IsTrue (o is VerifyObjectNameTestData, "#2");
  990. }
  991. [Test]
  992. [ExpectedException (typeof (SerializationException))]
  993. public void ReadObjectVerifyObjectNameTrue ()
  994. {
  995. string xml = @"<any><Member1>bar</Member1></any>";
  996. new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  997. .ReadObject (XmlReader.Create (new StringReader (xml)), true);
  998. }
  999. [Test] // member name is out of scope
  1000. public void ReadObjectVerifyObjectNameTrue2 ()
  1001. {
  1002. string xml = @"<root><Member2>bar</Member2></root>";
  1003. new DataContractJsonSerializer (typeof (VerifyObjectNameTestData))
  1004. .ReadObject (XmlReader.Create (new StringReader (xml)), true);
  1005. }
  1006. [Test]
  1007. public void ReadTypedObjectJson ()
  1008. {
  1009. object o = Deserialize (@"{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}", typeof (DCWithEnum));
  1010. Assert.AreEqual (typeof (DCWithEnum), o.GetType ());
  1011. }
  1012. [Test]
  1013. public void ReadObjectDCArrayJson ()
  1014. {
  1015. object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0}]",
  1016. typeof (object []), typeof (DCWithEnum));
  1017. Assert.AreEqual (typeof (object []), o.GetType (), "#1");
  1018. object [] arr = (object []) o;
  1019. Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
  1020. }
  1021. [Test]
  1022. public void ReadObjectDCArray2Json ()
  1023. {
  1024. object o = Deserialize (@"[{""__type"":""DCWithEnum:#MonoTests.System.Runtime.Serialization.Json"",""_colors"":0},{""__type"":""DCSimple1:#MonoTests.System.Runtime.Serialization.Json"",""Foo"":""hello""}]",
  1025. typeof (object []), typeof (DCWithEnum), typeof (DCSimple1));
  1026. Assert.AreEqual (typeof (object []), o.GetType (), "#1");
  1027. object [] arr = (object []) o;
  1028. Assert.AreEqual (typeof (DCWithEnum), arr [0].GetType (), "#2");
  1029. Assert.AreEqual (typeof (DCSimple1), arr [1].GetType (), "#3");
  1030. }
  1031. private object Deserialize (string xml, Type type, params Type [] knownTypes)
  1032. {
  1033. DataContractJsonSerializer ser = new DataContractJsonSerializer (type, knownTypes);
  1034. XmlReader xr = JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (xml), new XmlDictionaryReaderQuotas ());
  1035. return ser.ReadObject (xr);
  1036. }
  1037. [Test]
  1038. public void IsStartObject ()
  1039. {
  1040. DataContractJsonSerializer s = new DataContractJsonSerializer (typeof (DCSimple1));
  1041. Assert.IsTrue (s.IsStartObject (XmlReader.Create (new StringReader ("<root></root>"))), "#1");
  1042. Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<dummy></dummy>"))), "#2");
  1043. Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<Foo></Foo>"))), "#3");
  1044. Assert.IsFalse (s.IsStartObject (XmlReader.Create (new StringReader ("<root xmlns='urn:foo'></root>"))), "#4");
  1045. }
  1046. }
  1047. public enum Colors {
  1048. Red, Green, Blue
  1049. }
  1050. [DataContract (Name = "_ColorsWithDC")]
  1051. public enum ColorsWithDC {
  1052. [EnumMember (Value = "_Red")]
  1053. Red,
  1054. [EnumMember]
  1055. Green,
  1056. Blue
  1057. }
  1058. public enum ColorsEnumMemberNoDC {
  1059. [EnumMember (Value = "_Red")]
  1060. Red,
  1061. [EnumMember]
  1062. Green,
  1063. Blue
  1064. }
  1065. [DataContract]
  1066. public class DCWithEnum {
  1067. [DataMember (Name = "_colors")]
  1068. public Colors colors;
  1069. }
  1070. [DataContract]
  1071. public class DCEmpty
  1072. {
  1073. // serializer doesn't touch it.
  1074. public string Foo = "TEST";
  1075. }
  1076. [DataContract]
  1077. public class DCSimple1
  1078. {
  1079. [DataMember]
  1080. public string Foo = "TEST";
  1081. }
  1082. [DataContract]
  1083. public class DCHasNonDC
  1084. {
  1085. [DataMember]
  1086. public NonDC Hoge= new NonDC ();
  1087. }
  1088. public class NonDC
  1089. {
  1090. public string Whee = "whee!";
  1091. }
  1092. [DataContract]
  1093. public class DCHasSerializable
  1094. {
  1095. [DataMember]
  1096. public SimpleSer1 Ser = new SimpleSer1 ();
  1097. }
  1098. [DataContract (Name = "Foo")]
  1099. public class DCWithName
  1100. {
  1101. [DataMember (Name = "FooMember")]
  1102. public string DMWithName = "value";
  1103. }
  1104. [DataContract (Name = "")]
  1105. public class DCWithEmptyName
  1106. {
  1107. }
  1108. [DataContract (Name = null)]
  1109. public class DCWithNullName
  1110. {
  1111. }
  1112. [DataContract (Namespace = "")]
  1113. public class DCWithEmptyNamespace
  1114. {
  1115. }
  1116. [Serializable]
  1117. public class SimpleSer1
  1118. {
  1119. public string Doh = "doh!";
  1120. }
  1121. public class Wrapper
  1122. {
  1123. [DataContract]
  1124. public class DCWrapped
  1125. {
  1126. }
  1127. }
  1128. [DataContract]
  1129. public class CollectionContainer
  1130. {
  1131. Collection<string> items = new Collection<string> ();
  1132. [DataMember]
  1133. public Collection<string> Items {
  1134. get { return items; }
  1135. }
  1136. }
  1137. [CollectionDataContract]
  1138. public class DataCollection<T> : Collection<T>
  1139. {
  1140. }
  1141. [DataContract]
  1142. public class DataCollectionContainer
  1143. {
  1144. DataCollection<string> items = new DataCollection<string> ();
  1145. [DataMember]
  1146. public DataCollection<string> Items {
  1147. get { return items; }
  1148. }
  1149. }
  1150. [DataContract]
  1151. class SerializeNonDCArrayType
  1152. {
  1153. [DataMember]
  1154. public IPAddress [] IPAddresses = new IPAddress [0];
  1155. }
  1156. [DataContract]
  1157. public class VerifyObjectNameTestData
  1158. {
  1159. [DataMember]
  1160. string Member1 = "foo";
  1161. }
  1162. }
  1163. [DataContract]
  1164. class GlobalSample1
  1165. {
  1166. }