Skip to content

Commit

Permalink
[bug] improve jsonpath cell with deep scan
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Mar 21, 2024
1 parent a599e71 commit 394394a
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions jsonpath.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
obj = input;
elseif (regexp(pathname, '$\d+'))
obj = input(str2double(pathname(2:end)) + 1);
elseif (regexp(pathname, '^\[[0-9:]+\]$'))
elseif (~isempty(regexp(pathname, '^\[[0-9:]+\]$', 'once')) || iscell(input))
arraystr = pathname(2:end - 1);
if (find(arraystr == ':'))
[arraystr, arrayrange] = regexp(arraystr, '(\d*):(\d*)', 'match', 'tokens');
Expand All @@ -66,12 +66,31 @@
else
arrayrange{2} = length(input);
end
else
elseif (regexp(arraystr, '^[0-9:]+', 'once'))
arrayrange = str2double(arraystr) + 1;
arrayrange = {arrayrange, arrayrange};
end
if (~exist('arrayrange', 'var'))
arrayrange = {1, length(input)};
end
if (iscell(input))
obj = {input{arrayrange{1}:arrayrange{2}}};
if (deepscan)
searchkey = ['..' pathname];
[val, isfound] = getonelevel(obj, [paths{1:pathid} {searchkey}], pathid + 1);
if (isfound)
if (~exist('newobj', 'var'))
newobj = {};
end
newobj = [newobj(:)', {val}];
end
if (exist('newobj', 'var'))
obj = newobj;
end
end
if (exist('obj', 'var') && iscell(obj) && length(obj) == 1)
obj = obj{1};
end
else
obj = input(arrayrange{1}:arrayrange{2});
end
Expand All @@ -83,16 +102,21 @@
end
items = fieldnames(input);
for idx = 1:length(items)
[val, isfound] = getonelevel(input.(items{idx}), [paths{:} {['..' pathname]}], pathid + 1);
[val, isfound] = getonelevel(input.(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);
if (isfound)
if (~exist('obj', 'var'))
obj = {};
end
obj = [obj{:}, val];
obj = [obj{:}, {val}];
end
end
if (exist('obj', 'var') && length(obj) == 1)
obj = obj{1};
end
else
obj = input.(stpath);
if (isfield(input, stpath))
obj = input.(stpath);
end
end
elseif (isa(input, 'containers.Map'))
if (deepscan)
Expand Down

0 comments on commit 394394a

Please sign in to comment.