Skip to content

Commit

Permalink
[bug] .. searches deep level of struct,make jdlink work for Octave 4.…
Browse files Browse the repository at this point in the history
…2 and 5.2
  • Loading branch information
fangq committed Mar 27, 2024
1 parent fea481e commit 5dba1de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
17 changes: 11 additions & 6 deletions jdlink.m
Expand Up @@ -88,18 +88,23 @@
if (opt.showlink)
fprintf(1, 'downloading from URL: %s\n', uripath);
end
rawdata = webread(uripath, weboptions('ContentType', 'binary'));

fname = [cachepath{1} filesep filename];
fpath = fileparts(fname);
if (~exist(fpath, 'dir'))
mkdir(fpath);
end
fid = fopen(fname, 'wb');
if (fid == 0)
error('can not save URL to cache at path %s', fname);
if (exist('websave'))
websave(fname, uripath);
else
rawdata = urlread(uripath);
fid = fopen(fname, 'wb');
if (fid == 0)
error('can not save URL to cache at path %s', fname);
end
fwrite(fid, uint8(rawdata));
fclose(fid);
end
fwrite(fid, uint8(rawdata));
fclose(fid);
newdata = loadjd(fname, opt);
elseif (~iscell(cachepath) && exist(cachepath, 'file'))
if (opt.showlink)
Expand Down
6 changes: 4 additions & 2 deletions jsonpath.m
Expand Up @@ -134,7 +134,7 @@
obj = {input(pathname)};
end
end
if (~exist('obj', 'var') && deepscan)
if (~exist('obj', 'var') || deepscan)
if (isa(input, 'containers.Map'))
items = keys(input);
else
Expand All @@ -143,7 +143,9 @@
for idx = 1:length(items)
if (isa(input, 'containers.Map'))
[val, isfound] = getonelevel(input(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);
elseif (length(input) > 1) % struct array
elseif (isa(input, 'table'))
[val, isfound] = getonelevel({input.(items{idx})}, [paths{1:pathid - 1} {['..' pathname]}], pathid);
elseif (isstruct(input) && length(input) > 1) % struct array
[val, isfound] = getonelevel({input.(items{idx})}, [paths{1:pathid - 1} {['..' pathname]}], pathid);
else
[val, isfound] = getonelevel(input.(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);
Expand Down
6 changes: 5 additions & 1 deletion loadbj.m
Expand Up @@ -102,7 +102,11 @@
string = fread(fid, jsonopt('MaxBuffer', inf, opt), 'uint8=>char')';
fclose(fid);
elseif (all(fname < 128) && ~isempty(regexpi(fname, '^\s*(http|https|ftp|file)://')))
string = char(webread(fname, weboptions('ContentType', 'binary')))';
if (exist('webread'))
string = char(webread(fname, weboptions('ContentType', 'binary')))';
else
string = urlread(fname);
end
elseif (~isempty(fname) && any(fname(1) == '[{SCHiUIulmLMhdDTFZN'))
string = fname;
else
Expand Down
14 changes: 7 additions & 7 deletions test/run_jsonlab_test.m
Expand Up @@ -376,18 +376,18 @@ function run_jsonlab_test(tests)
fprintf('Test JSONPath\n');
fprintf(sprintf('%s\n', char(ones(1, 79) * 61)));

testdata = struct('book', struct('title', {'Minch', 'Qui-Gon', 'Ben'}, 'author', {'Yoda', 'Jinn', 'Kenobi'}), 'game', struct('title', 'Mario'));
testdata = struct('book', struct('title', {'Minch', 'Qui-Gon', 'Ben'}, 'author', {'Yoda', 'Jinn', 'Kenobi'}), 'game', struct('title', 'Mario', 'new', struct('title', 'Minecraft')));
test_jsonlab('jsonpath of .key', @savejson, jsonpath(testdata, '$.game.title'), '"Mario"', 'compact', 1);
test_jsonlab('jsonpath of ..key', @savejson, jsonpath(testdata, '$.book..title'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
test_jsonlab('jsonpath of ..key cross objects', @savejson, jsonpath(testdata, '$..title'), '["Minch","Qui-Gon","Ben","Mario"]', 'compact', 1);
test_jsonlab('jsonpath of ..key cross objects', @savejson, jsonpath(testdata, '$..title'), '["Minch","Qui-Gon","Ben","Mario","Minecraft"]', 'compact', 1);
test_jsonlab('jsonpath of [index]', @savejson, jsonpath(testdata, '$..title[1]'), '["Qui-Gon"]', 'compact', 1);
test_jsonlab('jsonpath of [-index]', @savejson, jsonpath(testdata, '$..title[-1]'), '["Mario"]', 'compact', 1);
test_jsonlab('jsonpath of [-index]', @savejson, jsonpath(testdata, '$..title[-1]'), '["Minecraft"]', 'compact', 1);
test_jsonlab('jsonpath of [start:end]', @savejson, jsonpath(testdata, '$..title[0:2]'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
test_jsonlab('jsonpath of [:end]', @savejson, jsonpath(testdata, '$..title[:2]'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
test_jsonlab('jsonpath of [start:]', @savejson, jsonpath(testdata, '$..title[1:]'), '["Qui-Gon","Ben","Mario"]', 'compact', 1);
test_jsonlab('jsonpath of [-start:-end]', @savejson, jsonpath(testdata, '$..title[-2:-1]'), '["Ben","Mario"]', 'compact', 1);
test_jsonlab('jsonpath of [-start:]', @savejson, jsonpath(testdata, '$..title[:-3]'), '["Minch","Qui-Gon"]', 'compact', 1);
test_jsonlab('jsonpath of [:-end]', @savejson, jsonpath(testdata, '$..title[-1:]'), '["Mario"]', 'compact', 1);
test_jsonlab('jsonpath of [start:]', @savejson, jsonpath(testdata, '$..title[1:]'), '["Qui-Gon","Ben","Mario","Minecraft"]', 'compact', 1);
test_jsonlab('jsonpath of [-start:-end]', @savejson, jsonpath(testdata, '$..title[-2:-1]'), '["Mario","Minecraft"]', 'compact', 1);
test_jsonlab('jsonpath of [-start:]', @savejson, jsonpath(testdata, '$..title[:-3]'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
test_jsonlab('jsonpath of [:-end]', @savejson, jsonpath(testdata, '$..title[-1:]'), '["Minecraft"]', 'compact', 1);
test_jsonlab('jsonpath of object with [index]', @savejson, jsonpath(testdata, '$.book[1]'), '{"title":"Qui-Gon","author":"Jinn"}', 'compact', 1);
test_jsonlab('jsonpath of element after [index]', @savejson, jsonpath(testdata, '$.book[1:2].author'), '["Jinn","Kenobi"]', 'compact', 1);
test_jsonlab('jsonpath of [*] and deep scan', @savejson, jsonpath(testdata, '$.book[*]..author'), '["Yoda","Jinn","Kenobi"]', 'compact', 1);
Expand Down

0 comments on commit 5dba1de

Please sign in to comment.