-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathJSONTreeView.pas
More file actions
executable file
·279 lines (240 loc) · 7.28 KB
/
JSONTreeView.pas
File metadata and controls
executable file
·279 lines (240 loc) · 7.28 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
unit JSONTreeView;
// ***********************************************************************
//
// JSON TreeView Component
//
// pawel.glowacki@embarcadero.com
//
// July 2010 - version 1.0
// February 2016 - version 1.1
//
// ***********************************************************************
interface
uses
System.Classes, System.SysUtils, System.JSON, jsondoc, Vcl.ComCtrls;
type
EUnknownJsonValueDescendant = class(Exception)
constructor Create;
end;
TJSONTreeView = class(TTreeView)
private
FJSONDocument: TJSONDocument;
FVisibleChildrenCounts: boolean;
FVisibleByteSizes: boolean;
procedure SetJSONDocument(const Value: TJSONDocument);
procedure SetVisibleChildrenCounts(const Value: boolean);
procedure SetVisibleByteSizes(const Value: boolean);
procedure ProcessElement(currNode: TTreeNode; arr: TJSONArray;
aIndex: integer);
procedure ProcessPair(currNode: TTreeNode; obj: TJSONObject;
aIndex: integer);
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
public
class function IsSimpleJsonValue(v: TJSONValue): boolean; inline;
class function UnQuote(s: string): string; inline;
constructor Create(AOwner: TComponent); override;
procedure ClearAll;
procedure LoadJson;
published
property JSONDocument: TJSONDocument
read FJSONDocument write SetJSONDocument;
property VisibleChildrenCounts: boolean
read FVisibleChildrenCounts write SetVisibleChildrenCounts;
property VisibleByteSizes: boolean
read FVisibleByteSizes write SetVisibleByteSizes;
end;
implementation
{ TJSONTreeView }
procedure TJSONTreeView.ClearAll;
begin
Items.Clear;
end;
constructor TJSONTreeView.Create(AOwner: TComponent);
begin
inherited;
FVisibleChildrenCounts := true;
FVisibleByteSizes := false;
end;
class function TJSONTreeView.IsSimpleJsonValue(v: TJSONValue): boolean;
begin
Result := (v is TJSONNumber)
or (v is TJSONString)
or (v is TJSONTrue)
or (v is TJSONFalse)
or (v is TJSONNull);
end;
procedure TJSONTreeView.LoadJson;
var v: TJSONValue; currNode: TTreeNode; i, aCount: integer; s: string;
begin
ClearAll;
if (JSONDocument <> nil) and JSONDocument.IsActive then
begin
v := JSONDocument.RootValue;
Items.BeginUpdate;
try
Items.Clear;
if IsSimpleJsonValue(v) then
Items.AddChild(nil, UnQuote(v.Value))
else
if v is TJSONObject then
begin
aCount := TJSONObject(v).Count;
s := '{}';
if VisibleChildrenCounts then
s := s + ' (' + IntToStr(aCount) + ')';
if VisibleByteSizes then
s := s + ' (' + IntToStr(v.EstimatedByteSize) + ' bytes)';
currNode := Items.AddChild(nil, s);
for i := 0 to aCount - 1 do
ProcessPair(currNode, TJSONObject(v), i)
end
else
if v is TJSONArray then
begin
aCount := TJSONArray(v).Count;
s := '[]';
if VisibleChildrenCounts then
s := s + ' (' + IntToStr(aCount) + ')';
if VisibleByteSizes then
s := s + ' (' + IntToStr(v.EstimatedByteSize) + ' bytes)';
currNode := Items.AddChild(nil, s);
for i := 0 to aCount - 1 do
ProcessElement(currNode, TJSONArray(v), i)
end
else
raise EUnknownJsonValueDescendant.Create;
finally
Items.EndUpdate;
end;
FullExpand;
end;
end;
procedure TJSONTreeView.ProcessPair(currNode: TTreeNode; obj: TJSONObject; aIndex: integer);
var p: TJSONPair; s: string; n: TTreeNode; i, aCount: integer;
begin
p := obj.Pairs[aIndex];
s := UnQuote(p.JsonString.ToString) + ' : ';
if IsSimpleJsonValue(p.JsonValue) then
begin
Items.AddChild(currNode, s + p.JsonValue.ToString);
exit;
end;
if p.JsonValue is TJSONObject then
begin
aCount := TJSONObject(p.JsonValue).Count;
s := s + ' {}';
if VisibleChildrenCounts then
s := s + ' (' + IntToStr(aCount) + ')';
if VisibleByteSizes then
s := s + ' (' + IntToStr(p.EstimatedByteSize) + ' bytes)';
n := Items.AddChild(currNode, s);
for i := 0 to aCount - 1 do
ProcessPair(n, TJSONObject(p.JsonValue), i);
end
else if p.JsonValue is TJSONArray then
begin
aCount := TJSONArray(p.JsonValue).Count;
s := s + ' []';
if VisibleChildrenCounts then
s := s + ' (' + IntToStr(aCount) + ')';
if VisibleByteSizes then
s := s + ' (' + IntToStr(p.EstimatedByteSize) + ' bytes)';
n := Items.AddChild(currNode, s);
for i := 0 to aCount - 1 do
ProcessElement(n, TJSONArray(p.JsonValue), i);
end
else
raise EUnknownJsonValueDescendant.Create;
end;
procedure TJSONTreeView.ProcessElement(currNode: TTreeNode; arr: TJSONArray; aIndex: integer);
var v: TJSONValue; s: string; n: TTreeNode; i, aCount: integer;
begin
v := arr.Items[aIndex];
s := '[' + IntToStr(aIndex) + '] ';
if IsSimpleJsonValue(v) then
begin
Items.AddChild(currNode, s + v.ToString);
exit;
end;
if v is TJSONObject then
begin
aCount := TJSONObject(v).Count;
s := s + ' {}';
if VisibleChildrenCounts then
s := s + ' (' + IntToStr(aCount) + ')';
if VisibleByteSizes then
s := s + ' (' + IntToStr(v.EstimatedByteSize) + ' bytes)';
n := Items.AddChild(currNode, s);
for i := 0 to aCount - 1 do
ProcessPair(n, TJSONObject(v), i);
end
else if v is TJSONArray then
begin
aCount := TJSONArray(v).Count;
s := s + ' []';
n := Items.AddChild(currNode, s);
if VisibleChildrenCounts then
s := s + ' (' + IntToStr(aCount) + ')';
if VisibleByteSizes then
s := s + ' (' + IntToStr(v.EstimatedByteSize) + ' bytes)';
for i := 0 to aCount - 1 do
ProcessElement(n, TJSONArray(v), i);
end
else
raise EUnknownJsonValueDescendant.Create;
end;
procedure TJSONTreeView.SetJSONDocument(const Value: TJSONDocument);
begin
if FJSONDocument <> Value then
begin
FJSONDocument := Value;
ClearAll;
if FJSONDocument <> nil then
begin
if FJSONDocument.IsActive then
LoadJson;
end;
end;
end;
procedure TJSONTreeView.SetVisibleByteSizes(const Value: boolean);
begin
if FVisibleByteSizes <> Value then
begin
FVisibleByteSizes := Value;
LoadJson;
end;
end;
procedure TJSONTreeView.SetVisibleChildrenCounts(const Value: boolean);
begin
if FVisibleChildrenCounts <> Value then
begin
FVisibleChildrenCounts := Value;
LoadJson;
end;
end;
class function TJSONTreeView.UnQuote(s: string): string;
begin
Result := Copy(s,2,Length(s)-2);
end;
procedure TJSONTreeView.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
if Operation = opRemove then
if FJSONDocument <> nil then
if AComponent = FJSONDocument then
begin
FJSONDocument := nil;
ClearAll;
end;
end;
{ EUnknownJsonValueDescendant }
resourcestring
StrUnknownTJSONValueDescendant = 'Unknown TJSONValue descendant';
constructor EUnknownJsonValueDescendant.Create;
begin
inherited Create(StrUnknownTJSONValueDescendant);
end;
end.