-
Notifications
You must be signed in to change notification settings - Fork 27
/
Pkg.Json.Visualizer.pas
139 lines (114 loc) · 3.64 KB
/
Pkg.Json.Visualizer.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
unit Pkg.Json.Visualizer;
interface
uses
FMX.TreeView,
Pkg.Json.Mapper, Pkg.Json.StubField;
Type
JsonVisualizer = class
private
class procedure InternalFormatTreeViewFields(AItem: TTreeViewItem);
class procedure FormatFields(aTreeView: TTreeView);
class procedure InternalVisualize(aTreeViewItem: TTreeViewItem; aClass: TStubClass; AItemStyleLookup: string);
public
// Visualizes stub class structure in a treeview
class procedure Visualize(aTreeView: TTreeView; AItemStyleLookup: string; aJsonString: string); overload;
class procedure Visualize(aTreeView: TTreeView; AItemStyleLookup: string; aMapper: TPkgJsonMapper); overload;
end;
implementation
uses
System.Sysutils, Pkg.Json.JsonValueHelper;
{ TJsonVisualizer }
class procedure JsonVisualizer.Visualize(aTreeView: TTreeView; AItemStyleLookup: string; aJsonString: string);
var
JsonMapper: TPkgJsonMapper;
begin
JsonMapper := TPkgJsonMapper.Create;
try
JsonMapper.Parse(aJsonString);
Visualize(aTreeView, AItemStyleLookup, JsonMapper);
finally
JsonMapper.Free;
end;
end;
class procedure JsonVisualizer.FormatFields(aTreeView: TTreeView);
begin
if aTreeView.Count = 1 then
InternalFormatTreeViewFields(aTreeView.Items[0]);
end;
class procedure JsonVisualizer.InternalFormatTreeViewFields(AItem: TTreeViewItem);
var
LItem: TTreeViewItem;
k: Integer;
LSize, LPos: Integer;
begin
LSize := 0;
// Find max len
for k := 0 to AItem.Count - 1 do
begin
LItem := AItem.Items[k];
LPos := Pos(':', LItem.Text);
if (LPos > 0) AND (LPos > LSize) then
LSize := LPos;
end;
for k := 0 to AItem.Count - 1 do
begin
LItem := AItem.Items[k];
LPos := LSize - Pos(':', LItem.Text);
if (LPos > 0) then
LItem.Text := LItem.Text.Replace(':', StringOfChar(' ', LPos) + ':');
InternalFormatTreeViewFields(LItem);
end;
end;
class procedure JsonVisualizer.InternalVisualize(aTreeViewItem: TTreeViewItem; aClass: TStubClass; AItemStyleLookup: string);
var
StubField: TStubField;
TreeViewItem: TTreeViewItem;
begin
for StubField in aClass.Items do
begin
TreeViewItem := TTreeViewItem.Create(aTreeViewItem);
TreeViewItem.StyleLookup := AItemStyleLookup;
TreeViewItem.TagObject := StubField;
TreeViewItem.WordWrap := false;
case StubField.FieldType of
jtObject:
begin
TreeViewItem.Text := StubField.Name + ': {} ' + StubField.TypeAsString;
InternalVisualize(TreeViewItem, (StubField as TStubObjectField).FieldClass, AItemStyleLookup);
end;
jtArray:
begin
TreeViewItem.Text := StubField.Name + ': [] ' + StubField.TypeAsString;
if (StubField as TStubArrayField).ContainedType = jtObject then
InternalVisualize(TreeViewItem, (StubField as TStubArrayField).FieldClass, AItemStyleLookup);
end;
else
TreeViewItem.Text := StubField.Name + ': ' + StubField.TypeAsString;
end;
aTreeViewItem.AddObject(TreeViewItem);
end;
end;
class procedure JsonVisualizer.Visualize(aTreeView: TTreeView; AItemStyleLookup: string; aMapper: TPkgJsonMapper);
var
TreeViewItem: TTreeViewItem;
RootClass: TStubClass;
begin
aTreeView.Clear;
try
RootClass := aMapper.RootClass;
if RootClass = nil then
exit;
aTreeView.BeginUpdate;
TreeViewItem := TTreeViewItem.Create(aTreeView);
TreeViewItem.Text := RootClass.Name;
TreeViewItem.TagObject := RootClass;
TreeViewItem.WordWrap := false;
aTreeView.AddObject(TreeViewItem);
InternalVisualize(TreeViewItem, RootClass, AItemStyleLookup);
FormatFields(aTreeView);
finally
aTreeView.ExpandAll;
aTreeView.EndUpdate;
end;
end;
end.