-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathufrmJsonConvertMain.pas
More file actions
424 lines (357 loc) · 12.3 KB
/
ufrmJsonConvertMain.pas
File metadata and controls
424 lines (357 loc) · 12.3 KB
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
unit ufrmJsonConvertMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.Buttons;
type
TAddJsonCustomerProc = reference to procedure(const FirstName, LastName, City, State: string);
TAddJsonInvoiceProc = reference to procedure(const InvoiceID: Integer; const InvoiceDT: TDateTime; const InvoiceTotal: Double);
TAddJsonItemProc = reference to procedure(const LineID, TrackID: Integer; const UnitPrice: Double; const Quantity: Integer);
TfrmJSONConvert = class(TForm)
Panel1: TPanel;
DBGrid1: TDBGrid;
Panel2: TPanel;
DBGrid2: TDBGrid;
Label1: TLabel;
Label2: TLabel;
Panel3: TPanel;
Label3: TLabel;
DBGrid3: TDBGrid;
dsCustomers: TDataSource;
srcInvoices: TDataSource;
srcInvItems: TDataSource;
btnExport: TBitBtn;
cmbJsonLibs: TComboBox;
procedure btnExportClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FirstTime: Boolean;
FExportFilename: string;
procedure ExportItems(AddJsonItemProc: TAddJsonItemProc);
procedure ExportInvoices(AddJsonInvoiceProc: TAddJsonInvoiceProc);
procedure ExportCustomers(AddJsonCustomerProc: TAddJsonCustomerProc);
procedure ExportToJson;
procedure ExportToSystemJson;
procedure ExportToEasyJson;
procedure ExportToMcJson;
procedure ExportToVSoftYAML;
procedure ExportToSuperObject;
end;
var
frmJSONConvert: TfrmJSONConvert;
implementation
{$R *.dfm}
uses
System.Diagnostics,
System.JSON, {Embarcadero}
EasyJson, {tinyBigGames}
McJson, {HydroByte}
VSoft.YAML, {VSoft Technologies}
SuperObject, {Vadim Lou}
udmJsonConvert;
procedure TfrmJSONConvert.btnExportClick(Sender: TObject);
begin
ExportToJson;
end;
procedure TfrmJSONConvert.FormActivate(Sender: TObject);
begin
if FirstTime then begin
FirstTime := False;
dmJsonConvert.OpenTables;
end;
end;
procedure TfrmJSONConvert.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
FirstTime := True;
end;
procedure TfrmJSONConvert.ExportToJson;
begin
case cmbJsonLibs.ItemIndex of
0: ExportToSystemJson;
1: ExportToEasyJson;
2: ExportToMcJson;
3: ExportToVSoftYAML;
4: ExportToSuperObject;
end;
ShowMessage('Exported to ' + FExportFilename);
end;
procedure TfrmJSONConvert.ExportCustomers(AddJsonCustomerProc: TAddJsonCustomerProc);
begin
dmJsonConvert.qryCustomers.First;
while not dmJsonConvert.qryCustomers.Eof do begin
AddJsonCustomerProc(dmJsonConvert.qryCustomersFirstName.AsString, dmJsonConvert.qryCustomersLastName.AsString,
dmJsonConvert.qryCustomersCity.AsString, dmJsonConvert.qryCustomersState.AsString);
dmJsonConvert.qryCustomers.Next;
end;
end;
procedure TfrmJSONConvert.ExportInvoices(AddJsonInvoiceProc: TAddJsonInvoiceProc);
begin
dmJsonConvert.qryInvoices.First;
while not dmJsonConvert.qryInvoices.Eof do begin
AddJsonInvoiceProc(dmJsonConvert.qryInvoicesInvoiceId.AsInteger, dmJsonConvert.qryInvoicesInvoiceDate.AsDateTime,
dmJsonConvert.qryInvoicesTotal.AsFloat);
dmJsonConvert.qryInvoices.Next;
end;
end;
procedure TfrmJSONConvert.ExportItems(AddJsonItemProc: TAddJsonItemProc);
begin
dmJsonConvert.qryInvoiceItems.First;
while not dmJsonConvert.qryInvoiceItems.Eof do begin
AddJsonItemProc(dmJsonConvert.qryInvoiceItemsInvoiceLineId.AsInteger, dmJsonConvert.qryInvoiceItemsTrackId.AsInteger,
dmJsonConvert.qryInvoiceItemsUnitPrice.AsFloat, dmJsonConvert.qryInvoiceItemsQuantity.AsInteger);
dmJsonConvert.qryInvoiceItems.Next;
end;
end;
procedure TfrmJSONConvert.ExportToSystemJson;
var
sj: TJSONObject;
begin
FExportFilename := 'System.json';
sj := TJSONObject.Create;
try
var sjCustArray := TJSONArray.Create;
// == CUSTOMERS ==
ExportCustomers(
procedure(const FirstName, LastName, City, State: string)
begin
var sjCust := TJSONObject.Create;
sjCust.AddPair('FirstName', FirstName)
.AddPair('LastName', LastName)
.AddPair('City', City)
.AddPair('State', State);
// === INVOICES ===
var sjInvList := TJSONArray.Create;
ExportInvoices(
procedure(const InvoiceID: Integer; const InvoiceDT: TDateTime; const InvoiceTotal: Double)
begin
var sjInv := TJSONObject.Create;
sjInv.AddPair('InvId', InvoiceID)
.AddPair('InvDT', InvoiceDT)
.AddPair('Total', InvoiceTotal);
// === INVOICE ITEMS ===
var sjInvItems := TJSONArray.Create;
ExportItems(
procedure(const LineID, TrackID: Integer; const UnitPrice: Double; const Quantity: Integer)
begin
var sjItem := TJSONObject.Create;
sjItem.AddPair('LineId', LineID)
.AddPair('TrackId', TrackID)
.AddPair('UnitPrice', UnitPrice)
.AddPair('Quantity', Quantity);
sjInvItems.Add(sjItem);
end);
sjInv.AddPair('items', sjInvItems);
sjInvList.Add(sjInv);
end);
sjCust.AddPair('invoices', sjInvList);
sjCustArray.Add(sjCust);
end);
sj.AddPair('customers', sjCustArray);
var sw := TStreamWriter.Create(FExportFilename, False);
try
sw.Write(sj.Format(2));
finally
sw.Free;
end;
finally
sj.Free;
end;
end;
procedure TfrmJSONConvert.ExportToEasyJson;
var
ej: TEasyJson;
begin
FExportFilename := 'EasyJson.json';
ej := TEasyJson.Create;
try
var ejCustList := ej.AddArray('customers');
var CustCount := 0;
// == CUSTOMERS ==
ExportCustomers(
procedure(const FirstName, LastName, City, State: string)
begin
var ejCust := TEasyJson.Create;
try
ejCust.Put('FirstName', FirstName)
.Put('LastName', LastName)
.Put('City', City)
.Put('State', State);
// === INVOICES ===
var ejInvList := ejCust.AddArray('invoices');
var InvCount := 0;
ExportInvoices(
procedure(const InvoiceID: Integer; const InvoiceDT: TDateTime; const InvoiceTotal: Double)
begin
var ejInv := TEasyJson.Create;
try
ejInv.Put('InvId', InvoiceID)
.Put('InvDT', InvoiceDT)
.Put('Total', InvoiceTotal);
// === INVOICE ITEMS ===
var ejInvItems := ejInv.AddArray('items');
var ItemCount := 0;
ExportItems(
procedure(const LineID, TrackID: Integer; const UnitPrice: Double; const Quantity: Integer)
begin
var ejItem := TEasyJson.Create;
try
ejItem.Put('LineId', LineID)
.Put('TrackId', TrackID)
.Put('UnitPrice', UnitPrice)
.Put('Quantity', Quantity);
ejInvItems.Put(ItemCount, ejItem);
Inc(ItemCount);
finally
ejItem.Free;
end;
end);
ejInvList.Put(InvCount, ejInv);
Inc(InvCount);
finally
ejInv.Free;
end;
end);
ejCustList.Put(CustCount, ejCust);
Inc(CustCount);
finally
ejCust.Free;
end;
end);
ej.SaveToFile(FExportFilename);
finally
ej.Free;
end;
end;
procedure TfrmJSONConvert.ExportToMcJson;
var
mj: TMcJsonItem;
begin
FExportFilename := 'McJson.json';
mj := TMcJsonItem.Create;
try
// == CUSTOMERS ==
mj.Add('customers', jitArray);
ExportCustomers(
procedure(const FirstName, LastName, City, State: string)
begin
var mjCust := mj['customers'].Add(jitObject);
mjCust.S['FirstName'] := FirstName;
mjCust.S['LastName'] := LastName;
mjCust.S['City'] := City;
mjCust.S['State'] := State;
// === INVOICES ===
mjCust.Add('invoices', jitArray);
ExportInvoices(
procedure(const InvoiceID: Integer; const InvoiceDT: TDateTime; const InvoiceTotal: Double)
begin
var mjInv := mjCust['invoices'].Add(jitObject);
mjInv.I['InvId'] := InvoiceID;
mjInv.D['InvDT'] := InvoiceDT;
mjInv.D['Total'] := InvoiceTotal;
// === INVOICE ITEMS ===
mjInv.Add('items', jitArray);
ExportItems(
procedure(const LineID, TrackID: Integer; const UnitPrice: Double; const Quantity: Integer)
begin
var mjInvItem := mjInv['items'].Add(jitObject);
mjInvItem.I['LineId'] := LineID;
mjInvItem.I['TrackId'] := TrackID;
mjInvItem.D['UnitPrice'] := UnitPrice;
mjInvItem.I['Quantity'] := Quantity;
end);
end);
end);
mj.SaveToFile(FExportFilename);
finally
mj.Free;
end;
end;
procedure TfrmJSONConvert.ExportToVSoftYAML;
var
vyj: IYAMLDocument;
begin
FExportFilename := 'VSoftYAML.json';
vyj := TYAML.CreateMapping;
// == CUSTOMERS ==
var vyjCustomers := vyj.AsMapping.AddOrSetSequence('customers');
ExportCustomers(
procedure(const FirstName, LastName, City, State: string)
begin
var vyjCust := vyjCustomers.AddMapping;
vyjCust.S['FirstName'] := FirstName;
vyjCust.S['LastName'] := LastName;
vyjCust.S['City'] := City;
vyjCust.S['State'] := State;
// === INVOICES ===
var vyjInvoices := vyjCust.AsMapping.AddOrSetSequence('invoices');
ExportInvoices(
procedure(const InvoiceID: Integer; const InvoiceDT: TDateTime; const InvoiceTotal: Double)
begin
var vyjInv := vyjInvoices.AddMapping;
vyjInv.I['InvId'] := InvoiceID;
vyjInv.D['InvDT'] := InvoiceDT;
vyjInv.F['Total'] := InvoiceTotal;
// === INVOICE ITEMS ===
var vyjItems := vyjInv.AsMapping.AddOrSetSequence('items');
ExportItems(
procedure(const LineID, TrackID: Integer; const UnitPrice: Double; const Quantity: Integer)
begin
var vyjInvItem := vyjItems.AddMapping;
vyjInvItem.I['LineId'] := LineID;
vyjInvItem.I['TrackId'] := TrackID;
vyjInvItem.F['UnitPrice'] := UnitPrice;
vyjInvItem.I['Quantity'] := Quantity;
end);
end);
end);
TYAML.WriteToJSONFile(vyj, FExportFilename);
end;
procedure TfrmJSONConvert.ExportToSuperObject;
var
soj: ISuperObject;
begin
FExportFilename := 'SuperObject.json';
soj := SO; // create a SuperObject
// == CUSTOMERS ==
var sojCustomers := SA; // create a SuperArray
ExportCustomers(
procedure(const FirstName, LastName, City, State: string)
begin
var soCust := SO;
soCust.S['FirstName'] := FirstName;
soCust.S['LastName'] := LastName;
soCust.S['City'] := City;
soCust.S['State'] := State;
// === INVOICES ===
var soInvList := SA;
ExportInvoices(
procedure(const InvoiceID: Integer; const InvoiceDT: TDateTime; const InvoiceTotal: Double)
begin
var soInv := SO;
soInv.I['InvId'] := InvoiceID;
soInv.V['InvDT'] := InvoiceDT;
soInv.D['Total'] := InvoiceTotal;
// === INVOICE ITEMS ===
var soInvItems := SA;
ExportItems(
procedure(const LineID, TrackID: Integer; const UnitPrice: Double; const Quantity: Integer)
begin
var soItem := SO;
soItem.I['LineId'] := LineID;
soItem.I['TrackId'] := TrackID;
soItem.D['UnitPrice'] := UnitPrice;
soItem.I['Quantity'] := Quantity;
soInvItems.AsArray.Add(soItem);
end);
soInv.O['items'] := soInvItems;
soInvList.AsArray.Add(soInv);
end);
soCust.O['invoices'] := soInvList;
sojCustomers.AsArray.Add(soCust);
end);
soj.AsObject.O['customers'] := sojCustomers;
soj.SaveTo(FExportFilename);
end;
end.