-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunit1.pas
220 lines (198 loc) · 6.69 KB
/
unit1.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
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
(******************************************************************************)
(* JSON-Analyser ??.??.???? *)
(* *)
(* Version : 0.01 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Demo that shows how to use uJSON.pas and also implements a *)
(* JSON Prettyfier. *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* *)
(******************************************************************************)
Unit Unit1;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls, uJSON;
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
CheckBox1: TCheckBox;
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
Memo1: TMemo;
TreeView1: TTreeView;
Procedure Button1Click(Sender: TObject);
Procedure Button2Click(Sender: TObject);
Procedure Button3Click(Sender: TObject);
Procedure FormCreate(Sender: TObject);
private
Procedure CreateTree(N: TJSONObj; Root: TTreeNode);
public
End;
Var
Form1: TForm1;
Implementation
{$R *.lfm}
{ TForm1 }
Procedure TForm1.Button1Click(Sender: TObject);
Var
jo: TJSONObj;
p: TJSONParser;
Begin
p := TJSONParser.Create;
p.SupportJSON5Comments := CheckBox1.Checked;
Try
jo := p.Parse(memo1.Text);
Except
On av: Exception Do Begin
showmessage(av.Message);
jo := Nil;
End;
End;
p.free;
TreeView1.Items.Clear;
If Not assigned(jo) Then Begin
showmessage('Invalid JSON input');
exit;
End;
If assigned(jo) Then Begin
memo1.Text := trim(jo.ToString('')); // Pretty printed
// Generieren des TTreeview
CreateTree(jo, Nil);
jo.free;
TreeView1.FullExpand;
End
Else Begin
TreeView1.Items.Clear;
End;
End;
Procedure TForm1.Button2Click(Sender: TObject);
Begin
close;
End;
Procedure TForm1.Button3Click(Sender: TObject);
Var
jpo, jo: TJSONObj;
p: TJSONParser;
Begin
// Findpath
p := TJSONParser.Create;
p.SupportJSON5Comments := CheckBox1.Checked;
Try
jo := p.Parse(memo1.Text);
Except
On av: Exception Do Begin
showmessage(av.Message);
End;
End;
p.free;
If Not assigned(jo) Then Begin
showmessage('Invalid JSON input');
exit;
End;
jpo := jo.FindPath(ComboBox1.text);
If jpo <> Nil Then Begin
If jpo Is TJSONValue Then Begin
showmessage((jpo As TJSONValue).Value);
jo.free;
exit;
End;
If jpo Is TJSONTerminal Then Begin
showmessage((jpo As TJSONTerminal).Value);
jo.free;
exit;
End;
showmessage('Findpath gave result ' + jpo.ClassName + ' but there is no showing implementation in this demo for it.');
End
Else Begin
showmessage('Could not find path.');
End;
jo.free;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Begin
(*
* History: 0.01 = initial version
* 0.02 = Demo für Findpath integriert
* 0.03 = Fix AV-wenn Code nicht Parsebar
* Schriftart Courier New
* 0.04 = Support für JSON5 Kommentare
* 0.05 = Update Uncommenter für besseren Support der JSON5 Kommentare
*)
caption := 'JSON-Class Analyzer ver. 0.05';
Application.Title := caption;
ComboBox1.items.add('User:info.Username');
ComboBox1.items.add('User:info.Roles');
ComboBox1.items.add('User:info.Roles[0]');
ComboBox1.items.add('User:info.Roles[1]');
ComboBox1.items.add('app:options.localregion');
ComboBox1.itemindex := 0;
// memo1.Text := '{"box":[[1.2,3.4],[5.6,7.8]]}';
End;
Procedure TForm1.CreateTree(N: TJSONObj; Root: TTreeNode);
Var
t: TTreeNode;
//jt: TJSONString;
ja: TJSONArray;
jv: TJSONValue;
jn: TJSONNode;
jno: TJSONNodeObj;
i: Integer;
Begin
If n Is TJSONNode Then Begin
jn := n As TJSONNode;
t := TreeView1.Items.AddChild(root, 'TJSONNode');
For i := 0 To jn.ObjCount - 1 Do Begin
CreateTree(jn.Obj[i], t);
End;
exit;
End;
If n Is TJSONNodeObj Then Begin
jno := n As TJSONNodeObj;
t := TreeView1.Items.AddChild(root, 'TJSONNodeObj: ' + jno.Name);
CreateTree(jno.Value, t);
exit;
End;
If n Is TJSONValue Then Begin
jv := n As TJSONValue;
t := TreeView1.Items.AddChild(root, 'TJSONValue: ' + jv.Name);
exit;
End;
If n Is TJSONArray Then Begin
ja := n As TJSONArray;
t := TreeView1.Items.AddChild(root, 'TJSONArray (' + inttostr(ja.ObjCount) + ' elements)');
For i := 0 To ja.ObjCount - 1 Do Begin
CreateTree(ja.Obj[i], t);
End;
exit;
End;
If n Is TJSONTerminal Then Begin
//jt := n As TJSONTerminal;
t := TreeView1.Items.AddChild(root, 'TJSONTerminal');
exit;
End;
Raise exception.create('Error not implemented class: ' + n.ClassName);
End;
End.