Delphi JSON (CDs contains delta packets)

Source: Internet
Author: User

In Delphi, datasets are the most common way of accessing data. Therefore, we must establish the relationship between JSON and Tdataset, and realize the communication and transformation between the data. It is important to note that this is only the normal conversion between Tdataset and JSON, since CDs contains delta packets, whose data formats are far more complex than ordinary tdataset.

The dataset field information is a complete dictionary information. Therefore, we must also establish the dictionary information in JSON in order to create the field information of the dataset. We set its JSON information as follows:

cols:[field List information], such as:

"Cols": [{"Jsontype": "Integer", "Fieldindex": 0, "FieldType": "Integer", "FieldSize": 0, "FieldName": "ID", "Required": false},{"Jsontype": "string", "Fieldindex": 1, "FieldType": "string", "FieldSize": +, "FieldName": "Title", "Required" : false},{"Jsontype": "Variant", "Fieldindex": 2, "FieldType": "Blob", "FieldSize": 0, "FieldName": "Picture", "Required" : false}]

The data information is the node of the database, and it is also an array nesting record information:

"Data": [Recordset information]

Say less nonsense, directly on the code:

  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. Create a field
  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//increment field cannot be entered and must be changed
  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 was 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 was 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. Create columns
  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. To create data for a dataset
  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. Invocation Example:
  218. DataSet to JSON object or JSON text
  219. Var
  220. Json:ttablejson;
  221. s:string;
  222. Begin
  223. S: = json. Jsonfromdataset (ADODATASET1);
  224. Read the string s in Tstringstream, save it as text, and look at its format.
  225. End
  226. JSON object or text, loading to the dataset
  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

Delphi JSON (CDs contains delta packets)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.