JSON和数据集互相转换单元

JSON和数据集互相转换单元

如题......只是一个单元, 为了测试JSON单元性能的...

具体测试结果参考: http://www.cnblogs.com/hs-kill/p/3668052.html

代码中用到的SevenZIP单元在这里: http://www.cnblogs.com/hs-kill/p/3876160.html

  1 unit DSCJSON;
  2 
  3 (*
  4 作者: 刘志林
  5 最后修改日期: 2016-08-17
  6 版本: 1.3
  7 
  8 修改历史:
  9   1.3
 10     去掉字符编码配置, 去掉二进制编码配置
 11     增加对二进制字段压缩的支持(使用7-ZIP)
 12     优化了一些代码结构
 13   1.2
 14     支持QJSON
 15     增加SYSTEM.JSON单元支持(D10中新单元, 就是原有的DBXJSON)
 16   1.1
 17     支持FireDAC
 18     增加DBXJSON单元支持
 19     增加对NULL值字段支持
 20   1.0:
 21     支持ADO/ClientDataset与JSON互相转换
 22     使用SuperObject单元作为JSON解析单元
 23 
 24 联系方式: lzl_17948876@hotmail.com
 25 
 26 !!! 若有修改,请通知作者,谢谢合作 !!!
 27 *)
 28 
 29 {$DEFINE FIREDAC}
 30 //{$DEFINE ADO}
 31 
 32 {$DEFINE QJSON}
 33 //{$DEFINE JSON_SO}
 34 //{$DEFINE JSON_DBX}
 35 //{$DEFINE JSON_SYS}
 36 {$IF DEFINED(JSON_DBX) OR DEFINED(JSON_SYS)}
 37   {$DEFINE SYSJSON}
 38 {$ENDIF}
 39 
 40 //{$DEFINE 7ZIP}
 41 
 42 interface
 43 
 44 uses
 45   SysUtils, Classes, DB, DBClient, DateUtils
 46 {$IFDEF JSON_DBX}
 47   , DBXJSON
 48 {$ENDIF}
 49 {$IFDEF JSON_SO}
 50   , superobject, Variants
 51 {$ENDIF}
 52 {$IFDEF FIREDAC}
 53   , FireDAC.Comp.DataSet
 54 {$ENDIF}
 55 {$IFDEF JSON_SYS}
 56   , System.JSON
 57 {$ENDIF}
 58 {$IFDEF QJSON}
 59   , QJSON
 60 {$ENDIF}
 61 {$IFDEF ADO}
 62   , ADODB
 63 {$ENDIF}
 64   , EncdDecd;
 65 
 66 /// <summary>将数据集转化为JSON数据</summary>
 67 /// <param name="ADataSet">TDataSet - 数据集</param>
 68 /// <param name="AJSON">WideString - 输出转换结果</param>
 69 /// <returns>转换结果 成功: True 失败: False</returns>
 70 function DataSetToJSON(ADataSet: TDataSet; var AJSON: WideString): Boolean;
 71 
 72 /// <summary>JSON数据转换为结果集</summary>
 73 /// <param name="AJSON">JSON数据</param>
 74 /// <param name="ADataSet">数据集</param>
 75 /// <returns>转换结果 成功: True 失败: False</returns>
 76 function JSONToDataSet(AJSON: WideString; ADataSet: TDataSet): Boolean;
 77 
 78 implementation
 79 
 80 {$IFDEF 7ZIP}
 81 uses
 82   SevenZIP;
 83 {$ENDIF}
 84 
 85 (*
 86   C: 配置表
 87     C.BC BolbComp 二进制字段是否压缩
 88       0:未压缩 1:已压缩
 89       默认 0
 90     C.CC CompClass 压缩类型 {使用7-ZIP压缩, 如果不打开7ZIP编译开关, 此控制无效认为不压缩}
 91       空表示不压缩, 否则为7-ZIP压缩类型
 92       默认 CLSID_CFormatGZip : TGUID = '{23170F69-40C1-278A-1000-000110EF0000}'
 93 
 94   T: 表结构表
 95     T.N:列名
 96     T.D:显示列名
 97     T.T:列数据类型 Data.DB.TFieldType
 98     T.L:列数据长度
 99     T.R:列值是否允许为空
100 
101   R: 数据表
102 
103 {
104   "C":{"BC":0, "CM":"", "BE":0},
105   "T":[{"N":"FieldName", "D":"DisplayName", "T":0, "L":100, "R":1}],
106   "R":[["Field1Value", "Field2Value"]]
107 }
108 
109 *)
110 
111 const
112   _FT_STRING = $00; {字符}
113   _FT_INTEGER = $01; {整形}
114   _FT_FLOAT = $02; {浮点}
115   _FT_DATETIME = $03; {日期}
116   _FT_BOOLEAN = $04; {布尔}
117   _FT_BLOB = $05; {二进制}
118 
119   _FT_CURRENCY = $10; {金额}
120 
121 type
122   TConfig = record
123     BolbComp: Boolean; {二进制字段是否压缩}
124     CompClass: string; {压缩模式 空表示不压缩, 否则为7-ZIP压缩类型}
125   end;
126 
127 function JSONToDataSet(AJSON: WideString; ADataSet: TDataSet): Boolean;
128 var
129   nJDS: {$IFDEF SYSJSON}TJSONObject{$ENDIF}
130         {$IFDEF JSON_SO}ISuperObject{$ENDIF}
131         {$IFDEF QJSON}TQJson{$ENDIF}
132         ;
133   nConfig: TConfig;
134 
135   procedure _JTDConfig;
136   var
137     nJO: {$IFDEF SYSJSON}TJSONObject{$ENDIF}
138           {$IFDEF JSON_SO}ISuperObject{$ENDIF}
139           {$IFDEF QJSON}TQJson{$ENDIF}
140           ;
141   begin
142     with nConfig do
143     begin
144       BolbComp := False;
145       CompClass := '';
146     end;
147 
148 {$IFDEF SYSJSON}
149     nJO := nJDS.GetValue('C') as TJSONObject;
150     if nJO = nil then
151       Exit;
152 {$ENDIF}
153 {$IFDEF JSON_SO}
154     nJO := nJDS.N['C'];
155     if nJO.DataType = stNull then
156       Exit;
157 {$ENDIF}
158 {$IFDEF QJSON}
159     nJO := nJDS.ItemByName('C');
160     if nJO.DataType = jdtNull then
161       Exit;
162 {$ENDIF}
163     with nConfig do
164     begin
165       BolbComp := {$IFDEF SYSJSON}TJSONNumber(nJO.GetValue('BC')).AsInt{$ENDIF}
166                   {$IFDEF JSON_SO}nJO.I['BC']{$ENDIF}
167                   {$IFDEF QJSON}nJO.ItemByName('BC').AsInteger{$ENDIF}
168                    = 1;
169 {$IFDEF 7ZIP}
170       CompClass := {$IFDEF SYSJSON}nJO.GetValue('CC').Value{$ENDIF}
171                   {$IFDEF JSON_SO}nJO['CC'].AsString{$ENDIF}
172                   {$IFDEF QJSON}nJO.ItemByName('CC').AsString{$ENDIF}
173                   ;
174 {$ELSE}
175       CompClass := '';
176 {$ENDIF}
177     end;
178   end;
179 
180   function _JTDStepField: Boolean;
181   var
182     nFName, nFDisplay: String;
183     i, nFLength: Integer;
184     nFType: Byte;
185     nFD: TFieldDef;
186     nFRequired: Boolean;
187 {$IFDEF SYSJSON}
188     nJA: TJSONArray;
189     nJO: TJSONObject;
190     nJV: TJSONValue;
191     nJP: TJSONPair;
192 {$ENDIF}
193 {$IFDEF JSON_SO}
194     nJA: TSuperArray;
195     nJO, nJR: ISuperObject;
196 {$ENDIF}
197 {$IFDEF QJSON}
198     nJO, nJR: TQJson;
199 {$ENDIF}
200   begin
201     Result := False;
202     ADataSet.Close;
203 
204 {$IFDEF SYSJSON}
205     nJA := nJDS.GetValue('T') as TJSONArray;
206     if nJA = nil then
207       Exit;
208 {$ENDIF}
209 {$IFDEF JSON_SO}
210     nJO := nJDS.N['T'];
211     if nJO.DataType = stNull then
212       Exit;
213 {$ENDIF}
214 {$IFDEF QJSON}
215     nJO := nJDS.ItemByName('T');
216     if nJO.DataType = jdtNull then
217       Exit;
218 {$ENDIF}
219 
220     ADataSet.FieldDefs.BeginUpdate;
221     try
222       ADataSet.FieldDefs.Clear;
223 
224     {拆解Field}
225 {$IFDEF SYSJSON}
226       for i := 0 to nJA.Size - 1 do
227       begin
228         nJO := nJA.Get(i) as TJSONObject;
229         nFName := nJO.GetValue('N').Value;
230         nFDisplay := nJO.GetValue('D').Value;
231         nFType := TJSONNumber(nJO.GetValue('T')).AsInt;
232         nFLength := TJSONNumber(nJO.GetValue('L')).AsInt;
233         nFRequired := Boolean(TJSONNumber(nJO.GetValue('R')).AsInt);
234 {$ENDIF}
235 {$IFDEF JSON_SO}
236       nJA := nJO.AsArray;
237       for i := 0 to nJA.Length - 1 do
238       begin
239         nJR := nJA[i];
240         nFName := nJR['N'].AsString;
241         nFDisplay := nJR['D'].AsString;
242         nFType := nJR['T'].AsInteger;
243         nFLength := nJR['L'].AsInteger;
244         nFRequired := Boolean(nJR['R'].AsInteger);
245 {$ENDIF}
246 {$IFDEF QJSON}
247       for i := 0 to nJO.Count - 1 do
248       begin
249         nJR := nJO.Items[i];
250         nFName := nJR.ItemByName('N').AsString;
251         nFDisplay := nJR.ItemByName('D').AsString;
252         nFType := nJR.ItemByName('T').AsInteger;
253         nFLength := nJR.ItemByName('L').AsInteger;
254         nFRequired := Boolean(nJR.ItemByName('R').AsInteger);
255 {$ENDIF}
256         nFD := ADataSet.FieldDefs.AddFieldDef;
257         with nFD do
258         try
259           Name := nFName;
260           case nFType of
261             _FT_INTEGER:
262               DataType := ftLargeint;
263             _FT_FLOAT:
264               DataType := ftFloat;
265             _FT_DATETIME:
266               DataType := ftDateTime;
267             _FT_BOOLEAN:
268               DataType := ftBoolean;
269             _FT_BLOB:
270               DataType := ftBlob;
271             _FT_CURRENCY:
272               DataType := ftCurrency;
273           else
274             DataType := ftString;
275             Size := nFLength;
276           end;
277           Required := nFRequired;
278           DisplayName := nFDisplay;
279         except
280           DisposeOf;
281         end;
282       end;
283     finally
284       ADataSet.FieldDefs.EndUpdate;
285     end;
286     Result := True;
287   end;
288 
289   function _JTDStepRecord: Boolean;
290   var
291     nFName, nStr: String;
292     i, j: Integer;
293     nField: TField;
294     nMSI, nMSO: TMemoryStream;
295 {$IFDEF 7ZIP}
296     nCItemIndex: Integer;
297     nMSC: TMemoryStream; {解压缩用}
298 {$ENDIF}
299     nJRA: {$IFDEF SYSJSON}TJSONArray{$ENDIF}
300           {$IFDEF JSON_SO}TSuperArray{$ENDIF}
301           {$IFDEF QJSON}TQJson{$ENDIF}
302           ;
303 {$IFDEF SYSJSON}
304     nJA: TJSONArray;
305 {$ENDIF}
306 {$IFDEF JSON_SO}
307     nJA: TSuperArray;
308     nJO, nJR: ISuperObject;
309 {$ENDIF}
310 {$IFDEF QJSON}
311     nJO: TQJson;
312 {$ENDIF}
313   begin
314     Result := False;
315 {$IFDEF SYSJSON}
316     nJA := nJDS.GetValue('R') as TJSONArray;
317     if nJA = nil then
318       Exit;
319 {$ENDIF}
320 {$IFDEF JSON_SO}
321     nJO := nJDS.N['R'];
322     if nJO.DataType = stNull then
323       Exit;
324     nJA := nJO.AsArray;
325 {$ENDIF}
326 {$IFDEF QJSON}
327     nJO := nJDS.ItemByName('R');
328     if nJO.DataType = jdtNull then
329       Exit;
330 {$ENDIF}
331     nMSO := TMemoryStream.Create;
332     nMSI := TStringStream.Create;
333 {$IFDEF 7ZIP}
334     nMSC := TMemoryStream.Create;
335 {$ENDIF}
336     ADataSet.DisableControls;
337     try
338       for i := 0 to {$IFDEF SYSJSON}nJA.Size - 1{$ENDIF}
339                     {$IFDEF JSON_SO}nJA.Length - 1{$ENDIF}
340                     {$IFDEF QJSON}nJO.Count - 1{$ENDIF}
341       do
342       begin
343         nJRA := {$IFDEF SYSJSON}nJA.Get(i) as TJSONArray{$ENDIF}
344                 {$IFDEF JSON_SO}nJA[i].AsArray{$ENDIF}
345                 {$IFDEF QJSON}nJO.Items[i]{$ENDIF}
346                 ;
347         ADataSet.Append;
348         for j := 0 to ADataSet.Fields.Count - 1 do
349         begin
350           nField := ADataSet.Fields[j];
351           nFName := nField.FieldName;
352           if
353             {$IFDEF SYSJSON}nJRA.Get(j).Null{$ENDIF}
354             {$IFDEF JSON_SO}nJRA[j].DataType = stNull{$ENDIF}
355             {$IFDEF QJSON}nJRA[j].DataType = jdtNull{$ENDIF}
356           then
357           begin
358             nField.SetData(nil);
359           end
360           else
361           begin
362             case nField.DataType of
363               ftLargeint:
364               begin
365                 nField.Value := {$IFDEF SYSJSON}TJSONNumber(nJRA.Get(j)).AsInt64{$ENDIF}
366                                 {$IFDEF JSON_SO}nJRA[j].AsInteger{$ENDIF}
367                                 {$IFDEF QJSON}nJRA.Items[j].AsInteger{$ENDIF}
368                                 ;
369               end;
370               ftFloat, ftCurrency:
371               begin
372                 nField.Value := {$IFDEF SYSJSON}TJSONNumber(nJRA.Get(j)).AsDouble{$ENDIF}
373                                 {$IFDEF JSON_SO}nJRA[j].AsDouble{$ENDIF}
374                                 {$IFDEF QJSON}nJRA.Items[j].AsFloat{$ENDIF}
375                                 ;
376               end;
377               ftDateTime:
378               begin
379                 nField.Value := UnixToDateTime(
380                                   {$IFDEF SYSJSON}TJSONNumber(nJRA.Get(j)).AsInt64{$ENDIF}
381                                   {$IFDEF JSON_SO}nJRA[j].AsInteger{$ENDIF}
382                                   {$IFDEF QJSON}nJRA.Items[j].AsInt64{$ENDIF}
383                                 );
384               end;
385               ftBoolean:
386               begin
387                 nField.Value := Boolean(
388                                   {$IFDEF SYSJSON}TJSONNumber(nJRA.Get(j)).AsInt{$ENDIF}
389                                   {$IFDEF JSON_SO}nJRA[j].AsInteger{$ENDIF}
390                                   {$IFDEF QJSON}nJRA.Items[j].AsInteger{$ENDIF}
391                                 );
392               end;
393               ftBlob:
394               begin
395                 nMSI.Clear;
396                 nMSO.Clear;
397                 nStr := {$IFDEF SYSJSON}TJSONString(nJRA.Get(j)).Value{$ENDIF}
398                         {$IFDEF JSON_SO}nJRA[j].AsString{$ENDIF}
399                         {$IFDEF QJSON}nJRA.Items[j].AsString{$ENDIF}
400                         ;
401                 nMSI.Write(nStr[1], Length(nStr) * SizeOf(Char));
402                 nMSI.Position := 0;
403                 nMSO.Clear;
404 {$IFDEF 7ZIP}
405                 if nConfig.CompClass = '' then
406                 begin
407 {$ENDIF}
408                   DecodeStream(nMSI, nMSO);
409 {$IFDEF 7ZIP}
410                 end
411                 else
412                 try
413                   nMSC.Clear;
414                   DecodeStream(nMSI, nMSC);
415                   nMSC.Position := 0;
416                   with CreateInArchive(TGUID.Create(nConfig.CompClass)) do
417                   begin
418                     OpenStream(T7zStream.Create(nMSC, soReference));
419                     for nCItemIndex := 0 to NumberOfItems - 1 do
420                       if not ItemIsFolder[nCItemIndex] then
421                       begin
422                         ExtractItem(nCItemIndex, nMSO, False);
423                         Break;
424                       end;
425                   end;
426                 except
427                   {此处解压缩异常后, 默认不写入数据, 根据实际情况进行处理}
428                   nMSO.Clear;
429                 end;
430 {$ENDIF}
431                 nMSO.Position := 0;
432                 TBlobField(nField).LoadFromStream(nMSO);
433               end;
434             else
435               nField.Value := {$IFDEF SYSJSON}TJSONString(nJRA.Get(j)).Value{$ENDIF}
436                               {$IFDEF JSON_SO}nJRA[j].AsString{$ENDIF}
437                               {$IFDEF QJSON}nJRA.Items[j].AsString{$ENDIF}
438                               ;
439             end;
440           end;
441         end;
442         ADataSet.Post;
443       end;
444       ADataSet.First;
445     finally
446       ADataSet.EnableControls;
447       nMSO.Free;
448       nMSI.Free;
449 {$IFDEF 7ZIP}
450       nMSC.Free;
451 {$ENDIF}
452     end;
453     Result := True;
454   end;
455 
456 begin
457   if ADataSet = nil then
458     Exit;
459 
460 {$IFDEF SYSJSON}
461   nJDS := TJSONObject.ParseJSONValue(AJSON) as TJSONObject;
462   try
463 {$ENDIF}
464 {$IFDEF JSON_SO}
465   nJDS := SO(AJSON);
466 {$ENDIF}
467 {$IFDEF QJSON}
468   nJDS := TQJson.Create;
469   nJDS.Parse(AJSON);
470   try
471 {$ENDIF}
472     try
473       _JTDConfig;
474 
475       if ADataSet is TCustomClientDataSet then
476       begin
477         Result := _JTDStepField;
478         if Result then
479         begin
480           TCustomClientDataSet(ADataSet).CreateDataSet;
481           Result := _JTDStepRecord;
482         end;
483       end
484 {$IFDEF ADO}
485       else if ADataSet is TADODataSet then
486       begin
487         Result := _JTDStepField;
488         if Result then
489         begin
490           TADODataSet(ADataSet).CreateDataSet;
491           Result := _JTDStepRecord;
492         end;
493       end
494 {$ENDIF}
495 {$IFDEF FIREDAC}
496       else if ADataSet is TFDDataSet then
497       begin
498         Result := _JTDStepField;
499         if Result then
500         begin
501           TFDDataSet(ADataSet).CreateDataSet;
502           Result := _JTDStepRecord;
503         end;
504       end
505 {$ENDIF}
506       else
507         Result := False;
508     except
509       Result := False;
510     end;
511 {$IFDEF SYSJSON}
512   finally
513     nJDS.Free;
514   end;
515 {$ENDIF}
516 {$IFDEF QJSON}
517   finally
518     nJDS.Free;
519   end;
520 {$ENDIF}
521 end;
522 
523 function DataSetToJSON(ADataSet: TDataSet; var AJSON: WideString): Boolean;
524 var
525   nJA, nJRA: {$IFDEF SYSJSON}TJSONArray{$ENDIF}
526              {$IFDEF JSON_SO}TSuperArray{$ENDIF}
527              {$IFDEF QJSON}TQJson{$ENDIF}
528              ;
529   nJDS: {$IFDEF SYSJSON}TJSONObject{$ENDIF}
530         {$IFDEF JSON_SO}ISuperObject{$ENDIF}
531         {$IFDEF QJSON}TQJson{$ENDIF}
532         ;
533 {$IFDEF SYSJSON}
534   nJO: TJSONObject;
535 {$ENDIF}
536 {$IFDEF JSON_SO}
537   nJR: ISuperObject;
538 {$ENDIF}
539   i: Integer;
540   nTitle, nStr, nFDisplay: string;
541   nField: TField;
542   nFT: Byte;
543   nMSI: TMemoryStream;
544   nSSO: TStringStream;
545   nCompClassStr: string;
546 {$IFDEF 7ZIP}
547   nMSC: TMemoryStream; {解压缩用}
548 {$ENDIF}
549 const
550   _DEF_TITLE = '{"C":{"BC":0,"CC":"%s"},"T":[],"R":[]}';
551   _DEf_RECORD = '{"N":"%s","D":"%s","T":%d,"L":%d,"R":%d}';
552 begin
553   Result := False;
554 {$IFDEF 7ZIP}
555   nCompClassStr := CLSID_CFormatGZip.ToString;
556 {$ELSE}
557   nCompClassStr := '';
558 {$ENDIF}
559   nTitle := Format(_DEF_TITLE, [nCompClassStr]);
560 {$IFDEF SYSJSON}
561   nJDS := TJSONObject.ParseJSONValue(nTitle) as TJSONObject;
562 {$ENDIF}
563 {$IFDEF JSON_SO}
564   nJDS := SO(nTitle);
565 {$ENDIF}
566 {$IFDEF QJSON}
567   nJDS := TQJson.Create;
568   nJDS.Parse(nTitle);
569 {$ENDIF}
570   ADataSet.DisableControls;
571   nMSI := TMemoryStream.Create;
572   nSSO := TStringStream.Create;
573 {$IFDEF 7ZIP}
574   nMSC := TMemoryStream.Create;
575 {$ENDIF}
576   try
577     nJA := {$IFDEF SYSJSON}nJDS.GetValue('T') as TJSONArray{$ENDIF}
578            {$IFDEF JSON_SO}nJDS.A['T']{$ENDIF}
579            {$IFDEF QJSON}nJDS.ItemByName('T'){$ENDIF}
580            ;
581     AJSON := '';
582     try
583       ADataSet.First;
584       for i := 0 to ADataSet.Fields.Count - 1 do
585       begin
586         nField := ADataSet.Fields[i];
587         case nField.DataType of
588           ftSmallint, ftInteger, ftWord, ftLargeint, ftLongWord, ftShortint, ftByte:
589             nFT := _FT_INTEGER;
590           ftFloat, ftBCD, ftSingle, ftExtended:
591             nFT := _FT_FLOAT;
592           ftDate, ftTime, ftDateTime:
593             nFT := _FT_DATETIME;
594           ftBoolean:
595             nFT := _FT_BOOLEAN;
596           ftBlob, ftMemo, ftGraphic:
597             nFT := _FT_BLOB;
598           ftCurrency:
599             nFT := _FT_CURRENCY;
600         else
601           nFT := _FT_STRING;
602         end;
603         if nField.DisplayLabel = nField.FieldName then
604           nFDisplay := ''
605         else
606           nFDisplay := nField.DisplayLabel;
607         nStr := Format(_DEf_RECORD, [nField.FieldName, nFDisplay, nFT,
608           nField.DataSize, Byte(nField.Required)]);
609 {$IFDEF SYSJSON}
610         nJA.AddElement(TJSONObject.ParseJSONValue(nStr));
611 {$ENDIF}
612 {$IFDEF JSON_SO}
613         nJA.Add(SO(nStr));
614 {$ENDIF}
615 {$IFDEF QJSON}
616         nJA.Add.Parse(nStr);
617 {$ENDIF}
618       end;
619 
620       nJA := {$IFDEF SYSJSON}nJDS.GetValue('R') as TJSONArray{$ENDIF}
621              {$IFDEF JSON_SO}nJDS.A['R']{$ENDIF}
622              {$IFDEF QJSON}nJDS.ItemByName('R'){$ENDIF}
623              ;
624       while not ADataSet.Eof do
625       begin
626 {$IFDEF SYSJSON}
627         nJRA := TJSONArray.Create;
628         nJA.AddElement(nJRA);
629 {$ENDIF}
630 {$IFDEF JSON_SO}
631         nJR := SA([]);
632         nJA.Add(nJR);
633         nJRA := nJR.AsArray;
634 {$ENDIF}
635 {$IFDEF QJSON}
636         nJRA := nJA.Add('', jdtArray);
637 {$ENDIF}
638         for i := 0 to ADataSet.Fields.Count - 1 do
639         begin
640           nField := ADataSet.Fields[i];
641           if nField.IsNull then
642           begin
643 {$IFDEF SYSJSON}
644             nJRA.AddElement(TJSONNull.Create);
645 {$ENDIF}
646 {$IFDEF JSON_SO}
647             nJRA.Add(SO(NULL));
648 {$ENDIF}
649 {$IFDEF QJSON}
650             nJRA.Add('', jdtNull);
651 {$ENDIF}
652           end
653           else
654           begin
655             case nField.DataType of
656               ftSmallint, ftInteger, ftWord, ftLargeint, ftLongWord, ftShortint, ftByte:
657               begin
658 {$IFDEF SYSJSON}
659                 nJRA.Add(nField.AsInteger);
660 {$ENDIF}
661 {$IFDEF JSON_SO}
662                 nJRA.Add(SO(nField.AsInteger));
663 {$ENDIF}
664 {$IFDEF QJSON}
665                 nJRA.Add.AsInteger := nField.AsInteger;
666 {$ENDIF}
667               end;
668               ftFloat, ftBCD, ftSingle, ftExtended, ftCurrency:
669               begin
670 {$IFDEF SYSJSON}
671                 nJRA.Add(nField.AsFloat);
672 {$ENDIF}
673 {$IFDEF JSON_SO}
674                 nJRA.Add(SO(nField.AsFloat));
675 {$ENDIF}
676 {$IFDEF QJSON}
677                 nJRA.Add.AsFloat := nField.AsFloat;
678 {$ENDIF}
679               end;
680               ftDate, ftTime, ftDateTime:
681               begin
682 {$IFDEF SYSJSON}
683                 nJRA.Add(DateTimeToUnix(nField.AsDateTime));
684 {$ENDIF}
685 {$IFDEF JSON_SO}
686                 nJRA.Add(SO(DateTimeToUnix(nField.AsDateTime)));
687 {$ENDIF}
688 {$IFDEF QJSON}
689                 nJRA.Add.AsInt64 := DateTimeToUnix(nField.AsDateTime);
690 {$ENDIF}
691               end;
692               ftBlob, ftMemo, ftGraphic:
693               begin
694                 nMSI.Clear;
695                 nSSO.Clear;
696 {$IFDEF 7ZIP}
697                 if nCompClassStr <> '' then
698                 try
699                   nMSC.Clear;
700                   TBlobField(nField).SaveToStream(nMSC);
701                   nMSC.Position := 0;
702                   with CreateOutArchive(TGUID.Create(nCompClassStr)) do
703                   begin
704                     AddStream(nMSC, soReference, faArchive, CurrentFileTime, CurrentFileTime,
705                       ExtractFilePath(ParamStr(0)), False, False);
706                     SaveToStream(nMSI);
707                   end;
708                 except
709                   nMSI.Clear;
710                 end
711                 else
712                 begin
713 {$ENDIF}
714                   TBlobField(nField).SaveToStream(nMSI);
715 {$IFDEF 7ZIP}
716                 end;
717 {$ENDIF}
718                 nMSI.Position := 0;
719                 EncodeStream(nMSI, nSSO);
720 {$IFDEF SYSJSON}
721                 nJRA.Add(nSSO.DataString);
722 {$ENDIF}
723 {$IFDEF JSON_SO}
724                 nJRA.Add(SO(nSSO.DataString));
725 {$ENDIF}
726 {$IFDEF QJSON}
727                 nJRA.Add('', nSSO.DataString);
728 {$ENDIF}
729               end;
730             else
731 {$IFDEF SYSJSON}
732               nJRA.Add(nField.AsString);
733 {$ENDIF}
734 {$IFDEF JSON_SO}
735               nJRA.Add(SO(nField.AsString));
736 {$ENDIF}
737 {$IFDEF QJSON}
738               nJRA.Add('', nField.AsString);
739 {$ENDIF}
740             end;
741           end;
742         end;
743         ADataSet.Next;
744       end;
745       AJSON := {$IFDEF SYSJSON}nJDS.ToString{$ENDIF}
746                {$IFDEF JSON_SO}nJDS.AsJSon(False, False){$ENDIF}
747                {$IFDEF QJSON}nJDS.Encode(False){$ENDIF}
748                ;
749       Result := True;
750     except
751     end;
752   finally
753 {$IFDEF 7ZIP}
754     nMSC.Free;
755 {$ENDIF}
756     nMSI.Free;
757     nSSO.Free;
758     ADataSet.EnableControls;
759 {$IFDEF SYSJSON}
760     nJDS.Free;
761 {$ENDIF}
762 {$IFDEF QJSON}
763     nJDS.Free;
764 {$ENDIF}
765   end;
766 end;
767 
768 end.

 

转载自 http://www.cnblogs.com/hs-kill/p/4959879.html

posted @ 2016-08-17 14:46  今心  阅读(2344)  评论(0编辑  收藏  举报