-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUJSON.PAS
224 lines (204 loc) · 5.89 KB
/
UJSON.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
221
222
223
224
{
ujson Unit
A JSON file reader that returns a hierachy of objects
2022 LRT
}
unit
ujson;
interface
uses
ascii, consts, utils, uexc, uclasses, types, locale, uobject, ustream,
udict, ulist, ustring, unumber;
type
PJsonReader = ^TJsonReader;
TJsonReader = object (TObject)
public
function createFromStream(stream: PStream): PObject;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_stream: PStream;
function readNext: char;
function createObject: PObject;
function createNumber: PNumber;
function createBool: PNumber;
function createString: PString;
function createDict: PDictionary;
function createList: PList;
function exceptionStr: string;
end;
implementation
const
C_SKIPPABLE_CHARS = [#0, ' ', C_LF, C_CR];
C_DICTIONARY_KEY_CHARACTERS = ['A'..'Z','a'..'z','_','-'];
{ TJsonReader public }
function TJsonReader.createFromStream(stream: PStream): PObject;
begin
createFromStream := nil;
_stream := stream;
createFromStream := createObject;
_stream := nil;
end;
function TJsonReader.getClassName: string;
begin
getClassName := 'TJsonReader';
end;
function TJsonReader.getClassId: word;
begin
getClassId := C_CLASS_ID_JsonReader;
end;
{ TJsonReader private }
function TJsonReader.readNext: char;
var
result: word;
ch: char;
begin
ch := #0;
while not _stream^.isEOF and (ch in C_SKIPPABLE_CHARS) do
result := _stream^.read(@ch, 1);
if result = 1 then readNext := ch else readNext := #0;
end;
function TJsonReader.createObject: PObject;
var
ch: char;
begin
ch := readNext;
_stream^.seek(_stream^.getPosition-1);
case ch of
'0'..'9', '-': createObject := createNumber;
'"': createObject := createString;
'{': createObject := createDict;
'[': createObject := createList;
'f', 't': createObject := createBool;
else
raise(@self, 0, exceptionStr);
end;
end;
function TJsonReader.createNumber: PNumber;
var
ch: char;
str: string32;
result: word;
value: longint;
code: integer;
number: PNumber;
begin
str := '';
result := _stream^.read(@ch, 1);
if ch in ['-', '0'..'9'] then
begin
str := str + ch;
result := _stream^.read(@ch, 1);
end;
while not _stream^.isEOF and (ch in ['0'..'9']) do
begin
str := str + ch;
result := _stream^.read(@ch, 1);
end;
iassert(length(str)>0, @self, _stream^.getPosition, exceptionStr);
_stream^.seek(_stream^.getPosition-1);
val(str, value, code);
iassert(code=0, @self, _stream^.getPosition, exceptionStr);
number := new(PNumber, init);
number^.setValue(value);
createNumber := number;
end;
function TJsonReader.createBool: PNumber;
var
ch: char;
i: byte;
expected: string8;
value: PNumber;
begin
ch := readNext;
iassert(ch in ['t', 'f'], @self, _stream^.getPosition, exceptionStr);
if ch = 't' then expected := 'true' else expected := 'false';
i := 2;
while i <= length(expected) do
begin
ch := readNext;
iassert(ch = expected[i], @self, _stream^.getPosition, exceptionStr);
inc(i);
end;
value := new(PNumber, init);
if expected[1] = 't' then value^.setValue(1) else value^.setValue(0);
createBool := value;
end;
function TJsonReader.createString: PString;
var
ch: char;
result: word;
str: string;
pStr: PString;
begin
ch := readNext;
iassert(ch = '"', @self, _stream^.getPosition, exceptionStr);
result := _stream^.read(@ch, 1);
str := '';
while not _stream^.isEOF and (ch <> '"') do
begin
str := str + ch;
result := _stream^.read(@ch, 1);
end;
iassert(ch = '"', @self, _stream^.getPosition, exceptionStr);
pStr := new(PString, init);
pStr^.setValue(str);
createString := pStr;
end;
function TJsonReader.createDict: PDictionary;
var
ch: char;
keyStr: TDictionaryKey;
result: word;
dict: PDictionary;
obj: PObject;
begin
iassert(readNext = '{', @self, _stream^.getPosition, exceptionStr);
dict := new(PDictionary, init);
repeat
keyStr := '';
ch := #0;
while not _stream^.isEOF and (ch in C_SKIPPABLE_CHARS) do
result := _stream^.read(@ch, 1);
iassert(ch = '"', @self, _stream^.getPosition, exceptionStr);
result := _stream^.read(@ch, 1);
while not _stream^.isEOF and (ch in C_DICTIONARY_KEY_CHARACTERS) do
begin
keyStr := keyStr + ch;
result := _stream^.read(@ch, 1);
end;
iassert(ch = '"', @self, _stream^.getPosition, exceptionStr);
result := _stream^.read(@ch, 1);
iassert(length(keyStr) > 0, @self, _stream^.getPosition, exceptionStr);
iassert(ch = ':', @self, _stream^.getPosition, exceptionStr);
obj := createObject;
dict^.addObject(obj, keyStr);
obj^.release;
ch := readnext;
iassert(ch in [',', '}'], @self, _stream^.getPosition, exceptionStr);
until ch = '}';
createDict := dict;
end;
function TJsonReader.createList: PList;
var
ch: char;
list: PList;
obj: PObject;
begin
iassert(readNext = '[', @self, _stream^.getPosition, exceptionStr);
list := new(PList, init);
repeat
obj := createObject;
list^.addObject(obj);
obj^.release;
ch := readnext;
iassert(ch in [',', ']'], @self, _stream^.getPosition, exceptionStr);
until ch = ']';
createList := list;
end;
function TJsonReader.exceptionStr: string;
begin
exceptionStr := S_ERR_INVALID_JSON_FILE + ' (pos:' + inttostr(_stream^.getPosition) + ')';
end;
{ Other }
end.