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

时间:2022-12-04 10:48:10

在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. for i := 0 to DataSet.FieldCount - 1 do
  176. begin
  177. sj2 := SO();
  178. GetFieldTypeInfo(DataSet.Fields[i],Fieldtyp,JsonTyp);
  179. sj2.S[cstFieldName] := DataSet.Fields[i].FieldName;
  180. sj2.S[cstFieldType] := Fieldtyp;
  181. sj2.S[cstJsonType] := JsonTyp;
  182. sj2.I[cstFieldSize] := DataSet.Fields[i].Size;
  183. sj2.B[cstRequired] := DataSet.Fields[i].Required;
  184. sj2.I[cstFieldIndex] := DataSet.Fields[i].Index;
  185. aj.AsArray.Add(sj2);
  186. List.Add(DataSet.Fields[i].FieldName+'='+JsonTyp);
  187. end;
  188. sj.O['Cols'] := aj;
  189. //创建数据集的数据
  190. DataSet.DisableControls;
  191. DataSet.First;
  192. aj := SA([]);
  193. while not DataSet.Eof do
  194. begin
  195. sj2 := SO();
  196. for i := 0 to DataSet.FieldCount - 1 do
  197. begin
  198. //sj2.S[IntToStr(DataSet.Fields[i].Index)] := VarToStrDef(DataSet.Fields[i].Value,'');
  199. if VarIsNull(DataSet.Fields[i].Value) then
  200. sj2.O[DataSet.Fields[i].FieldName] := SO(Null)
  201. else
  202. begin
  203. CreateJsonValueByField(sj2,DataSet.Fields[i]);
  204. end;
  205. end;
  206. aj.AsArray.Add(sj2);
  207. DataSet.Next;
  208. end;
  209. sj.O['Data'] := aj;
  210. Result := sj.AsString;
  211. finally
  212. List.Free;
  213. DataSet.EnableControls;
  214. end;
  215. end;
  216. end.
  217. 调用示例:
  218. //数据集转JSON对象或JSON文本
  219. var
  220. json:TTableJSon;
  221. s:string;
  222. begin
  223. S := json.JSonFromDataSet(ADODataSet1);
  224. //在用TStringStream读入字符串S,存成文本,看看其格式.
  225. end;
  226. //JSON对象或文本,装载到数据集
  227. var
  228. json:ISuperObject;
  229. begin
  230. json := TSuperObject.ParseFile('json.txt',False);
  231. TTableJSon.CDSFromJSon(cdsJSON,json);
  232. end;
http://blog.csdn.net/diligentcatrich/article/details/6913512