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

Language C# Lines 545
MD5 Hash 41e43defb4c57722f318b590bd07842a Estimated Cost $9,809 (why?)
Repository https://bitbucket.org/kingshine/wojilu.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

using NUnit.Framework;
using wojilu.Web.Mvc;
using wojilu;
using wojilu.Serialization;
using wojilu.Members.Users.Domain;

namespace wojilu.Test.Common.Jsons {


    // 本测试用于解析json字符串
    [TestFixture]
    public class JsonTest {












        [Test]
        public void testValue() {

            string json = "376";
            Assert.AreEqual( 376, JsonParser.Parse( json ) );

            json = " false   ";
            Assert.AreEqual( false, JsonParser.Parse( json ) );

            json = " 356   ";
            Assert.AreEqual( 356, JsonParser.Parse( json ) );

            json = " 3.17   ";
            Assert.AreEqual( 3.17, JsonParser.Parse( json ) );

            json = " dfaflddak_dfaol   ";
            Assert.AreEqual( "dfaflddak_dfaol", JsonParser.Parse( json ) );

            json = " dfaflddak=dfaol   ";
            Assert.AreEqual( "dfaflddak=dfaol", JsonParser.Parse( json ) );

            json = " dfaflddak#dfaol   ";
            Assert.AreEqual( "dfaflddak#dfaol", JsonParser.Parse( json ) );

            json = " dfaflddak;dfaol   ";
            Assert.AreEqual( "dfaflddak;dfaol", JsonParser.Parse( json ) );

            json = " dfaflddak\\\"dfaol   ";
            Assert.AreEqual( "dfaflddak\"dfaol", JsonParser.Parse( json ) );
        }

        [Test]
        public void testArray() {

            string json = "  [ \"zhangsan\", 3, false, \"lisi\"]  ";
            testArraySimple( json );

            testArraySimple2();

            json = "  [ \"zhangsan\", 3, false, { Name : \"zhangsan\",  Gender:\"male\"} ]  ";
            testObjectArray( json );
        }

        private void testObjectArray( string json ) {

            List<object> list = JsonParser.Parse( json ) as List<object>;
            Assert.AreEqual( 4, list.Count );
            Assert.AreEqual( "zhangsan", list[0] );
            Assert.AreEqual( 3, list[1] );
            Assert.AreEqual( false, list[2] );

            Dictionary<string, object> map = list[3] as Dictionary<string, object>;
            Assert.IsNotNull( map );

            Assert.AreEqual( 2, map.Count );
            foreach (KeyValuePair<string, object> pair in map) {
                Console.WriteLine( pair.Key + ":" + pair.Value );
            }

        }

        private void testArraySimple( string json ) {
            List<object> list = JsonParser.Parse( json ) as List<object>;
            Assert.AreEqual( 4, list.Count );
            Assert.AreEqual( "zhangsan", list[0] );
            Assert.AreEqual( 3, list[1] );
            Assert.AreEqual( false, list[2] );
            Assert.AreEqual( "lisi", list[3] );
        }

        private void testArraySimple2() {

            string json = " [erieroe  , 38  , lakkk]   ";
            List<object> list = JsonParser.Parse( json ) as List<object>;
            Assert.AreEqual( 3, list.Count );
            Assert.AreEqual( "erieroe", list[0] );
            Assert.AreEqual( 38, list[1] );
            Assert.AreEqual( "lakkk", list[2] );

        }

        [Test]
        public void testArraySimple3() {
            string json = "  [ \"zhang\\\"san\", 3, false, \"lisi\"]  ";

            List<object> list = JsonParser.Parse( json ) as List<object>;
            Assert.AreEqual( 4, list.Count );
            Assert.AreEqual( "zhang\"san", list[0] );
            Assert.AreEqual( 3, list[1] );
            Assert.AreEqual( false, list[2] );
            Assert.AreEqual( "lisi", list[3] );
        }


        [Test]
        public void testObject() {

            string json = "{ \"Name\" : \"zhangsan\",  \"Gender\":\"male\"}";
            testObjectPrivate( json );

            json = "{ Name : \"zhangsan\",  Gender:\"male\"}";
            testObjectPrivate( json );

            json = "{ \"Name\" : \"zhangsan\",  \"Gender\":\"male\", \"Address\" : { \"Name\":\"changan\", \"zip\":\"100000\"  } }";
            testsecondObject( json );

            json = "{ \"Name\" : \"zhangsan\",  \"Gender\":\"male\", \"Address\" : { \"Name\":{\"cname\":\"长安\", \"ename\":\"changan\"}, \"zip\":\"100000\"  } }";
            testthirdObject( json );
        }

        private void testObjectPrivate( string json ) {

            Dictionary<string, object> map = JsonParser.Parse( json ) as Dictionary<string, object>;

            Assert.AreEqual( 2, map.Count );

            foreach (KeyValuePair<string, object> pair in map) {
                Console.WriteLine( pair.Key + ":" + pair.Value );
            }
            Console.WriteLine( "------------------------------------------------" );
        }

        private void testsecondObject( string json ) {

            Dictionary<string, object> map = JsonParser.Parse( json ) as Dictionary<string, object>;

            Assert.AreEqual( 3, map.Count );

            Assert.IsTrue( map.ContainsKey( "Address" ) );
            Dictionary<string, object> obj = map["Address"] as Dictionary<string, object>;
            Assert.IsNotNull( obj );

            Assert.AreEqual( 2, obj.Count );

            foreach (KeyValuePair<string, object> pair in obj) {
                Console.WriteLine( pair.Key + ":" + pair.Value );
            }
            Console.WriteLine( "------------------------------------------------" );

        }

        private void testthirdObject( string json ) {

            Dictionary<string, object> map = JsonParser.Parse( json ) as Dictionary<string, object>;

            Assert.AreEqual( 3, map.Count );

            Assert.IsTrue( map.ContainsKey( "Address" ) );
            Dictionary<string, object> addr = map["Address"] as Dictionary<string, object>;
            Assert.IsNotNull( addr );

            Assert.AreEqual( 2, addr.Count );

            Assert.IsTrue( addr.ContainsKey( "Name" ) );
            Dictionary<string, object> name = addr["Name"] as Dictionary<string, object>;
            Assert.IsNotNull( name );

            Assert.AreEqual( 2, name.Count );


            foreach (KeyValuePair<string, object> pair in name) {
                Console.WriteLine( pair.Key + ":" + pair.Value );
            }
            Console.WriteLine( "------------------------------------------------" );
        }

        [Test]
        public void StringToObject() {

            string str = "{Id:2,  Name:\"诺基亚n78\", Weight:300, Owner:6}";

            object obj = JSON.ToObject( str, typeof( MyPhone ) );
            Assert.IsNotNull( obj );
            MyPhone phone = obj as MyPhone;
            Assert.IsNotNull( phone );
            Assert.AreEqual( 6, phone.Owner.Id );

        }


        [Test]
        public void JsonStringToList() {
            string result = @"
[
	{ Id:0, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
	{ Id:1, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
	{ Id:2, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
	{ Id:3, Name:""新闻大事690501468"", Weight:0, Owner:""2"" }
]
";
            List<MyPhone> list = JSON.ToList<MyPhone>( result );
            Assert.AreEqual( 4, list.Count );

            for (int i = 0; i < list.Count; i++) {
                MyPhone phone = list[i] as MyPhone;
                Assert.AreEqual( i, phone.Id );
            }

        }

        [Test]
        public void StringToHashtable2() {
            string str = @"
[
{ Id:3, Name:""zhangsan"", Age:25 },
{ Id:5, Name:""lisi"", Age:18 }
]
";

            //object obj = JsonParser.Parse( str );

            List<Dictionary<string, object>> lists = JSON.ToDictionaryList( str );
            Assert.AreEqual( 2, lists.Count );

            Dictionary<string, object> dic = lists[0];
            Assert.AreEqual( 3, dic.Keys.Count );
            Assert.AreEqual( 3, dic["Id"] );
            Assert.AreEqual( "zhangsan", dic["Name"] );
            Assert.AreEqual( 25, dic["Age"] );

            Dictionary<string, object> dic2 = lists[1];
            Assert.AreEqual( 5, dic2["Id"] );
            Assert.AreEqual( "lisi", dic2["Name"] );
            Assert.AreEqual( 18, dic2["Age"] );


        }

        [Test]
        public void StringToHashtable() {
            string str = @"

[

       { Id:3, Name:""zhangsan"", Age:25 }  
]
";

            Dictionary<string, object> dic = JSON.ToDictionary( str );

            Assert.AreEqual( 3, dic.Keys.Count );
            Assert.AreEqual( 3, dic["Id"] );
            Assert.AreEqual( "zhangsan", dic["Name"] );
            Assert.AreEqual( 25, dic["Age"] );

        }



        [Test]
        public void testDbConfig() {
            string str = @"{

    ConnectionString : {
        default:""Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb"",
        db2:""server=localhost;uid=wojilu;pwd=abcd123;database=wojilu;""
    },
    
    IsCheckDatabase : true,
    MappingTablePrefix :"""",
    EnableContextCache : true,
    EnableApplicationCache : true,
    AssemblyList : [""wojilu.Core"",""wojilu.Apps""],
    MetaDLL : """"
    
}";
            Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
            Assert.AreEqual( 7, dic.Count );

            Assert.AreEqual( dic["IsCheckDatabase"], true );
            Assert.AreEqual( dic["MappingTablePrefix"], "" );
            Assert.AreEqual( dic["EnableContextCache"], true );
            Assert.AreEqual( dic["EnableApplicationCache"], true );
            Assert.AreEqual( dic["MetaDLL"], "" );

            List<object> list = dic["AssemblyList"] as List<object>;
            Assert.IsNotNull( list );
            Assert.AreEqual( list.Count, 2 );
            Assert.AreEqual( list[0], "wojilu.Core" );
            Assert.AreEqual( list[1], "wojilu.Apps" );

            Dictionary<string, object> objcn = dic["ConnectionString"] as Dictionary<string, object>;
            Assert.IsNotNull( objcn );
            Assert.AreEqual( objcn["default"], "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb" );
            Assert.AreEqual( objcn["db2"], "server=localhost;uid=wojilu;pwd=abcd123;database=wojilu;" );
        }

        [Test]
        public void testDbConfigExpress() {
            string str = @"{ 
ConnectionStringTable : {
default:""Server = ./sqlexpress;uid=sa;pwd=gl;database=wojilu;Pooling=true;""
}, 

DbType : { 
default:""SqlServer"" 
}, 

AssemblyList : [""wojilu.Core"",""wojilu.Apps""] 
} ";
            Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;

            List<object> list = dic["AssemblyList"] as List<object>;
            Assert.IsNotNull( list );
            Assert.AreEqual( list.Count, 2 );
            Assert.AreEqual( list[0], "wojilu.Core" );
            Assert.AreEqual( list[1], "wojilu.Apps" );

            Dictionary<string, object> objcn = dic["ConnectionStringTable"] as Dictionary<string, object>;
            Assert.IsNotNull( objcn );
            Assert.AreEqual( objcn["default"], "Server = ./sqlexpress;uid=sa;pwd=gl;database=wojilu;Pooling=true;" );
        }

        [Test]
        public void testDbConfigWithPort() {
            string str = @"{ 
ConnectionStringTable : {
default:""server=192.168.15.121:1433;uid=test;pwd=test;database=mydb;""
}, 

DbType : { 
default:""SqlServer"" 
}, 

AssemblyList : [""wojilu.Core"",""wojilu.Apps""] 
} ";
            Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;

            List<object> list = dic["AssemblyList"] as List<object>;
            Assert.IsNotNull( list );
            Assert.AreEqual( list.Count, 2 );
            Assert.AreEqual( list[0], "wojilu.Core" );
            Assert.AreEqual( list[1], "wojilu.Apps" );

            Dictionary<string, object> objcn = dic["ConnectionStringTable"] as Dictionary<string, object>;
            Assert.IsNotNull( objcn );
            Assert.AreEqual( objcn["default"], "server=192.168.15.121:1433;uid=test;pwd=test;database=mydb;" );
        }


        [Test]
        public void testONe() {

            string str = "{topic: \"<a href='/bv/Forum1/Topic/Show/4440.aspx'>亚裔美国文学研究的新起点</a>\"}";

            Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
            Assert.AreEqual( 1, dic.Count );

            Assert.AreEqual( "<a href='/bv/Forum1/Topic/Show/4440.aspx'>亚裔美国文学研究的新起点</a>", dic["topic"] );
        }

        [Test]
        public void testMultiline() {

            string str = @"
{
    
    name : ""sunzhongshan"",
    age : 99,
    gender : ""male""

}
";
            Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
            Assert.AreEqual( 3, dic.Count );
            Assert.AreEqual( dic["name"], "sunzhongshan" );
            Assert.AreEqual( dic["age"], 99 );
            Assert.AreEqual( dic["gender"], "male" );
        }

        [Test]
        public void testMultiObjects() {
            string str = @"
{
    
    name : [""sunwen"",  ""袁世凯""],
    age : 99,
    gender : ""male""

}
";
            Dictionary<string, object> dic = JsonParser.Parse( str ) as Dictionary<string, object>;
            Assert.AreEqual( 3, dic.Count );

            object obj = dic["name"];

            List<object> list = dic["name"] as List<object>;
            Assert.IsNotNull( list );
            Assert.AreEqual( list.Count, 2 );
            Assert.AreEqual( list[0], "sunwen" );
            Assert.AreEqual( list[1], "袁世凯" );

            Assert.AreEqual( dic["age"], 99 );
            Assert.AreEqual( dic["gender"], "male" );
        }





        [Test]
        public void testEmotions() {
            String ems = @"{
    	'$001':'微笑','$002':'大笑','$003':'抛媚眼','$004':'惊讶','$005':'吐舌头、扮鬼脸','$006':'热烈','$007':'生气、黑脸','$008':'困惑','$009':'尴尬','$010':'悲伤',
    	'$011':'狂笑','$012':'晕、难以理解','$013':'扮酷','$014':'','$015':'偷笑','$016':'色、流口水','$017':'hoho','$018':'咬牙切齿','$019':'悄悄话','$020':'撞墙',
    	'$021':'大哭','$022':'书呆子','$023':'打哈欠','$024':'砸、敲头','$025':'','$026':'拍手','$027':'狂怒、砍人','$028':'捂嘴偷笑','$029':'不是、不要','$030':'orz (失意体前屈, 或五体投地)',
    	'$031':'厉害、牛、强','$032':'差劲、弱','$033':'握手、或英雄所见略同','$034':'竖起中指(fuck you)','$035':'OK、好的','$036':'女孩','$037':'男孩','$038':'左侧拥抱','$039':'右侧拥抱','$040':'骷髅头',
    	'$041':'爱你、真心','$042':'心碎','$043':'红唇、飞吻','$044':'鲜花、玫瑰','$045':'花儿凋零','$046':'沉睡的月亮、或晚安','$047':'星星','$048':'太阳','$049':'彩虹','$050':'雨伞',
    	'$051':'咖啡','$052':'蛋糕','$053':'音乐','$054':'电影','$055':'电视、视频','$056':'汽车','$057':'飞机','$058':'照相机','$059':'时钟','$060':'礼物',
    	'$061':'狗狗','$062':'小猫','$063':'猪头','$064':'蜗牛','$065':'岛屿','$066':'足球','$067':'电话','$068':'灯泡','$069':'臭大粪、shit'
    }";

            Dictionary<string, object> map = JsonParser.Parse( ems ) as Dictionary<string, object>;
            Assert.IsNotNull( map );
            Assert.AreEqual( 69, map.Count );



        }


        private string encode( string str ) {
            if (strUtil.IsNullOrEmpty( str )) return str;
            return str.Replace( ":", @"\:" ).Replace( "\"", "\\\"" ).Replace( ",", "\\," ).Replace( "'", "\\'" );
        }

        [Test]
        public void testEncode() {

            string str = "can't find matching route : Url=_vti_bin/owssvr.dl";

            string result = encode( str );
            string expected = "can\\'t find matching route \\: Url=_vti_bin/owssvr.dl";
            Assert.AreEqual( expected, result );
        }

        [Test]
        public void testReadDbConfig() {

            string str = @"{

    ConnectionStringTable : {
        default:""Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb"",
        db2:""server=localhost;uid=wojilusyy;pwd=test123;database=syyWojilu;""
    },
    
    IsCheckDatabase : true,
    MappingTablePrefix :"""",
    EnableContextCache : true,
    EnableApplicationCache : true,
    AssemblyList : [""wojilu.Core"",""wojilu.Apps""],
    MetaDLL : """"
    
}";

            MyDbConfig cf = JSON.ToObject<MyDbConfig>( str );

            Assert.IsNotNull( cf );

            Assert.AreEqual( 2, cf.ConnectionStringTable.Count );
            Assert.AreEqual( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wojilu.mdb", cf.ConnectionStringTable["default"] );
            Assert.AreEqual( "server=localhost;uid=wojilusyy;pwd=test123;database=syyWojilu;", cf.ConnectionStringTable["db2"] );

            Assert.AreEqual( 2, cf.AssemblyList.Count );
            Assert.AreEqual( "wojilu.Core", cf.AssemblyList[0] );
            Assert.AreEqual( "wojilu.Apps", cf.AssemblyList[1] );

            Assert.IsNull( cf.Interceptor );
        }


        [Test]
        public void testQuote() {
            string str = "{post: \"<a href='/bv/Forum1/Post/Show/17355.aspx'>re:天下之大,无奇不有</a>\"}";
            Dictionary<string, object> dic = JSON.ToDictionary( str );
            Assert.AreEqual( 1, dic.Count );
            Assert.AreEqual( "<a href='/bv/Forum1/Post/Show/17355.aspx'>re:天下之大,无奇不有</a>", dic["post"] );
        }

        [Test]
        public void testDic() {

            //string str = @"{ blog:""<a href='/space/sgzwiz/Blog574/Post/95'>\framework\views\Common\Admin\AppBase\</a>"" }";
            string str = @"{ blog:""<a href=\""/space/sgzwiz/Blog574/Post/95\"">\\framework\\views\\Common\\Admin\\AppBase\\</a>"" }";
            Dictionary<string, object> dic = JSON.ToDictionary( str );
            Assert.AreEqual( 1, dic.Count );

        }

        [Test]
        public void testVarCount() {

            string str = "{#actor#} 发表了 {#count#} 篇日志,最近的是 {#blogTitle#}";

            int scount = getVarCount( str );
            Assert.AreEqual( 3, scount );

        }

        private static int getVarCount( string str ) {
            char[] arrChar = str.ToCharArray();
            int scount = 0;

            for (int i = 0; i < arrChar.Length; i++) {
                if (i == 0) continue;
                if (arrChar[i - 1] == '{' && arrChar[i] == '#') scount++;
            }
            return scount;
        }

    }



}
Back to Top