PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Json45r7/Source/Src/Newtonsoft.Json.Tests/Converters/ExpandoObjectConverterTests.cs

https://bitbucket.org/wantstudios/bitbucketclient
C# | 190 lines | 140 code | 28 blank | 22 comment | 7 complexity | f8e6cbfb5fed0c60eebde4422571976b MD5 | raw file
  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. #if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
  26. using System;
  27. using System.Collections.Generic;
  28. #if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
  29. using System.Data.Linq;
  30. #endif
  31. #if !SILVERLIGHT && !NETFX_CORE
  32. using System.Data.SqlTypes;
  33. #endif
  34. using System.Dynamic;
  35. using System.Linq;
  36. using System.Text;
  37. using Newtonsoft.Json.Converters;
  38. #if !NETFX_CORE
  39. using NUnit.Framework;
  40. #else
  41. using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
  42. using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
  43. using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
  44. #endif
  45. using Newtonsoft.Json.Tests.TestObjects;
  46. namespace Newtonsoft.Json.Tests.Converters
  47. {
  48. [TestFixture]
  49. public class ExpandoObjectConverterTests : TestFixtureBase
  50. {
  51. public class ExpandoContainer
  52. {
  53. public string Before { get; set; }
  54. public ExpandoObject Expando { get; set; }
  55. public string After { get; set; }
  56. }
  57. [Test]
  58. public void SerializeExpandoObject()
  59. {
  60. ExpandoContainer d = new ExpandoContainer
  61. {
  62. Before = "Before!",
  63. Expando = new ExpandoObject(),
  64. After = "After!"
  65. };
  66. dynamic o = d.Expando;
  67. o.String = "String!";
  68. o.Integer = 234;
  69. o.Float = 1.23d;
  70. o.List = new List<string> {"First", "Second", "Third"};
  71. o.Object = new Dictionary<string, object>
  72. {
  73. {"First", 1}
  74. };
  75. string json = JsonConvert.SerializeObject(d, Formatting.Indented);
  76. Assert.AreEqual(@"{
  77. ""Before"": ""Before!"",
  78. ""Expando"": {
  79. ""String"": ""String!"",
  80. ""Integer"": 234,
  81. ""Float"": 1.23,
  82. ""List"": [
  83. ""First"",
  84. ""Second"",
  85. ""Third""
  86. ],
  87. ""Object"": {
  88. ""First"": 1
  89. }
  90. },
  91. ""After"": ""After!""
  92. }", json);
  93. }
  94. [Test]
  95. public void SerializeNullExpandoObject()
  96. {
  97. ExpandoContainer d = new ExpandoContainer();
  98. string json = JsonConvert.SerializeObject(d, Formatting.Indented);
  99. Assert.AreEqual(@"{
  100. ""Before"": null,
  101. ""Expando"": null,
  102. ""After"": null
  103. }", json);
  104. }
  105. [Test]
  106. public void DeserializeExpandoObject()
  107. {
  108. string json = @"{
  109. ""Before"": ""Before!"",
  110. ""Expando"": {
  111. ""String"": ""String!"",
  112. ""Integer"": 234,
  113. ""Float"": 1.23,
  114. ""List"": [
  115. ""First"",
  116. ""Second"",
  117. ""Third""
  118. ],
  119. ""Object"": {
  120. ""First"": 1
  121. }
  122. },
  123. ""After"": ""After!""
  124. }";
  125. ExpandoContainer o = JsonConvert.DeserializeObject<ExpandoContainer>(json);
  126. Assert.AreEqual(o.Before, "Before!");
  127. Assert.AreEqual(o.After, "After!");
  128. Assert.IsNotNull(o.Expando);
  129. dynamic d = o.Expando;
  130. CustomAssert.IsInstanceOfType(typeof(ExpandoObject), d);
  131. Assert.AreEqual("String!", d.String);
  132. CustomAssert.IsInstanceOfType(typeof(string), d.String);
  133. Assert.AreEqual(234, d.Integer);
  134. CustomAssert.IsInstanceOfType(typeof(long), d.Integer);
  135. Assert.AreEqual(1.23, d.Float);
  136. CustomAssert.IsInstanceOfType(typeof(double), d.Float);
  137. Assert.IsNotNull(d.List);
  138. Assert.AreEqual(3, d.List.Count);
  139. CustomAssert.IsInstanceOfType(typeof(List<object>), d.List);
  140. Assert.AreEqual("First", d.List[0]);
  141. CustomAssert.IsInstanceOfType(typeof(string), d.List[0]);
  142. Assert.AreEqual("Second", d.List[1]);
  143. Assert.AreEqual("Third", d.List[2]);
  144. Assert.IsNotNull(d.Object);
  145. CustomAssert.IsInstanceOfType(typeof(ExpandoObject), d.Object);
  146. Assert.AreEqual(1, d.Object.First);
  147. CustomAssert.IsInstanceOfType(typeof(long), d.Object.First);
  148. }
  149. [Test]
  150. public void DeserializeNullExpandoObject()
  151. {
  152. string json = @"{
  153. ""Before"": null,
  154. ""Expando"": null,
  155. ""After"": null
  156. }";
  157. ExpandoContainer c = JsonConvert.DeserializeObject<ExpandoContainer>(json);
  158. Assert.AreEqual(null, c.Expando);
  159. }
  160. }
  161. }
  162. #endif