This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPanamahSDK.JsonUtils.pas
163 lines (141 loc) · 6.02 KB
/
PanamahSDK.JsonUtils.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
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
unit PanamahSDK.JsonUtils;
interface
uses
Classes, DateUtils, SysUtils, Variants, PanamahSDK.Types, uLkJSON;
type
TSetFieldOption = (sfoKEEPCASE = 0);
TSetFieldOptions = set of TSetFieldOption;
function GetFieldValue(AJSONObject: TlkJSONbase; const AName: string; const ADefault: Variant): Variant; overload;
function GetFieldValue(AJSONObject: TlkJSONbase; const AName: string): Variant; overload;
function GetFieldValueAsString(AJSONObject: TlkJSONbase; const AName, ADefault: string): string; overload;
function GetFieldValueAsString(AJSONObject: TlkJSONbase; const AName: string): string; overload;
function GetFieldValueAsInteger(AJSONObject: TlkJSONbase; const AName: string; ADefault: Integer): Integer; overload;
function GetFieldValueAsInteger(AJSONObject: TlkJSONbase; const AName: string): Integer; overload;
function GetFieldValueAsBoolean(AJSONObject: TlkJSONbase; const AName: string; ADefault: Boolean): Boolean; overload;
function GetFieldValueAsBoolean(AJSONObject: TlkJSONbase; const AName: string): Boolean; overload;
function GetFieldValueAsDouble(AJSONObject: TlkJSONbase; const AName: string; ADefault: Double): Double; overload;
function GetFieldValueAsDouble(AJSONObject: TlkJSONbase; const AName: string): Double; overload;
function GetFieldValueAsDateTime(AJSONObject: TlkJSONbase; const AName: string; ADefault: TDateTime): Double; overload;
function GetFieldValueAsDateTime(AJSONObject: TlkJSONbase; const AName: string): TDateTime; overload;
procedure SetFieldValue(AJSONObject: TlkJSONobject; const AName: string; AValue: Variant); overload;
procedure SetFieldValue(AJSONObject: TlkJSONobject; const AName: string; AValue: Variant; const AOptions: TSetFieldOptions); overload;
procedure SetFieldValue(AJSONObject: TlkJSONobject; const AName: string; AValue: IJSONSerializable); overload;
implementation
function GetFieldValue(AJSONObject: TlkJSONbase; const AName: string; const ADefault: Variant): Variant; overload;
begin
Result := ADefault;
if AJSONObject.Field[AName] <> nil then
Result := AJSONObject.Field[AName].Value;
end;
function GetFieldValue(AJSONObject: TlkJSONbase; const AName: string): Variant; overload;
begin
Result := GetFieldValue(AJSONObject, AName, Unassigned);
end;
function GetFieldValueAsString(AJSONObject: TlkJSONbase; const AName, ADefault: string): string; overload;
var
FieldValue: Variant;
begin
FieldValue := GetFieldValue(AJSONObject, AName);
if VarIsNull(FieldValue) then
Result := EmptyStr
else
if FieldValue = Unassigned then
Result := ADefault
else
Result := VarToStr(FieldValue);
end;
function GetFieldValueAsString(AJSONObject: TlkJSONbase; const AName: string): string; overload;
begin
Result := GetFieldValueAsString(AJSONObject, AName, EmptyStr);
end;
function GetFieldValueAsInteger(AJSONObject: TlkJSONbase; const AName: string; ADefault: Integer): Integer; overload;
begin
Result := GetFieldValue(AJSONObject, AName);
if Result = Unassigned then
Result := ADefault
else
Result := StrToIntDef(VarToStr(Result), ADefault);
end;
function GetFieldValueAsInteger(AJSONObject: TlkJSONbase; const AName: string): Integer; overload;
begin
Result := GetFieldValueAsInteger(AJSONObject, AName, 0);
end;
function GetFieldValueAsBoolean(AJSONObject: TlkJSONbase; const AName: string; ADefault: Boolean): Boolean; overload;
begin
Result := GetFieldValue(AJSONObject, AName);
if Result = Unassigned then
Result := ADefault
else
Result := StrToBoolDef(VarToStr(Result), ADefault);
end;
function GetFieldValueAsBoolean(AJSONObject: TlkJSONbase; const AName: string): Boolean; overload;
begin
Result := GetFieldValueAsBoolean(AJSONObject, AName, False);
end;
function GetFieldValueAsDouble(AJSONObject: TlkJSONbase; const AName: string; ADefault: Double): Double; overload;
begin
Result := GetFieldValue(AJSONObject, AName);
if Result = Unassigned then
Result := ADefault
else
Result := StrToFloatDef(VarToStr(Result), ADefault);
end;
function GetFieldValueAsDouble(AJSONObject: TlkJSONbase; const AName: string): Double; overload;
begin
Result := GetFieldValueAsDouble(AJSONObject, AName, 0);
end;
function GetFieldValueAsDateTime(AJSONObject: TlkJSONbase; const AName: string; ADefault: TDateTime): Double; overload;
var
FieldValue: Variant;
begin
FieldValue := GetFieldValue(AJSONObject, AName);
if FieldValue = Unassigned then
Result := ADefault
else
begin
case VarType(FieldValue) of
varDate: Result := VarToDateTime(FieldValue);
varDouble: Result := FieldValue;
else
Result := ISO8601ToDateTime(FieldValue);
end;
end;
end;
function GetFieldValueAsDateTime(AJSONObject: TlkJSONbase; const AName: string): TDateTime; overload;
begin
Result := GetFieldValueAsDateTime(AJSONObject, AName, 0);
end;
procedure SetFieldValue(AJSONObject: TlkJSONobject; const AName: string; AValue: Variant); overload;
begin
SetFieldValue(AJSONObject, AName, AValue, []);
end;
procedure SetFieldValue(AJSONObject: TlkJSONobject; const AName: string; AValue: Variant; const AOptions: TSetFieldOptions); overload;
var
StringValue: string;
begin
case varType(AValue) of
varSmallint, varInteger, varInt64, varDouble, varCurrency, varShortInt, varWord, varLongWord:
AJSONObject.Add(AName, TlkJSONnumber.Generate(AValue));
varDate:
if AValue <> Unassigned then
AJSONObject.Add(AName, TlkJSONstring.Generate(DateTimeToISO8601(VarToDateTime(AValue))));
varBoolean:
AJSONObject.Add(AName, TlkJSONboolean.Generate(AValue <> 0));
varNull:
AJSONObject.Add(AName, TlkJSONnull.Create);
else
if AValue <> Unassigned then
begin
StringValue := VarToStr(AValue);
if not (sfoKEEPCASE in AOptions) then
StringValue := UpperCase(StringValue);
AJSONObject.Add(AName, TlkJSONstring.Generate(StringValue));
end;
end;
end;
procedure SetFieldValue(AJSONObject: TlkJSONobject; const AName: string; AValue: IJSONSerializable); overload;
begin
if AValue <> nil then
AJSONObject.Add(AName, TlkJSON.ParseText(AValue.SerializeToJSON));
end;
end.