forked from calvinwilliams/DirectStruct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME-CN
2407 lines (2139 loc) · 88.2 KB
/
README-CN
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
DirectStruct - C结构体工具
1.概述
2.编译安装
3.基本使用
3.1.编写IDL定义文件,用自动化代码工具dsc处理之
3.2.编写应用代码,调用网络/主机字节序化函数
3.3.编写应用代码,调用二进制序列化/反序列化函数
3.4.编写应用代码,调用XML序列化/反序列化函数
3.5.编写应用代码,调用JSON序列化/反序列化函数
3.6.编写应用代码,使用ESQL代码开发数据库应用
N-2.DirectStruct报文定义文件语法
N-1.性能压测
N.最后
------------------------------------------------------------
1.概述
DirectStruct是一个强有力的代码自动生成工具,它可以自动生成C结构体与XML、JSON、二进制(带压缩)等报文之间的转换代码,还能自动生成ESQL代码片段,在应用中直接使用自动化代码以提高开发效率,避免开发冗余和手工编码风险。
DirectStruct生成了哪些自动化代码?
* C结构体定义
* 批量初始化缺省值函数
* 网络/主机字节序化函数
* 二进制序列化/反序列化函数(可带压缩)
* XML序列化/反序列化函数(展开风格和紧缩风格)(依赖开源库fasterxml[url]http://git.oschina.net/calvinwilliams/fasterxml[/url])
* JSON序列化/反序列化函数(展开风格和紧缩风格)(依赖开源库fasterjson[url]http://git.oschina.net/calvinwilliams/fasterjson[/url])
* 嵌入式SQL代码片段以及一些嵌入式SQL函数
* 打印结构体内所有字段值函数
用DirectStruct自动生成的代码能干什么?
* 直接C结构体通讯交换 : 在特定平台之间直接把C结构体变量通过TCP扔给对方是一个很诱人的想法,阻碍我们的主要是整型字段的字节序问题,手工编码是一件冗余的事情,DirectStruct可以帮助你自动化这一过程。解决了字节序问题后就可以直接交换报文,避免了通讯发送前的打包和通讯接收后的解包,所以速度非常快,是某些追求极端性能的场景中的首选报文格式。
* 结构体序列化/反序列化 : 使用DirectStruct自动生成的序列化/反序列化函数,可以将C结构体打包成二进制报文或XML报文或JSON报文等,以及反向解包,是的,打包解包函数自动生成,省却了你不少麻烦吧。二进制报文如果启用了压缩选项,压缩后的二进制数据块大幅缩小,更便于通讯高效交换。
* 基于嵌入式SQL的数据库映射接口的代码自动化生成
* 快速打印结构体内所有字段值 : 对于一个大结构,编写他的打印所有字段值的函数是一件很无聊的事情,你可以用DirectStruct帮助你自动生成日志函数,输出到屏幕,或你自己的日志流中。
* ( 新主意增加中 ... 把你的需求告诉我吧 )
为什么使用DirectStruct?
* 灵活的报文定义语法 : 命令行工具dsc读入定义文件,生成自动化代码,支持子结构嵌套、子结构数组、定义文件包含等灵活配置方式。
* 跨平台:DirectStruct支持WINDOWS、Linux、AIX等主流操作系统,尤其是多平台中不同的表达方法,如64位整型,相同的IDL定义文件将生成不同平台的标准。
2.编译安装
for Linux
$ tar xvzf dsc-1.1.0.tar.gz
$ cd dsc-1.1.0/src
$ su
# make -f makefile.Linux clean
rm -f util.o
rm -f main.o
rm -f dsc.o
rm -f ReadDscFile.o
rm -f GenerateCCode.o
rm -f dsc
# make -f makefile.Linux install
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c util.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c main.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c dsc.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c ReadDscFile.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c GenerateCCode.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o dsc util.o main.o dsc.o ReadDscFile.o GenerateCCode.o -L.
cp -f dsc /usr/bin/
3.基本使用
本章示例都取自于test目录中的测试程序。
3.1.编写IDL定义文件,用自动化代码工具dsc处理之
[code]
$ cat IDL_AllTypes.dsc
STRUCT AllTypes
{
# 这是注释
INT 1 n1 # 这也是注释
INT 2 n2
INT 4 n4
INT 8 n8
UINT 1 u1
UINT 2 u2
UINT 4 u4
UINT 8 u8
FLOAT 4 f4
FLOAT 8 f8
CHAR 1 ch
UCHAR 1 uch
STRING 32 str32
STRING 1024 str1024
}
$ dsc -f IDL_AllTypes.dsc -c-ALL
STRUCT AllTypes
INT 1 n1
INT 2 n2
INT 4 n4
INT 8 n8 # longlong or _int64 for c
UINT 1 u1
UINT 2 u2
UINT 4 u4
UINT 8 u8
FLOAT 4 f4
FLOAT 8 f8
CHAR 1 ch
UCHAR 1 uch
STRING 32 str32
STRING 1024 str1024
ok!
$ ls
IDL_AllTypes.dsc
IDL_AllTypes.dsc.h
IDL_AllTypes.dsc.c
IDL_AllTypes.dsc.LOG.c
[/code]
IDL_test.dsc.h包含了自动生成的一个C结构体定义。
IDL_test.dsc.c包含了自动生成的初始化缺省函数、网络/主机字节序化、序列化/反序列化等代码。
IDL_test.dsc.LOG.c包含了自动生成的一个日志函数用于快速打印报文所有字段到标准输出或你自己的日志流上便于调试。
注意:IDL_test.dsc.h、IDL_test.dsc.c实际内容取决于参数"-c-*"
[code]
$ dsc
dsc v1.1.0 - DirectSrtuct Compiler
Copyright by calvin<calvinwilliams.c@gmail.com> 2014
USAGE : dsc -f .dsc [ -c ] [ -c-order ] [ -c-compact ] [ -c-compress ] [ -c-xml ] [ -c-LOG ] [ -c-ALL ]
[/code]
-c 基本的c结构体定义
-c-order 还包含主机/网络字节序化函数
-c-compact 还包含二进制紧凑序列化/反序列化函数
-c-compress 还包含二进制压缩序列化/反序列化函数
-c-xml 还包含XML序列化/反序列化函数(依赖于开源库fastxml)
-c-LOG 生成IDL_test.dsc.LOG.c
-c-ALL 生成以上所有
注意:test/makefile未包含处理.dsc定义文件的动作,如修改.dsc文件后需要手工处理之。
3.2.编写应用代码,调用网络/主机字节序化函数
[code]
$ cat test_netorder.c
int test_netorder()
{
AllTypes st ;
int nret = 0 ;
/* client code */
memset( & st , 0x00 , sizeof(AllTypes) );
st.n1 = -0x11 ;
st.n2 = -0x1122 ;
st.n4 = -0x11223344 ;
st.n8 = LL(-0x1122334455667788) ;
st.u1 = 0x11 ;
st.u2 = 0x1122 ;
st.u4 = 0x11223344 ;
st.u8 = ULL(0x1122334455667788) ;
st.f4 = 100.00 ;
st.f8 = 10000.00 ;
st.ch = 'A' ;
st.uch = 'B' ;
strcpy( st.str32 , "calvin" );
strcpy( st.str1024 , "XXXXXXXXXXXXXXXX" );
DSCLOG_AllTypes( & st );
nret = DSCNETORDER_AllTypes( & st ) ;
if( nret )
{
printf( "DSCNETORDER_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCNETORDER_AllTypes ok\n" );
}
/* ... client send communication struct to server ... */
/* server code */
nret = DSCHOSTORDER_AllTypes( & st ) ;
if( nret )
{
printf( "DSCHOSTORDER_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCHOSTORDER_AllTypes ok\n" );
}
DSCLOG_AllTypes( & st );
return 0;
}
$ make -f makefile.Linux
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_netorder.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c IDL_AllTypes.dsc.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_netorder test_netorder.o IDL_AllTypes.dsc.o -L.
$ ./test_netorder
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
DSCNETORDER_AllTypes ok
0x0000000000 ef 00 ee de ee dd cc bc ee dd cc bb aa 99 88 78 ...............x
0x0000000001 11 00 11 22 11 22 33 44 11 22 33 44 55 66 77 88 ..."."3D."3DUfw.
0x0000000002 00 00 c8 42 00 00 00 00 00 88 c3 40 41 42 63 61 ...B.......@ABca
0x0000000003 6c 76 69 6e 00 00 00 00 00 00 00 00 00 00 00 00 lvin............
0x0000000004 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 58 ...............X
0x0000000005 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 00 XXXXXXXXXXXXXXX.
...
DSCHOSTORDER_AllTypes ok
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
[/code]
3.3.编写应用代码,调用二进制序列化/反序列化函数
[code]
$ cat test_serialize_compact.c
int test_serialize_compact()
{
AllTypes st ;
char buf[ DSCSERIALIZE_COMPACT_BUFSIZE_AllTypes + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
memset( & st , 0x00 , sizeof(AllTypes) );
st.n1 = -0x11 ;
st.n2 = -0x1122 ;
st.n4 = -0x11223344 ;
st.n8 = LL(-0x1122334455667788) ;
st.u1 = 0x11 ;
st.u2 = 0x1122 ;
st.u4 = 0x11223344 ;
st.u8 = ULL(0x1122334455667788) ;
st.f4 = 100.00 ;
st.f8 = 10000.00 ;
st.ch = 'A' ;
st.uch = 'B' ;
strcpy( st.str32 , "calvin" );
strcpy( st.str1024 , "XXXXXXXXXXXXXXXX" );
memset( buf , 0x00 , sizeof(buf) );
nret = DSCSERIALIZE_COMPACT_AllTypes( & st , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_COMPACT_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_COMPACT_AllTypes ok , len[%d]\n" , len );
}
dump( buf , len );
/* ... client send communication struct to server ... */
/* server code */
memset( & st , 0x00 , sizeof(AllTypes) );
nret = DSCDESERIALIZE_COMPACT_AllTypes( buf , & len , & st ) ;
if( nret )
{
printf( "DSCDESERIALIZE_COMPACT_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_COMPACT_AllTypes ok\n" );
}
DSCLOG_AllTypes( & st );
return 0;
}
$ make -f makefile.Linux
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_netorder.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_serialize_compact.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_compact test_serialize_compact.o IDL_AllTypes.dsc.o -L.
$ ./test_serialize_compact
DSCSERIALIZE_COMPACT_AllTypes ok , len[1100]
0x0000000000 ef ee de ee dd cc bc ee dd cc bb aa 99 88 78 11 ..............x.
0x0000000001 11 22 11 22 33 44 11 22 33 44 55 66 77 88 00 00 ."."3D."3DUfw...
0x0000000002 c8 42 00 00 00 00 00 88 c3 40 41 42 63 61 6c 76 .B.......@ABcalv
0x0000000003 69 6e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 in..............
0x0000000004 00 00 00 00 00 00 00 00 00 00 00 00 58 58 58 58 ............XXXX
0x0000000005 58 58 58 58 58 58 58 58 58 58 58 58 00 00 00 00 XXXXXXXXXXXX....
...
DSCDESERIALIZE_COMPACT_AllTypes ok
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
[/code]
紧凑(COMPACT)序列化的内部原理是把所有字段值在内存中的映像平铺在一个大数据块中,首尾相接,无填充空间,且整型值做字节序处理。如
STRUCT struct_name
{
INT 4 n4
STRING 256 str256
}
序列化后的布局为
n4(4字节)+str256(256字节)
总共260字节
使用带压缩的序列化函数可以使得最终二进制数据块更小
[code]
$ cat test_serialize_compress.c
...
int test_serialize_compress()
{
AllTypes st ;
char buf[ DSCSERIALIZE_COMPRESS_BUFSIZE_AllTypes + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
memset( & st , 0x00 , sizeof(AllTypes) );
st.n1 = -0x11 ;
st.n2 = -0x1122 ;
st.n4 = -0x11223344 ;
st.n8 = LL(-0x1122334455667788) ;
st.u1 = 0x11 ;
st.u2 = 0x1122 ;
st.u4 = 0x11223344 ;
st.u8 = ULL(0x1122334455667788) ;
st.f4 = 100.00 ;
st.f8 = 10000.00 ;
st.ch = 'A' ;
st.uch = 'B' ;
strcpy( st.str32 , "calvin" );
strcpy( st.str1024 , "XXXXXXXXXXXXXXXX" );
DSCLOG_AllTypes( & st );
memset( buf , 0x00 , sizeof(buf) );
nret = DSCSERIALIZE_COMPRESS_AllTypes( & st , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_COMPRESS_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_COMPRESS_AllTypes ok , len[%d]\n" , len );
}
dump( buf , len );
/* ... client send communication struct to server ... */
/* server code */
memset( & st , 0x00 , sizeof(AllTypes) );
nret = DSCDESERIALIZE_COMPRESS_AllTypes( buf , & len , & st ) ;
if( nret )
{
printf( "DSCDESERIALIZE_COMPRESS_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_COMPRESS_AllTypes ok\n" );
}
DSCLOG_AllTypes( & st );
return 0;
}
...
$ make -f makefile.Linux
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_netorder.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_serialize_compress.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_compress test_serialize_compress.o IDL_AllTypes.dsc.o -L.
$ ./test_serialize_compress
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
DSCSERIALIZE_COMPRESS_AllTypes ok , len[74]
0x0000000000 ef ee de 14 11 22 33 44 18 11 22 33 44 55 66 77 ....."3D.."3DUfw
0x0000000001 88 11 11 22 04 11 22 33 44 08 11 22 33 44 55 66 ...".."3D.."3DUf
0x0000000002 77 88 00 00 c8 42 00 00 00 00 00 88 c3 40 41 42 w....B.......@AB
0x0000000003 01 06 63 61 6c 76 69 6e 01 10 58 58 58 58 58 58 ..calvin..XXXXXX
0x0000000004 58 58 58 58 58 58 58 58 58 58 XXXXXXXXXX
DSCDESERIALIZE_COMPRESS_AllTypes ok
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
[/code]
这个示例中不压缩的二进制数据块有1100字节,压缩后只有74字节,适用于网络数据交换。
压缩(COMPRESS)序列化的内部原理是在紧凑序列化的基础上对某些类型的字段做压缩转换处理。
INT 4类型放置前置头字节,左四位为选项,目前只用到了正负数标志(正数为0,负数为1),右四位为实际存储长度,如某值的网络字节序为0x00001234,实际存储为0x1234,加上前置头,总共用3字节表达
0x02 0x12 0x34
值-0x1234也是用3字节表达
0x12 0x12 0x34
INT 8类同
UINT类型INT
STRING n类型放置前置UINT 4字段记录长度。如"abc",实际长度为3字节,前置UINT 4字段0x01 0x03,总共用5字节表达
0x01 0x03 0x61 0x62 0x63
其它字段类型不压缩转换
如
STRUCT struct_name
{
INT 4 n4
STRING 256 str256
}
当
n4=0x3456
str256="abc"
压缩序列化后的数据为
0x02 0x34 0x56 0x01 0x05 0x61 0x62 0x63
总共8字节,看,是不是比紧凑序列化要短的多呢,这在通讯数据交换中可以节省更多的流量。
3.4.编写应用代码,调用XML序列化/反序列化函数
XML序列化/反序列化函数依赖于开源库fasterxml。
[code]
$ cat test_serialize_xml.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "IDL_BankXmlTransaction.dsc.h"
#include "IDL_BankXmlTransaction.dsc.LOG.c"
int test_serialize_xml()
{
BankXmlTransaction trans ;
char buf[ 40960 + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
DSCINIT_BankXmlTransaction( & trans );
strcpy( trans.ResponseHeader.transaction_code , "P0101" );
trans.ResponseHeader.trans_jnlsno = 12345678 ;
trans.ResponseHeader.response_code = 0 ;
strcpy( trans.ResponseHeader.response_desc , "OK" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[1].message_text , "Check channel passed" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[0].message_text , "Check account passed" );
strcpy( trans.QueryTransactionDetails.TransactionDetailTitle.title_text , "DETAIL TITLE" );
trans.QueryTransactionDetails.TransactionDetailTitle.page_no = 1 ;
trans.QueryTransactionDetails.TransactionDetailTitle.page_size = 2 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_date , "2014-01-01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_time , "08:01:01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].outlet_id , "1001" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_amount = 100.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_date , "2014-02-02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_time , "08:02:02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].outlet_id , "2002" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_amount = 200.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[2].card_no , "<" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[3].card_no , "&" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[4].card_no , ">" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[5].card_no , "<&>" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[6].card_no , "<你&他>" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[7].card_no , "我<你&他>她" );
DSCLOG_BankXmlTransaction( & trans );
memset( buf , 0x00 , sizeof(buf) );
len = sizeof(buf)-1 ;
nret = DSCSERIALIZE_XML_BankXmlTransaction( & trans , "GBK" , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_XML_BankXmlTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_XML_BankXmlTransaction ok , len[%d]\n" , len );
}
printf( "[%s]\n" , buf );
/* ... client send communication struct to server ... */
/* server code */
memset( & trans , 0x00 , sizeof(BankXmlTransaction) );
nret = DSCDESERIALIZE_XML_BankXmlTransaction( NULL , buf , & len , & trans ) ;
if( nret )
{
printf( "DSCDESERIALIZE_XML_BankXmlTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_XML_BankXmlTransaction ok\n" );
}
DSCLOG_BankXmlTransaction( & trans );
return 0;
}
int main()
{
return -test_serialize_xml();
}
$ make
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_serialize_xml.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I/home/calvin/exinc/fastxml -I. -c IDL_BankXmlTransaction.dsc.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_xml test_serialize_xml.o IDL_BankXmlTransaction.dsc.o -L. -L/home/calvin/exlib -lfastxl
$ ./test_serialize_xml
BankXmlTransaction.version[1]
BankXmlTransaction.ResponseHeader.transaction_code[P0101]
BankXmlTransaction.ResponseHeader.trans_jnlsno[12345678]
BankXmlTransaction.ResponseHeader.response_code[0]
BankXmlTransaction.ResponseHeader.response_desc[OK]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check account passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check channel passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.title_text[DETAIL TITLE]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_no[1]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_size[2]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-01-01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:01:01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[1001]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[100.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-02-02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:02:02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[2002]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[200.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[&]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<&>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<你&他>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[我<你&他>她]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
DSCSERIALIZE_XML_BankXmlTransaction ok , len[2734]
[<?xml version="1.0" encoding="GBK"?>
<BankXmlTransaction>
<version>1</version>
<ResponseHeader>
<transaction_code>P0101</transaction_code>
<trans_jnlsno>12345678</trans_jnlsno>
<response_code>0</response_code>
<response_desc>OK</response_desc>
</ResponseHeader>
<QueryTransactionDetails>
<AddonMessages>
<AddonMessage>
<message_text>Check account passed</message_text>
</AddonMessage>
<AddonMessage>
<message_text>Check channel passed</message_text>
</AddonMessage>
<AddonMessage>
<message_text></message_text>
</AddonMessage>
</AddonMessages>
<TransactionDetailTitle>
<title_text>DETAIL TITLE</title_text>
<page_no>1</page_no>
<page_size>2</page_size>
</TransactionDetailTitle>
<TransactionDetails>
<TransactionDetail>
<trans_date>2014-01-01</trans_date>
<trans_time>08:01:01</trans_time>
<outlet_id>1001</outlet_id>
<card_no>603367123412341234</card_no>
<trans_amount>100.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date>2014-02-02</trans_date>
<trans_time>08:02:02</trans_time>
<outlet_id>2002</outlet_id>
<card_no>603367123412341234</card_no>
<trans_amount>200.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no><</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>&</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>></card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no><&></card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no><你&他></card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>我<你&他>她</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no></card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no></card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
</TransactionDetails>
</QueryTransactionDetails>
</BankXmlTransaction>
]
DSCDESERIALIZE_XML_BankXmlTransaction ok
BankXmlTransaction.version[1]
BankXmlTransaction.ResponseHeader.transaction_code[P0101]
BankXmlTransaction.ResponseHeader.trans_jnlsno[12345678]
BankXmlTransaction.ResponseHeader.response_code[0]
BankXmlTransaction.ResponseHeader.response_desc[OK]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check account passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check channel passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.title_text[DETAIL TITLE]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_no[1]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_size[2]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-01-01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:01:01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[1001]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[100.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-02-02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:02:02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[2002]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[200.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[&]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<&>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<你&他>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[我<你&他>她]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
[/code]
3.5.编写应用代码,调用JSON序列化/反序列化函数
JSON序列化/反序列化函数依赖于开源库fasterjson。
[code]
$ cat test_serialize_json.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "IDL_BankJsonTransaction.dsc.h"
#include "IDL_BankJsonTransaction.dsc.LOG.c"
int test_serialize_json()
{
BankJsonTransaction trans ;
char buf[ 40960 + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
DSCINIT_BankJsonTransaction( & trans );
strcpy( trans.ResponseHeader.transaction_code , "P0101" );
trans.ResponseHeader.trans_jnlsno = 12345678 ;
trans.ResponseHeader.response_code = 0 ;
strcpy( trans.ResponseHeader.response_desc , "OK" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[1].message_text , "Check channel passed" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[0].message_text , "Check account passed" );
strcpy( trans.QueryTransactionDetails.TransactionDetailTitle.title_text , "DETAIL TITLE" );
trans.QueryTransactionDetails.TransactionDetailTitle.page_no = 1 ;
trans.QueryTransactionDetails.TransactionDetailTitle.page_size = 2 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_date , "2014-01-01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_time , "08:01:01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].outlet_id , "1001" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_amount = 100.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_date , "2014-02-02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_time , "08:02:02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].outlet_id , "2002" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_amount = 200.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[2].card_no , "\"" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[3].card_no , "\"你\"他\"" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[4].card_no , "我\"你\"他\"她" );
DSCLOG_BankJsonTransaction( & trans );
memset( buf , 0x00 , sizeof(buf) );
len = sizeof(buf)-1 ;
nret = DSCSERIALIZE_JSON_BankJsonTransaction( & trans , "GBK" , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_JSON_BankJsonTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_JSON_BankJsonTransaction ok , len[%d]\n" , len );
}
printf( "[%s]\n" , buf );
/* ... client send communication struct to server ... */
/* server code */
memset( & trans , 0x00 , sizeof(BankJsonTransaction) );
nret = DSCDESERIALIZE_JSON_BankJsonTransaction( NULL , buf , & len , & trans ) ;
if( nret )
{
printf( "DSCDESERIALIZE_JSON_BankJsonTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_JSON_BankJsonTransaction ok\n" );
}
DSCLOG_BankJsonTransaction( & trans );
return 0;
}
int main()
{
return -test_serialize_json();
}
$ make
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -I/home/calvin/exinc/fasterxml -I/home/calvin/exinc/fasterjson -c test_serialize_json.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_json test_serialize_json.o IDL_BankJsonTransaction.dsc.o -L. -L/home/calvin/exlib -lfasterjson
$ ./test_serialize_json
BankJsonTransaction.version[1]
BankJsonTransaction.ResponseHeader.transaction_code[P0101]
BankJsonTransaction.ResponseHeader.trans_jnlsno[12345678]
BankJsonTransaction.ResponseHeader.response_code[0]
BankJsonTransaction.ResponseHeader.response_desc[OK]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check account passed]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check channel passed]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.title_text[DETAIL TITLE]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.page_no[1]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.page_size[2]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-01-01]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:01:01]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[1001]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[100.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-02-02]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:02:02]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[2002]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[200.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no["]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no["你"他"]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[我"你"他"她]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
DSCSERIALIZE_JSON_BankJsonTransaction ok , len[2330]
[{
"BankJsonTransaction" :
{
"version" : 1 ,
"ResponseHeader" :
{
"transaction_code" : "P0101" ,
"trans_jnlsno" : 12345678 ,
"response_code" : 0 ,
"response_desc" : "OK"
} ,
"QueryTransactionDetails" :
{
"AddonMessages" :
{
"AddonMessage" :
[
{
"message_text" : "Check account passed"
} ,
{
"message_text" : "Check channel passed"
} ,
{
"message_text" : ""
}
]
} ,