码迷,mamicode.com
首页 > Windows程序 > 详细

delphi json(CDS包含了Delta数据包)

时间:2016-02-25 19:44:28      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:

在delphi中,数据集是最常用数据存取方式。因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换。值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包含了Delta数据包,其数据格式远比普通的TDataset更复杂。

数据集字段信息,是一个完整的字典信息。因此,我们在JSON必须也建立字典信息,才能创建数据集的字段信息。我们设置其JSON信息如下:

   COLS:[字段列表信息],如:

"Cols":[{"JsonType":"integer","FieldIndex":0,"FieldType":"Integer","FieldSize":0,"FieldName":"ID","Required":false},{"JsonType":"string","FieldIndex":1,"FieldType":"String","FieldSize":100,"FieldName":"Title","Required":false},{"JsonType":"variant","FieldIndex":2,"FieldType":"Blob","FieldSize":0,"FieldName":"Picture","Required":false}]

数据信息以Data做节点,也是一个数组嵌套记录信息:

"Data":[记录集信息]

废话少说,直接上代码:

  1. unit uDBJson;
  2. interface
  3. uses
  4. SysUtils,Classes,Variants,DB,DBClient,SuperObject;
  5. type
  6. TTableJSon = class
  7. private
  8.     const cstFieldType = ‘FieldType‘;
  9.     const cstFieldName = ‘FieldName‘;
  10.     const cstFieldSize = ‘FieldSize‘;
  11.     const cstJsonType = ‘JsonType‘;
  12.     const cstRequired = ‘Required‘;
  13.     const cstFieldIndex = ‘FieldIndex‘;
  14.     const cstCols= ‘Cols‘;
  15.     const cstData= ‘Data‘;
  16. public
  17.     class function JSonFromDataSet(DataSet:TDataSet):string;
  18.     class function CreateFieldByJson(Fields:TFieldDefs;ColsJson:ISuperObject):Boolean;
  19.     class function ImportDataFromJSon(DataSet:TDataSet;DataJson:ISuperObject):Integer;
  20.     class function CDSFromJSon(CDS:TClientDataSet;Json:ISuperObject):Boolean;
  21.     class function GetValue(Json:ISuperObject;const Name:string):Variant;
  22.     class function CreateJsonValue(Json:ISuperObject;const Name:string;const Value:Variant):Boolean;
  23.     class function CreateJsonValueByField(Json:ISuperObject;Field:TField):Boolean;
  24.     class function GetValue2Field(Field:TField;JsonValue:ISuperObject):Variant;
  25. end;
  26. implementation
  27. uses TypInfo,encddecd;
  28. { TTableJSon }
  29. class function TTableJSon.CDSFromJSon(CDS: TClientDataSet;
  30. Json: ISuperObject): Boolean;
  31. var
  32. ColsJson:ISuperObject;
  33. begin
  34. Result := False;
  35. if Json = nil then
  36.     Exit;
  37. CDS.Close;
  38. CDS.Data := Null;
  39. //创建字段
  40. ColsJson := Json.O[cstCols];
  41. CreateFieldByJson(CDS.FieldDefs,ColsJson);
  42. if CDS.FieldDefs.Count >0 then
  43.     CDS.CreateDataSet;
  44. ImportDataFromJSon(CDS,Json.O[cstData]);
  45. Result := True;
  46. end;
  47. class function TTableJSon.CreateFieldByJson(Fields: TFieldDefs;
  48. ColsJson: ISuperObject): Boolean;
  49. var
  50. SubJson:ISuperObject;
  51. ft:TFieldType;
  52. begin
  53. Result := False;
  54. Fields.DataSet.Close;
  55. Fields.Clear;
  56. for SubJson in ColsJson do
  57. begin
  58.     ft := TFieldType(GetEnumValue(TypeInfo(TFieldType),‘ft‘+SubJson.S[cstFieldType]));
  59.     if ft= ftAutoInc then //自增字段不能录入,必须更改
  60.       ft := ftInteger;
  61.     Fields.Add(SubJson.S[cstFieldName],ft,SubJson.I[cstFieldSize],SubJson.B[cstRequired]);
  62. end;
  63. Result := True;
  64. end;
  65. class function TTableJSon.CreateJsonValue(Json: ISuperObject;
  66. const Name: string; const Value: Variant): Boolean;
  67. begin
  68. Result := False;
  69. Json.O[Name] := SO(Value);
  70. Result := True;
  71. end;
  72. class function TTableJSon.CreateJsonValueByField(Json: ISuperObject;
  73. Field: TField): Boolean;
  74. begin
  75. Result := False;
  76. if Field Is TDateTimeField then
  77.     Json.O[Field.FieldName] := SO(Field.AsDateTime)
  78. else if Field is TBlobField then
  79.     Json.S[Field.FieldName] := EncodeString(Field.AsString)
  80. else
  81.     Json.O[Field.FieldName] := SO(Field.Value);
  82. Result := True;
  83. end;
  84. class function TTableJSon.GetValue(
  85. Json: ISuperObject;const Name: string): Variant;
  86. begin
  87. case Json.DataType of
  88.     stNull: Result := Null;
  89.     stBoolean: Result := Json.B[Name];
  90.     stDouble: Result := Json.D[Name];
  91.     stCurrency: Result := Json.C[Name];
  92.     stInt: Result := Json.I[Name];
  93.     stString: Result := Json.S[Name];
  94. end;
  95. end;
  96. class function TTableJSon.GetValue2Field(Field: TField; JsonValue:ISuperObject): Variant;
  97. begin
  98. if JsonValue.DataType = stNull then
  99.     Result := Null
  100. else if Field is TDateTimeField then
  101.     Result := JavaToDelphiDateTime(JsonValue.AsInteger)
  102. else if (Field is TIntegerField) or (Field is TLargeintField) then
  103.     Result := JsonValue.AsInteger
  104. else if Field is TNumericField then
  105.     Result := JsonValue.AsDouble
  106. else if Field is TBooleanField then
  107.     Result := JsonValue.AsBoolean
  108. else if Field is TStringField then
  109.     Result := JsonValue.AsString
  110. else if Field is TBlobField then
  111.     Result := DecodeString(JsonValue.AsString)    
  112. end;
  113. class function TTableJSon.ImportDataFromJSon(DataSet: TDataSet;
  114. DataJson: ISuperObject): Integer;
  115. var
  116. SubJson:ISuperObject;
  117. i:Integer;
  118. iter: TSuperObjectIter;
  119. begin
  120. if not DataSet.Active then
  121.     DataSet.Open;
  122. DataSet.DisableControls;
  123. try
  124.     for SubJson in DataJson do
  125.     begin
  126.       DataSet.Append;
  127.       if ObjectFindFirst(SubJson,iter) then
  128.       begin
  129.          repeat
  130.            if DataSet.FindField(iter.Ite.Current.Name)<>nil then
  131.              DataSet.FindField(iter.Ite.Current.Name).Value :=
  132.                 GetValue2Field(
  133.                 DataSet.FindField(iter.Ite.Current.Name),
  134.                 iter.Ite.Current.Value);
  135.          until not ObjectFindNext(iter) ;
  136.       end;
  137.       DataSet.Post;
  138.     end;
  139. finally
  140.     DataSet.EnableControls;
  141. end;
  142. end;
  143. class function TTableJSon.JSonFromDataSet(DataSet:TDataSet):string;
  144. procedure GetFieldTypeInfo(Field:TField;var Fieldtyp,JsonTyp:string);
  145. begin
  146.     Fieldtyp := GetEnumName(TypeInfo(tfieldtype),ord(Field.DataType));
  147.     Delete(Fieldtyp,1,2);
  148.     if Field is TStringField then
  149.       JsonTyp := ‘string‘
  150.     else if Field is TDateTimeField then
  151.       JsonTyp := ‘integer‘
  152.     else if (Field is TIntegerField) or (Field is TLargeintField) then
  153.       JsonTyp := ‘integer‘
  154.     else if Field is TCurrencyField then
  155.       JsonTyp := ‘currency‘
  156.     else if Field is TNumericField then
  157.       JsonTyp := ‘double‘
  158.     else if Field is TBooleanField then
  159.       JsonTyp := ‘boolean‘
  160.     else
  161.       JsonTyp := ‘variant‘;
  162. end;
  163. var
  164. sj,aj,sj2:ISuperObject;
  165. i:Integer;
  166. Fieldtyp,JsonTyp:string;
  167. List:TStringList;
  168. begin
  169. sj := SO();
  170. //创建列
  171. aj := SA([]);
  172. List := TStringList.Create;
  173. try
  174.     List.Sorted := True;
  175.    
  176.     for i := 0 to DataSet.FieldCount - 1 do
  177.     begin
  178.       sj2 := SO();
  179.       GetFieldTypeInfo(DataSet.Fields[i],Fieldtyp,JsonTyp);
  180.     
  181.       sj2.S[cstFieldName] := DataSet.Fields[i].FieldName;
  182.       sj2.S[cstFieldType] := Fieldtyp;
  183.       sj2.S[cstJsonType] := JsonTyp;
  184.       sj2.I[cstFieldSize] := DataSet.Fields[i].Size;
  185.       sj2.B[cstRequired] := DataSet.Fields[i].Required;
  186.       sj2.I[cstFieldIndex] := DataSet.Fields[i].Index;
  187.       aj.AsArray.Add(sj2);
  188.       List.Add(DataSet.Fields[i].FieldName+‘=‘+JsonTyp);
  189.     end;
  190.     sj.O[‘Cols‘] := aj;
  191.     //创建数据集的数据
  192.     DataSet.DisableControls;
  193.     DataSet.First;
  194.     aj := SA([]);
  195.     while not DataSet.Eof do
  196.     begin
  197.       sj2 := SO();
  198.       for i := 0 to DataSet.FieldCount - 1 do
  199.       begin
  200.         //sj2.S[IntToStr(DataSet.Fields[i].Index)] := VarToStrDef(DataSet.Fields[i].Value,‘‘);
  201.         if VarIsNull(DataSet.Fields[i].Value) then
  202.           sj2.O[DataSet.Fields[i].FieldName] := SO(Null)
  203.         else
  204.         begin
  205.           CreateJsonValueByField(sj2,DataSet.Fields[i]);
  206.         end;
  207.       end;
  208.       aj.AsArray.Add(sj2);
  209.       DataSet.Next;
  210.     end;
  211.     sj.O[‘Data‘] := aj;
  212.     Result := sj.AsString;
  213. finally
  214.     List.Free;
  215.     DataSet.EnableControls;
  216. end;
  217. end;
  218. end.
  219. 调用示例:
  220. //数据集转JSON对象或JSON文本
  221. var
  222. json:TTableJSon;
  223. s:string;
  224. begin
  225. S := json.JSonFromDataSet(ADODataSet1);
  226. //在用TStringStream读入字符串S,存成文本,看看其格式.
  227. end;
  228. //JSON对象或文本,装载到数据集
  229. var
  230. json:ISuperObject;
  231. begin
  232. json := TSuperObject.ParseFile(‘json.txt‘,False);
  233. TTableJSon.CDSFromJSon(cdsJSON,json);
  234. end;
http://blog.csdn.net/diligentcatrich/article/details/6913512

delphi json(CDS包含了Delta数据包)

标签:

原文地址:http://www.cnblogs.com/findumars/p/5218043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!