-
Notifications
You must be signed in to change notification settings - Fork 5
/
TulipEasyJsonReader.pas
160 lines (130 loc) · 3.93 KB
/
TulipEasyJsonReader.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
{ ******************************************************* }
{ }
{ TULIP EASY JSON READER }
{ }
{ SAMER ASSIL - 2024 }
{ }
{ ******************************************************* }
unit TulipEasyJsonReader;
///
/// to use it:
/// 1. create TjsonValue object.
/// 2. use property "jData" to access the json fields.
///
/// To get elements from array , just pass the index as parameter.
///
interface
uses
System.JSON,
System.SysUtils,
System.StrUtils,
System.Variants,
System.Generics.Collections;
type
TJsonValueHelper = class helper for TJsonValue
private
function GetjData: Variant;
public
property jData: Variant read GetjData;
end;
TVarJsonValueType = class(TInvokeableVariantType)
protected
function FixupIdent(const AText: string): string; override;
public
procedure Clear(var V: TVarData); override;
procedure Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean); override;
function DoFunction(var Dest: TVarData; const V: TVarData; const Name: string; const Arguments: TVarDataArray): Boolean; override;
end;
{$A16}
type
TVarJsonValueData = record
VType: TVarType;
Reserved1, Reserved2, Reserved3: Word;
JVal: TJsonValue;
Reserved4: LongWord;
end;
var
VarJsonValueType: TVarJsonValueType = nil;
implementation
{ TVarJsonStringType }
procedure TVarJsonValueType.Clear(var V: TVarData);
begin
inherited;
SimplisticClear(V);
end;
procedure TVarJsonValueType.Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean);
begin
inherited;
SimplisticCopy(Dest, Source, Indirect);
end;
function TVarJsonValueType.DoFunction(var Dest: TVarData; const V: TVarData; const Name: string; const Arguments: TVarDataArray): Boolean;
var
jv: TJsonValue;
lv: TJsonValue;
begin
try
Result := true;
jv := TVarJsonValueData(V).JVal.FindValue(Name);
if not Assigned(jv) then
raise Exception.Create(format('"%s" not found', [Name]));
if length(Arguments) > 0 then
begin
lv := TJsonArray(jv).items[Arguments[0].VInteger];
if not Assigned(lv) then
raise Exception.Create(format('"%s" not found', [Name]));
if (lv is TJsonString) or (lv is TJsonNumber) or (lv is TJSONBool) or (lv is TJSONNull) then
begin
Variant(Dest) := lv.Value;
exit;
end;
if (lv is TJsonArray) or (lv is TJSONObject) then
begin
Variant(Dest) := lv.jData;
exit;
end;
end
else
begin
if (jv is TJsonString) or (jv is TJsonNumber) or (jv is TJSONBool) or (jv is TJSONNull) then
begin
Variant(Dest) := jv.Value;
exit;
end;
if (jv is TJsonArray) or (jv is TJSONObject) then
begin
Variant(Dest) := jv.jData;
exit;
end;
end;
except
on E: Exception do
begin
Result := false;
raise Exception.Create(E.Message);
end;
end;
end;
function TVarJsonValueType.FixupIdent(const AText: string): string;
begin
Result := AText;
Result := ReplaceText(Result, '__', ' ');
end;
{ TJsonValueHelper }
function TJsonValueHelper.GetjData: Variant;
begin
try
VarClear(Result);
TVarJsonValueData(Result).VType := VarJsonValueType.VarType;
TVarJsonValueData(Result).JVal := Self;
except
On E: Exception do
raise Exception.Create('[TulipJsonParse] ' + E.Message);
end;
end;
initialization
{ Create our custom variant type, which will be registered automatically. }
VarJsonValueType := TVarJsonValueType.Create;
finalization
{ Free our custom variant type. }
FreeAndNil(VarJsonValueType);
end.