Skip to content

Commit 4599429

Browse files
committed
jsonV: support array in array
1 parent 93b637b commit 4599429

File tree

4 files changed

+69
-37
lines changed

4 files changed

+69
-37
lines changed

CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ v1.1.0
33
- JSON function: use '{' key suffix to start embedded document (instead of value '[')
44
- IJSONDocument.ToString re-done using IJSONEnumerator
55
- IJSONDocWithReUse to fix re-using pre-allocated keys
6+
- jsonV: now supports array as element in array
67

78
v1.0.5
89
- IJSONDocument.Delete

jsonDoc.pas

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,16 @@ function TJSONDocument.Parse(const JSONData: WideString): IJSONDocument;
384384
const
385385
VicinityExtent=8;
386386
begin
387-
if di<=VicinityExtent then
388-
Result:=#13#10'(#'+IntToStr(di)+')"'+Copy(jsonData,1,di-1)+
389-
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"'
387+
if l>VicinityExtent then
388+
if di<=VicinityExtent then
389+
Result:=#13#10'(#'+IntToStr(di)+')"'+Copy(jsonData,1,di-1)+
390+
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"'
391+
else
392+
Result:=#13#10'(#'+IntToStr(di)+')"...'+
393+
Copy(jsonData,di-VicinityExtent,VicinityExtent)+
394+
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"'
390395
else
391-
Result:=#13#10'(#'+IntToStr(di)+')"...'+
392-
Copy(jsonData,di-VicinityExtent,VicinityExtent)+
393-
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"';
396+
Result:=#13#10'(#'+IntToStr(di)+')"'+jsonData+'"';
394397
end;
395398
procedure Expect(c:WideChar;const msg:string);
396399
begin

jsonTS.pas

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,16 @@ function TJSONDocumentThreadSafe.Parse(const JSONData: WideString): IJSONDocumen
397397
const
398398
VicinityExtent=8;
399399
begin
400-
if di<=VicinityExtent then
401-
Result:=#13#10'(#'+IntToStr(di)+')"'+Copy(jsonData,1,di-1)+
402-
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"'
400+
if l>VicinityExtent then
401+
if di<=VicinityExtent then
402+
Result:=#13#10'(#'+IntToStr(di)+')"'+Copy(jsonData,1,di-1)+
403+
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"'
404+
else
405+
Result:=#13#10'(#'+IntToStr(di)+')"...'+
406+
Copy(jsonData,di-VicinityExtent,VicinityExtent)+
407+
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"'
403408
else
404-
Result:=#13#10'(#'+IntToStr(di)+')"...'+
405-
Copy(jsonData,di-VicinityExtent,VicinityExtent)+
406-
' >>> '+jsonData[di]+' <<< '+Copy(jsonData,di+1,VicinityExtent)+'"';
409+
Result:=#13#10'(#'+IntToStr(di)+')"'+jsonData+'"';
407410
end;
408411
procedure Expect(c:WideChar;const msg:string);
409412
begin

jsonV1.pas

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,36 @@ function TfrmJsonViewer.LoadJSON(const FilePath: string): IJSONDocument;
100100
m:=TMemoryStream.Create;
101101
try
102102
m.LoadFromFile(FilePath);
103-
//UTF-16
104-
if (PAnsiChar(m.Memory)[0]=#$FF) and
105-
(PAnsiChar(m.Memory)[1]=#$FE) then
106-
begin
107-
SetLength(w,i div 2);
108-
Move(w[1],PAnsiChar(m.Memory)[2],i);
109-
end
110-
else
111-
//UTF-8
112-
if (PAnsiChar(m.Memory)[0]=#$EF) and
113-
(PAnsiChar(m.Memory)[1]=#$BB) and
114-
(PAnsiChar(m.Memory)[2]=#$BF) then
115-
begin
116-
m.Position:=m.Size;
117-
i:=0;
118-
m.Write(i,1);
119-
w:=UTF8Decode(PAnsiChar(@PAnsiChar(m.Memory)[3]));
120-
end
121-
//ANSI
103+
if m.Size=0 then
104+
w:=''
122105
else
123106
begin
124-
m.Position:=m.Size;
125-
i:=0;
126-
m.Write(i,1);
127-
w:=PAnsiChar(m.Memory);
107+
//UTF-16
108+
if (PAnsiChar(m.Memory)[0]=#$FF) and
109+
(PAnsiChar(m.Memory)[1]=#$FE) then
110+
begin
111+
SetLength(w,i div 2);
112+
Move(w[1],PAnsiChar(m.Memory)[2],i);
113+
end
114+
else
115+
//UTF-8
116+
if (PAnsiChar(m.Memory)[0]=#$EF) and
117+
(PAnsiChar(m.Memory)[1]=#$BB) and
118+
(PAnsiChar(m.Memory)[2]=#$BF) then
119+
begin
120+
m.Position:=m.Size;
121+
i:=0;
122+
m.Write(i,1);
123+
w:=UTF8Decode(PAnsiChar(@PAnsiChar(m.Memory)[3]));
124+
end
125+
//ANSI
126+
else
127+
begin
128+
m.Position:=m.Size;
129+
i:=0;
130+
m.Write(i,1);
131+
w:=PAnsiChar(m.Memory);
132+
end;
128133
end;
129134
finally
130135
m.Free;
@@ -136,9 +141,10 @@ function TfrmJsonViewer.LoadJSON(const FilePath: string): IJSONDocument;
136141
procedure TfrmJsonViewer.TreeView1Expanding(Sender: TObject;
137142
Node: TTreeNode; var AllowExpansion: Boolean);
138143
var
139-
p:TJSONNode;
144+
p,q:TJSONNode;
140145
v:OleVariant;
141146
i,j,k:integer;
147+
ii:array of integer;
142148
x:IJSONDocument;
143149
begin
144150
p:=Node as TJSONNode;
@@ -154,7 +160,26 @@ procedure TfrmJsonViewer.TreeView1Expanding(Sender: TObject;
154160
// else
155161
begin
156162
v:=p.Data[p.Key];
157-
if p.Index<>-1 then v:=v[VarArrayLowBound(v,1)+p.Index];
163+
i:=0;
164+
if p.Index<>-1 then
165+
begin
166+
q:=p;
167+
while (q<>nil) and (q.Index<>-1) do
168+
begin
169+
inc(i);
170+
q:=q.Parent as TJSONNode;
171+
end;
172+
SetLength(ii,i);
173+
q:=p;
174+
while i<>0 do //while (q<>nil) and (q.Index<>-1) do
175+
begin
176+
dec(i);
177+
ii[i]:=q.Index;
178+
q:=q.Parent as TJSONNode;
179+
end;
180+
for i:=0 to Length(ii)-1 do
181+
v:=v[VarArrayLowBound(v,1)+ii[i]];
182+
end;
158183
//case VarType(v)
159184
if VarIsArray(v) then
160185
begin

0 commit comments

Comments
 (0)