Skip to content

Commit 394394a

Browse files
committed
[bug] improve jsonpath cell with deep scan
1 parent a599e71 commit 394394a

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

jsonpath.m

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
obj = input;
5252
elseif (regexp(pathname, '$\d+'))
5353
obj = input(str2double(pathname(2:end)) + 1);
54-
elseif (regexp(pathname, '^\[[0-9:]+\]$'))
54+
elseif (~isempty(regexp(pathname, '^\[[0-9:]+\]$', 'once')) || iscell(input))
5555
arraystr = pathname(2:end - 1);
5656
if (find(arraystr == ':'))
5757
[arraystr, arrayrange] = regexp(arraystr, '(\d*):(\d*)', 'match', 'tokens');
@@ -66,12 +66,31 @@
6666
else
6767
arrayrange{2} = length(input);
6868
end
69-
else
69+
elseif (regexp(arraystr, '^[0-9:]+', 'once'))
7070
arrayrange = str2double(arraystr) + 1;
7171
arrayrange = {arrayrange, arrayrange};
7272
end
73+
if (~exist('arrayrange', 'var'))
74+
arrayrange = {1, length(input)};
75+
end
7376
if (iscell(input))
7477
obj = {input{arrayrange{1}:arrayrange{2}}};
78+
if (deepscan)
79+
searchkey = ['..' pathname];
80+
[val, isfound] = getonelevel(obj, [paths{1:pathid} {searchkey}], pathid + 1);
81+
if (isfound)
82+
if (~exist('newobj', 'var'))
83+
newobj = {};
84+
end
85+
newobj = [newobj(:)', {val}];
86+
end
87+
if (exist('newobj', 'var'))
88+
obj = newobj;
89+
end
90+
end
91+
if (exist('obj', 'var') && iscell(obj) && length(obj) == 1)
92+
obj = obj{1};
93+
end
7594
else
7695
obj = input(arrayrange{1}:arrayrange{2});
7796
end
@@ -83,16 +102,21 @@
83102
end
84103
items = fieldnames(input);
85104
for idx = 1:length(items)
86-
[val, isfound] = getonelevel(input.(items{idx}), [paths{:} {['..' pathname]}], pathid + 1);
105+
[val, isfound] = getonelevel(input.(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);
87106
if (isfound)
88107
if (~exist('obj', 'var'))
89108
obj = {};
90109
end
91-
obj = [obj{:}, val];
110+
obj = [obj{:}, {val}];
92111
end
93112
end
113+
if (exist('obj', 'var') && length(obj) == 1)
114+
obj = obj{1};
115+
end
94116
else
95-
obj = input.(stpath);
117+
if (isfield(input, stpath))
118+
obj = input.(stpath);
119+
end
96120
end
97121
elseif (isa(input, 'containers.Map'))
98122
if (deepscan)

0 commit comments

Comments
 (0)