PageRenderTime 526ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wojilu.Test/Common/Jsons/ObjectToJsonString.cs

https://bitbucket.org/kingshine/wojilu
C# | 360 lines | 285 code | 73 blank | 2 comment | 0 complexity | 08196d32b112716bad5120c2393db5c0 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. using wojilu.Serialization;
  7. using System.Reflection;
  8. using wojilu.Reflection;
  9. using wojilu.ORM;
  10. using System.Collections;
  11. namespace wojilu.Test.Common.Jsons {
  12. [TestFixture]
  13. public class ObjectToJsonString {
  14. [Test]
  15. public void testHashtable() {
  16. Hashtable dic = new Hashtable();
  17. dic.Add( "name", "sunweb" );
  18. dic.Add( "age", 99 );
  19. dic.Add( "gender", "male" );
  20. string str = JsonString.ConvertDictionary( dic, false );
  21. Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str );
  22. MyPhone phone = new MyPhone();
  23. phone.Name = "新闻大事690501468";
  24. phone.Owner = new PhoneOwner { Id = 2 };
  25. dic.Add( "phone", phone );
  26. str = JsonString.ConvertDictionary( dic, false );
  27. Console.WriteLine( str );
  28. }
  29. [Test]
  30. public void testDictionary() {
  31. Dictionary<string, object> dic = new Dictionary<string, object>();
  32. dic.Add( "name", "sunweb" );
  33. dic.Add( "age", 99 );
  34. dic.Add( "gender", "male" );
  35. string str = JsonString.ConvertDictionary( dic, false );
  36. Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str );
  37. // 将对象放入dic中
  38. MyPhone phone = new MyPhone();
  39. phone.Name = "新闻大事690501468";
  40. phone.Owner = new PhoneOwner { Id = 2 };
  41. dic.Add( "phone", phone );
  42. str = JsonString.ConvertDictionary( dic, false );
  43. Console.WriteLine( str );
  44. Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\", \"phone\":{ \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } } }", str );
  45. }
  46. [Test]
  47. public void testDicToString() {
  48. Dictionary<string, object> dic = new Dictionary<string, object>();
  49. dic["title"] = "abcd";
  50. dic["user"] = "john";
  51. dic["body"] = "mycontent";
  52. Assert.AreEqual( "{ \"title\":\"abcd\", \"user\":\"john\", \"body\":\"mycontent\" }", JSON.DicToString( dic ) );
  53. string lnk = "<a href=\"http://www.163.com/news.html\">这是文章标题</a>"; // 双引号需要转义
  54. dic["postLink"] = lnk;
  55. string expected = @"{ ""title"":""abcd"", ""user"":""john"", ""body"":""mycontent"", ""postLink"":""<a href=\""http://www.163.com/news.html\"">这是文章标题</a>"" }";
  56. Console.WriteLine( JSON.DicToString( dic ) );
  57. Assert.AreEqual( expected, JSON.DicToString( dic ) );
  58. Dictionary<string, object> result = JSON.ToDictionary( expected );
  59. Assert.AreEqual( 4, result.Count );
  60. Assert.AreEqual( lnk, result["postLink"] );
  61. }
  62. [Test]
  63. public void testEscape() {
  64. Dictionary<string, object> dic = new Dictionary<string, object>();
  65. dic["title"] = "abcd";
  66. dic["user"] = "john";
  67. dic["body"] = "my'content"; // 单引号不需要转义
  68. Assert.AreEqual( "{ \"title\":\"abcd\", \"user\":\"john\", \"body\":\"my'content\" }", JSON.DicToString( dic ) );
  69. dic["other"] = "my name is:china"; // 冒号不需要转义
  70. String expected = "{ \"title\":\"abcd\", \"user\":\"john\", \"body\":\"my'content\", \"other\":\"my name is:china\" }";
  71. String actual = JSON.DicToString( dic );
  72. Console.WriteLine( expected );
  73. Console.WriteLine( actual );
  74. Assert.AreEqual( expected, actual );
  75. Dictionary<string, object> result = JSON.ToDictionary( expected );
  76. Assert.AreEqual( 4, result.Count );
  77. Assert.AreEqual( "my'content", result["body"] );
  78. Assert.AreEqual( "my name is:china", result["other"] );
  79. }
  80. [Test]
  81. public void testEscapeEscape() {
  82. String blog = string.Format( "<a href=\"{0}\">{1}</a>", "/space/abcde/Blog22/Post/195", @"\framework\views\Common" );
  83. Dictionary<string, object> dic = new Dictionary<string, object>();
  84. dic.Add( "blog", blog );
  85. String str = JsonString.ConvertDictionary( dic, false );
  86. Console.WriteLine( str );
  87. Assert.AreEqual( @"{ ""blog"":""<a href=\""/space/abcde/Blog22/Post/195\"">\\framework\\views\\Common</a>"" }", str );
  88. Dictionary<String, object> mydic = JSON.ToDictionary( str );
  89. Assert.AreEqual( "<a href=\"/space/abcde/Blog22/Post/195\">\\framework\\views\\Common</a>", mydic["blog"] );
  90. }
  91. // 换行内容测试
  92. [Test]
  93. public void testBreakLine() {
  94. Dictionary<string, object> dic = new Dictionary<string, object>();
  95. dic["title"] = "abcd";
  96. dic["user"] = "john";
  97. dic["body"] = "my"+Environment.NewLine+"content"; //字符串里面有换行
  98. String expected = JSON.DicToString( dic );
  99. Assert.AreEqual( "{ \"title\":\"abcd\", \"user\":\"john\", \"body\":\"mycontent\" }", expected );
  100. Dictionary<string, object> result = JSON.ToDictionary( expected );
  101. Assert.AreEqual( 3, result.Count );
  102. }
  103. [Test]
  104. public void testList() {
  105. List<string> list = new List<string>();
  106. list.Add( "123" );
  107. list.Add( "abc" );
  108. list.Add( "" );
  109. list.Add( "name" );
  110. string str = JsonString.ConvertList( list );
  111. Assert.AreEqual( "[ \"123\", \"abc\", \"\", \"name\" ]", str );
  112. string newStr = JsonString.Convert( list );
  113. Assert.AreEqual( str, newStr );
  114. ArrayList nlist = new ArrayList();
  115. nlist.Add( 123 );
  116. nlist.Add( "abc" );
  117. nlist.Add( "" );
  118. nlist.Add( "name" );
  119. string nstr = JsonString.Convert( nlist );
  120. Assert.AreEqual( "[ 123, \"abc\", \"\", \"name\" ]", nstr );
  121. MyPhone phone = new MyPhone();
  122. phone.Name = "新闻大事690501468";
  123. phone.Owner = new PhoneOwner { Id = 2 };
  124. ArrayList mylist = new ArrayList();
  125. mylist.Add( "abc" );
  126. mylist.Add( 123 );
  127. mylist.Add( phone );
  128. string mystr = JsonString.Convert( mylist );
  129. Assert.AreEqual( "[ \"abc\", 123, { \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } } ]", mystr );
  130. }
  131. [Test]
  132. public void testSimpleObject() {
  133. MyPhone phone = new MyPhone();
  134. phone.Name = "新闻大事690501468";
  135. phone.Owner = new PhoneOwner { Id = 2 };
  136. string strJson = JsonString.ConvertObject( phone );
  137. Console.WriteLine( strJson );
  138. string result = " { \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } }";
  139. Assert.AreEqual( result.Trim(), strJson.Trim() );
  140. }
  141. [Test]
  142. public void testArray() {
  143. string[] arr = new string[] {
  144. "123", "abc", "", "name"
  145. };
  146. string str = JsonString.ConvertArray( arr );
  147. Assert.AreEqual( "[ \"123\", \"abc\", \"\", \"name\" ]", str );
  148. string newStr = JsonString.Convert( arr );
  149. Assert.AreEqual( str, newStr );
  150. }
  151. [Test]
  152. public void testNumber() {
  153. string str = JsonString.Convert( 123 );
  154. Assert.AreEqual( str, "123" );
  155. str = JsonString.Convert( 12.33 );
  156. Assert.AreEqual( str, "12.33" );
  157. str = JsonString.Convert( -3.5 );
  158. Assert.AreEqual( str, "-3.5" );
  159. }
  160. [Test]
  161. public void testString() {
  162. string str = JsonString.Convert( "" );
  163. Assert.AreEqual( str, "\"\"" );
  164. str = JsonString.Convert( null );
  165. Assert.AreEqual( str, "\"\"" );
  166. str = JsonString.Convert( "123" );
  167. Assert.AreEqual( str, "\"123\"" );
  168. str = JsonString.Convert( @"\framework\views\Common" );
  169. Console.WriteLine( str );
  170. Assert.AreEqual( str, @"""\\framework\\views\\Common""" );
  171. }
  172. [Test]
  173. public void testBool() {
  174. string str = JsonString.Convert( false );
  175. Assert.AreEqual( "false", str );
  176. str = JsonString.Convert( true );
  177. Assert.AreEqual( "true", str );
  178. }
  179. [Test]
  180. public void testDateTime() {
  181. string str = JsonString.Convert( new DateTime( 1999, 12, 6, 17, 25, 58 ) );
  182. //Assert.AreEqual( "\"1999-12-6 17:25:58\"", str );
  183. Console.WriteLine( str);
  184. }
  185. //---------------------------------------------------------------------------------
  186. [Test]
  187. public void ObjectsToString() {
  188. IList results = new ArrayList();
  189. results.Add( getPhoneList() );
  190. results.Add( getPhoneList() );
  191. results.Add( getPhoneList() );
  192. results.Add( getPhoneList() );
  193. string strJson = SimpleJsonString.ConvertList( results );
  194. Console.WriteLine( strJson );
  195. string result = @"
  196. [
  197. { Id:0, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
  198. { Id:0, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
  199. { Id:0, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
  200. { Id:0, Name:""新闻大事690501468"", Weight:0, Owner:""2"" }
  201. ]
  202. ";
  203. Assert.AreEqual( result.Trim(), strJson.Trim() );
  204. }
  205. private static MyPhone getPhoneList() {
  206. MyPhone phone = new MyPhone();
  207. phone.Name = "新闻大事690501468";
  208. phone.Owner = new PhoneOwner { Id = 2 };
  209. return phone;
  210. }
  211. [Test]
  212. public void testSubObjects() {
  213. AdminMenu m = new AdminMenu {
  214. Id = "3", Name = "新闻", Url = "/abc.aspx"
  215. };
  216. String str = SimpleJsonString.ConvertObject( m );
  217. Console.WriteLine( str );
  218. AdminMenuGroup g = new AdminMenuGroup();
  219. g.Id = "118";
  220. g.Name = "菜单组";
  221. g.AdminMenus = new List<AdminMenu>();
  222. g.AdminMenus.Add( m );
  223. //String subStr = JSON.ObjectToJSON( g );
  224. String subStr = JsonString.Convert( g );
  225. Console.WriteLine( subStr );
  226. }
  227. [Test]
  228. public void testDbConfig() {
  229. MyDbConfig cf = new MyDbConfig();
  230. cf.ConnectionStringTable = new Dictionary<string, object>();
  231. cf.ConnectionStringTable.Add( "default", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb" );
  232. cf.ConnectionStringTable.Add( "db2", "server=localhost;uid=wojilusyy;pwd=test123;database=syyWojilu;" );
  233. cf.AssemblyList = new List<object>();
  234. cf.AssemblyList.Add( "wojilu.Core" );
  235. cf.AssemblyList.Add( "wojilu.Apps" );
  236. string str = JsonString.Convert( cf, true );
  237. Console.WriteLine( str );
  238. }
  239. }
  240. public class AdminMenuGroup {
  241. public List<AdminMenu> AdminMenus { get; set; }
  242. public string Id { get; set; }
  243. public string Name { get; set; }
  244. }
  245. public class AdminMenu {
  246. public string Id { get; set; }
  247. public string Name { get; set; }
  248. public string Url { get; set; }
  249. public string Info { get; set; }
  250. }
  251. }