-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONHelper.pas
82 lines (69 loc) · 2.49 KB
/
JSONHelper.pas
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
unit JSONHelper;
interface
function EscapeJSON(s : WideString) : String;
function JSONElement(name, value : WideString) : String; overload;
function JSONElement(name : String; value : Integer) : String; overload;
function JSONElementMoney(name : String; value : Double) : String;
function JSONElementDate(name : String; value : TDateTime) : String;
function JSONElementDateTime(name : String; value : TDateTime) : String;
function JSONElementBool(name : String; value : Boolean) : string;
function JSONElement(name : String; value : Boolean) : string; overload;
function JSONObject(name, sObjData : WideString) : string;
function JSONArray(name : String; sObjects : String) : string;
implementation
uses
System.SysUtils;
function EscapeJSON(s : WideString) : String;
begin
Result := StringReplace(StringReplace(s, '\', '\\', [rfReplaceAll]), '"', '\"', [rfReplaceAll]);
Result := StringReplace(Result, #13, '\\r', [rfReplaceAll]);
Result := StringReplace(Result, #10, '\\n', [rfReplaceAll]);
end;
function JSONElement(name, value : WideString) : String; overload;
begin
Result := Format('"%s":"%s"', [EscapeJSON(name), EscapeJSON(value)]);
end;
function JSONElement(name : String; value : Integer) : String; overload;
var
ws : WideString;
begin
ws := Format('%d', [value]);
Result := JSONElement(name, ws);
end;
function JSONElementMoney(name : String; value : Double) : String;
begin
Result := JSONElement(name, FormatFloat('0.00', value));
end;
function JSONElementDate(name : String; value : TDateTime) : String;
begin
Result := JSONElement(name, FormatDateTime('yyyy-mm-dd', value));
end;
function JSONElementDateTime(name : String; value : TDateTime) : String;
begin
// TODO: This should really be zulu time...
Result := JSONElement(name, FormatDateTime('yyyy-mm-dd hh:nn:ss AMPM', value));
end;
// Special version so boolean is not quoted.
function JSONElementBool(name : String; value : Boolean) : string;
begin
if value then
Result := Format('"%s":%s', [EscapeJSON(name), 'true'])
else
Result := Format('"%s":%s', [EscapeJSON(name), 'false'])
end;
function JSONElement(name : String; value : Boolean) : string; overload;
begin
if value then
Result := JSONElement(name, 'true')
else
Result := JSONElement(name, 'false');
end;
function JSONObject(name, sObjData : WideString) : string;
begin
Result := Format('"%s":{%s}', [name, sObjData]);
end;
function JSONArray(name : String; sObjects : String) : string;
begin
Result := Format('"%s":[%s]', [name, sObjects]);
end;
end.