PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wojilu.Test/Common/Jsons/JsonTest.cs

https://bitbucket.org/kingshine/wojilu
C# | 544 lines | 397 code | 144 blank | 3 comment | 9 complexity | 70e0ec0f377ca59c08e735dbb591df29 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using NUnit.Framework;
  6. using wojilu.Web.Mvc;
  7. using wojilu;
  8. using wojilu.Serialization;
  9. using wojilu.Members.Users.Domain;
  10. namespace wojilu.Test.Common.Jsons {
  11. // 本测试用于解析json字符串
  12. [TestFixture]
  13. public class JsonTest {
  14. [Test]
  15. public void testValue() {
  16. string json = "376";
  17. Assert.AreEqual( 376, JsonParser.Parse( json ) );
  18. json = " false ";
  19. Assert.AreEqual( false, JsonParser.Parse( json ) );
  20. json = " 356 ";
  21. Assert.AreEqual( 356, JsonParser.Parse( json ) );
  22. json = " 3.17 ";
  23. Assert.AreEqual( 3.17, JsonParser.Parse( json ) );
  24. json = " dfaflddak_dfaol ";
  25. Assert.AreEqual( "dfaflddak_dfaol", JsonParser.Parse( json ) );
  26. json = " dfaflddak=dfaol ";
  27. Assert.AreEqual( "dfaflddak=dfaol", JsonParser.Parse( json ) );
  28. json = " dfaflddak#dfaol ";
  29. Assert.AreEqual( "dfaflddak#dfaol", JsonParser.Parse( json ) );
  30. json = " dfaflddak;dfaol ";
  31. Assert.AreEqual( "dfaflddak;dfaol", JsonParser.Parse( json ) );
  32. json = " dfaflddak\\\"dfaol ";
  33. Assert.AreEqual( "dfaflddak\"dfaol", JsonParser.Parse( json ) );
  34. }
  35. [Test]
  36. public void testArray() {
  37. string json = " [ \"zhangsan\", 3, false, \"lisi\"] ";
  38. testArraySimple( json );
  39. testArraySimple2();
  40. json = " [ \"zhangsan\", 3, false, { Name : \"zhangsan\", Gender:\"male\"} ] ";
  41. testObjectArray( json );
  42. }
  43. private void testObjectArray( string json ) {
  44. List<object> list = JsonParser.Parse( json ) as List<object>;
  45. Assert.AreEqual( 4, list.Count );
  46. Assert.AreEqual( "zhangsan", list[0] );
  47. Assert.AreEqual( 3, list[1] );
  48. Assert.AreEqual( false, list[2] );
  49. Dictionary<string, object> map = list[3] as Dictionary<string, object>;
  50. Assert.IsNotNull( map );
  51. Assert.AreEqual( 2, map.Count );
  52. foreach (KeyValuePair<string, object> pair in map) {
  53. Console.WriteLine( pair.Key + ":" + pair.Value );
  54. }
  55. }
  56. private void testArraySimple( string json ) {
  57. List<object> list = JsonParser.Parse( json ) as List<object>;
  58. Assert.AreEqual( 4, list.Count );
  59. Assert.AreEqual( "zhangsan", list[0] );
  60. Assert.AreEqual( 3, list[1] );
  61. Assert.AreEqual( false, list[2] );
  62. Assert.AreEqual( "lisi", list[3] );
  63. }
  64. private void testArraySimple2() {
  65. string json = " [erieroe , 38 , lakkk] ";
  66. List<object> list = JsonParser.Parse( json ) as List<object>;
  67. Assert.AreEqual( 3, list.Count );
  68. Assert.AreEqual( "erieroe", list[0] );
  69. Assert.AreEqual( 38, list[1] );
  70. Assert.AreEqual( "lakkk", list[2] );
  71. }
  72. [Test]
  73. public void testArraySimple3() {
  74. string json = " [ \"zhang\\\"san\", 3, false, \"lisi\"] ";
  75. List<object> list = JsonParser.Parse( json ) as List<object>;
  76. Assert.AreEqual( 4, list.Count );
  77. Assert.AreEqual( "zhang\"san", list[0] );
  78. Assert.AreEqual( 3, list[1] );
  79. Assert.AreEqual( false, list[2] );
  80. Assert.AreEqual( "lisi", list[3] );
  81. }
  82. [Test]
  83. public void testObject() {
  84. string json = "{ \"Name\" : \"zhangsan\", \"Gender\":\"male\"}";
  85. testObjectPrivate( json );
  86. json = "{ Name : \"zhangsan\", Gender:\"male\"}";
  87. testObjectPrivate( json );
  88. json = "{ \"Name\" : \"zhangsan\", \"Gender\":\"male\", \"Address\" : { \"Name\":\"changan\", \"zip\":\"100000\" } }";
  89. testsecondObject( json );
  90. json = "{ \"Name\" : \"zhangsan\", \"Gender\":\"male\", \"Address\" : { \"Name\":{\"cname\":\"长安\", \"ename\":\"changan\"}, \"zip\":\"100000\" } }";
  91. testthirdObject( json );
  92. }
  93. private void testObjectPrivate( string json ) {
  94. Dictionary<string, object> map = JsonParser.Parse( json ) as Dictionary<string, object>;
  95. Assert.AreEqual( 2, map.Count );
  96. foreach (KeyValuePair<string, object> pair in map) {
  97. Console.WriteLine( pair.Key + ":" + pair.Value );
  98. }
  99. Console.WriteLine( "------------------------------------------------" );
  100. }
  101. private void testsecondObject( string json ) {
  102. Dictionary<string, object> map = JsonParser.Parse( json ) as Dictionary<string, object>;
  103. Assert.AreEqual( 3, map.Count );
  104. Assert.IsTrue( map.ContainsKey( "Address" ) );
  105. Dictionary<string, object> obj = map["Address"] as Dictionary<string, object>;
  106. Assert.IsNotNull( obj );
  107. Assert.AreEqual( 2, obj.Count );
  108. foreach (KeyValuePair<string, object> pair in obj) {
  109. Console.WriteLine( pair.Key + ":" + pair.Value );
  110. }
  111. Console.WriteLine( "------------------------------------------------" );
  112. }
  113. private void testthirdObject( string json ) {
  114. Dictionary<string, object> map = JsonParser.Parse( json ) as Dictionary<string, object>;
  115. Assert.AreEqual( 3, map.Count );
  116. Assert.IsTrue( map.ContainsKey( "Address" ) );
  117. Dictionary<string, object> addr = map["Address"] as Dictionary<string, object>;
  118. Assert.IsNotNull( addr );
  119. Assert.AreEqual( 2, addr.Count );
  120. Assert.IsTrue( addr.ContainsKey( "Name" ) );
  121. Dictionary<string, object> name = addr["Name"] as Dictionary<string, object>;
  122. Assert.IsNotNull( name );
  123. Assert.AreEqual( 2, name.Count );
  124. foreach (KeyValuePair<string, object> pair in name) {
  125. Console.WriteLine( pair.Key + ":" + pair.Value );
  126. }
  127. Console.WriteLine( "------------------------------------------------" );
  128. }
  129. [Test]
  130. public void StringToObject() {
  131. string str = "{Id:2, Name:\"诺基亚n78\", Weight:300, Owner:6}";
  132. object obj = JSON.ToObject( str, typeof( MyPhone ) );
  133. Assert.IsNotNull( obj );
  134. MyPhone phone = obj as MyPhone;
  135. Assert.IsNotNull( phone );
  136. Assert.AreEqual( 6, phone.Owner.Id );
  137. }
  138. [Test]
  139. public void JsonStringToList() {
  140. string result = @"
  141. [
  142. { Id:0, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
  143. { Id:1, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
  144. { Id:2, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
  145. { Id:3, Name:""新闻大事690501468"", Weight:0, Owner:""2"" }
  146. ]
  147. ";
  148. List<MyPhone> list = JSON.ToList<MyPhone>( result );
  149. Assert.AreEqual( 4, list.Count );
  150. for (int i = 0; i < list.Count; i++) {
  151. MyPhone phone = list[i] as MyPhone;
  152. Assert.AreEqual( i, phone.Id );
  153. }
  154. }
  155. [Test]
  156. public void StringToHashtable2() {
  157. string str = @"
  158. [
  159. { Id:3, Name:""zhangsan"", Age:25 },
  160. { Id:5, Name:""lisi"", Age:18 }
  161. ]
  162. ";
  163. //object obj = JsonParser.Parse( str );
  164. List<Dictionary<string, object>> lists = JSON.ToDictionaryList( str );
  165. Assert.AreEqual( 2, lists.Count );
  166. Dictionary<string, object> dic = lists[0];
  167. Assert.AreEqual( 3, dic.Keys.Count );
  168. Assert.AreEqual( 3, dic["Id"] );
  169. Assert.AreEqual( "zhangsan", dic["Name"] );
  170. Assert.AreEqual( 25, dic["Age"] );
  171. Dictionary<string, object> dic2 = lists[1];
  172. Assert.AreEqual( 5, dic2["Id"] );
  173. Assert.AreEqual( "lisi", dic2["Name"] );
  174. Assert.AreEqual( 18, dic2["Age"] );
  175. }
  176. [Test]
  177. public void StringToHashtable() {
  178. string str = @"
  179. [
  180. { Id:3, Name:""zhangsan"", Age:25 }
  181. ]
  182. ";
  183. Dictionary<string, object> dic = JSON.ToDictionary( str );
  184. Assert.AreEqual( 3, dic.Keys.Count );
  185. Assert.AreEqual( 3, dic["Id"] );
  186. Assert.AreEqual( "zhangsan", dic["Name"] );
  187. Assert.AreEqual( 25, dic["Age"] );
  188. }
  189. [Test]
  190. public void testDbConfig() {
  191. string str = @"{
  192. ConnectionString : {
  193. default:""Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb"",
  194. db2:""server=localhost;uid=wojilu;pwd=abcd123;database=wojilu;""
  195. },
  196. IsCheckDatabase : true,
  197. MappingTablePrefix :"""",
  198. EnableContextCache : true,
  199. EnableApplicationCache : true,
  200. AssemblyList : [""wojilu.Core"",""wojilu.Apps""],
  201. MetaDLL : """"
  202. }";
  203. Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
  204. Assert.AreEqual( 7, dic.Count );
  205. Assert.AreEqual( dic["IsCheckDatabase"], true );
  206. Assert.AreEqual( dic["MappingTablePrefix"], "" );
  207. Assert.AreEqual( dic["EnableContextCache"], true );
  208. Assert.AreEqual( dic["EnableApplicationCache"], true );
  209. Assert.AreEqual( dic["MetaDLL"], "" );
  210. List<object> list = dic["AssemblyList"] as List<object>;
  211. Assert.IsNotNull( list );
  212. Assert.AreEqual( list.Count, 2 );
  213. Assert.AreEqual( list[0], "wojilu.Core" );
  214. Assert.AreEqual( list[1], "wojilu.Apps" );
  215. Dictionary<string, object> objcn = dic["ConnectionString"] as Dictionary<string, object>;
  216. Assert.IsNotNull( objcn );
  217. Assert.AreEqual( objcn["default"], "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb" );
  218. Assert.AreEqual( objcn["db2"], "server=localhost;uid=wojilu;pwd=abcd123;database=wojilu;" );
  219. }
  220. [Test]
  221. public void testDbConfigExpress() {
  222. string str = @"{
  223. ConnectionStringTable : {
  224. default:""Server = ./sqlexpress;uid=sa;pwd=gl;database=wojilu;Pooling=true;""
  225. },
  226. DbType : {
  227. default:""SqlServer""
  228. },
  229. AssemblyList : [""wojilu.Core"",""wojilu.Apps""]
  230. } ";
  231. Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
  232. List<object> list = dic["AssemblyList"] as List<object>;
  233. Assert.IsNotNull( list );
  234. Assert.AreEqual( list.Count, 2 );
  235. Assert.AreEqual( list[0], "wojilu.Core" );
  236. Assert.AreEqual( list[1], "wojilu.Apps" );
  237. Dictionary<string, object> objcn = dic["ConnectionStringTable"] as Dictionary<string, object>;
  238. Assert.IsNotNull( objcn );
  239. Assert.AreEqual( objcn["default"], "Server = ./sqlexpress;uid=sa;pwd=gl;database=wojilu;Pooling=true;" );
  240. }
  241. [Test]
  242. public void testDbConfigWithPort() {
  243. string str = @"{
  244. ConnectionStringTable : {
  245. default:""server=192.168.15.121:1433;uid=test;pwd=test;database=mydb;""
  246. },
  247. DbType : {
  248. default:""SqlServer""
  249. },
  250. AssemblyList : [""wojilu.Core"",""wojilu.Apps""]
  251. } ";
  252. Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
  253. List<object> list = dic["AssemblyList"] as List<object>;
  254. Assert.IsNotNull( list );
  255. Assert.AreEqual( list.Count, 2 );
  256. Assert.AreEqual( list[0], "wojilu.Core" );
  257. Assert.AreEqual( list[1], "wojilu.Apps" );
  258. Dictionary<string, object> objcn = dic["ConnectionStringTable"] as Dictionary<string, object>;
  259. Assert.IsNotNull( objcn );
  260. Assert.AreEqual( objcn["default"], "server=192.168.15.121:1433;uid=test;pwd=test;database=mydb;" );
  261. }
  262. [Test]
  263. public void testONe() {
  264. string str = "{topic: \"<a href='/bv/Forum1/Topic/Show/4440.aspx'>亚裔美国文学研究的新起点</a>\"}";
  265. Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
  266. Assert.AreEqual( 1, dic.Count );
  267. Assert.AreEqual( "<a href='/bv/Forum1/Topic/Show/4440.aspx'>亚裔美国文学研究的新起点</a>", dic["topic"] );
  268. }
  269. [Test]
  270. public void testMultiline() {
  271. string str = @"
  272. {
  273. name : ""sunzhongshan"",
  274. age : 99,
  275. gender : ""male""
  276. }
  277. ";
  278. Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
  279. Assert.AreEqual( 3, dic.Count );
  280. Assert.AreEqual( dic["name"], "sunzhongshan" );
  281. Assert.AreEqual( dic["age"], 99 );
  282. Assert.AreEqual( dic["gender"], "male" );
  283. }
  284. [Test]
  285. public void testMultiObjects() {
  286. string str = @"
  287. {
  288. name : [""sunwen"", ""袁世凯""],
  289. age : 99,
  290. gender : ""male""
  291. }
  292. ";
  293. Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
  294. Assert.AreEqual( 3, dic.Count );
  295. object obj = dic["name"];
  296. List<object> list = dic["name"] as List<object>;
  297. Assert.IsNotNull( list );
  298. Assert.AreEqual( list.Count, 2 );
  299. Assert.AreEqual( list[0], "sunwen" );
  300. Assert.AreEqual( list[1], "袁世凯" );
  301. Assert.AreEqual( dic["age"], 99 );
  302. Assert.AreEqual( dic["gender"], "male" );
  303. }
  304. [Test]
  305. public void testEmotions() {
  306. String ems = @"{
  307. '$001':'微笑','$002':'大笑','$003':'抛媚眼','$004':'惊讶','$005':'吐舌头、扮鬼脸','$006':'热烈','$007':'生气、黑脸','$008':'困惑','$009':'尴尬','$010':'悲伤',
  308. '$011':'狂笑','$012':'晕、难以理解','$013':'扮酷','$014':'吐','$015':'偷笑','$016':'色、流口水','$017':'hoho','$018':'咬牙切齿','$019':'悄悄话','$020':'撞墙',
  309. '$021':'大哭','$022':'书呆子','$023':'打哈欠','$024':'砸、敲头','$025':'汗','$026':'拍手','$027':'狂怒、砍人','$028':'捂嘴偷笑','$029':'不是、不要','$030':'orz (失意体前屈, 或五体投地)',
  310. '$031':'厉害、牛、强','$032':'差劲、弱','$033':'握手、或英雄所见略同','$034':'竖起中指(fuck you)','$035':'OK、好的','$036':'女孩','$037':'男孩','$038':'左侧拥抱','$039':'右侧拥抱','$040':'骷髅头',
  311. '$041':'爱你、真心','$042':'心碎','$043':'红唇、飞吻','$044':'鲜花、玫瑰','$045':'花儿凋零','$046':'沉睡的月亮、或晚安','$047':'星星','$048':'太阳','$049':'彩虹','$050':'雨伞',
  312. '$051':'咖啡','$052':'蛋糕','$053':'音乐','$054':'电影','$055':'电视、视频','$056':'汽车','$057':'飞机','$058':'照相机','$059':'时钟','$060':'礼物',
  313. '$061':'狗狗','$062':'小猫','$063':'猪头','$064':'蜗牛','$065':'岛屿','$066':'足球','$067':'电话','$068':'灯泡','$069':'臭大粪、shit'
  314. }";
  315. Dictionary<string, object> map = JsonParser.Parse( ems ) as Dictionary<string, object>;
  316. Assert.IsNotNull( map );
  317. Assert.AreEqual( 69, map.Count );
  318. }
  319. private string encode( string str ) {
  320. if (strUtil.IsNullOrEmpty( str )) return str;
  321. return str.Replace( ":", @"\:" ).Replace( "\"", "\\\"" ).Replace( ",", "\\," ).Replace( "'", "\\'" );
  322. }
  323. [Test]
  324. public void testEncode() {
  325. string str = "can't find matching route : Url=_vti_bin/owssvr.dl";
  326. string result = encode( str );
  327. string expected = "can\\'t find matching route \\: Url=_vti_bin/owssvr.dl";
  328. Assert.AreEqual( expected, result );
  329. }
  330. [Test]
  331. public void testReadDbConfig() {
  332. string str = @"{
  333. ConnectionStringTable : {
  334. default:""Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb"",
  335. db2:""server=localhost;uid=wojilusyy;pwd=test123;database=syyWojilu;""
  336. },
  337. IsCheckDatabase : true,
  338. MappingTablePrefix :"""",
  339. EnableContextCache : true,
  340. EnableApplicationCache : true,
  341. AssemblyList : [""wojilu.Core"",""wojilu.Apps""],
  342. MetaDLL : """"
  343. }";
  344. MyDbConfig cf = JSON.ToObject<MyDbConfig>( str );
  345. Assert.IsNotNull( cf );
  346. Assert.AreEqual( 2, cf.ConnectionStringTable.Count );
  347. Assert.AreEqual( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb", cf.ConnectionStringTable["default"] );
  348. Assert.AreEqual( "server=localhost;uid=wojilusyy;pwd=test123;database=syyWojilu;", cf.ConnectionStringTable["db2"] );
  349. Assert.AreEqual( 2, cf.AssemblyList.Count );
  350. Assert.AreEqual( "wojilu.Core", cf.AssemblyList[0] );
  351. Assert.AreEqual( "wojilu.Apps", cf.AssemblyList[1] );
  352. Assert.IsNull( cf.Interceptor );
  353. }
  354. [Test]
  355. public void testQuote() {
  356. string str = "{post: \"<a href='/bv/Forum1/Post/Show/17355.aspx'>re:天下之大,无奇不有</a>\"}";
  357. Dictionary<string, object> dic = JSON.ToDictionary( str );
  358. Assert.AreEqual( 1, dic.Count );
  359. Assert.AreEqual( "<a href='/bv/Forum1/Post/Show/17355.aspx'>re:天下之大,无奇不有</a>", dic["post"] );
  360. }
  361. [Test]
  362. public void testDic() {
  363. //string str = @"{ blog:""<a href='/space/sgzwiz/Blog574/Post/95'>\framework\views\Common\Admin\AppBase\</a>"" }";
  364. string str = @"{ blog:""<a href=\""/space/sgzwiz/Blog574/Post/95\"">\\framework\\views\\Common\\Admin\\AppBase\\</a>"" }";
  365. Dictionary<string, object> dic = JSON.ToDictionary( str );
  366. Assert.AreEqual( 1, dic.Count );
  367. }
  368. [Test]
  369. public void testVarCount() {
  370. string str = "{#actor#} 发表了 {#count#} 篇日志,最近的是 {#blogTitle#}";
  371. int scount = getVarCount( str );
  372. Assert.AreEqual( 3, scount );
  373. }
  374. private static int getVarCount( string str ) {
  375. char[] arrChar = str.ToCharArray();
  376. int scount = 0;
  377. for (int i = 0; i < arrChar.Length; i++) {
  378. if (i == 0) continue;
  379. if (arrChar[i - 1] == '{' && arrChar[i] == '#') scount++;
  380. }
  381. return scount;
  382. }
  383. }
  384. }