From 5dba1defe133c4350f6acce2c6bfcddd73f0839c Mon Sep 17 00:00:00 2001 From: Qianqian Fang Date: Wed, 27 Mar 2024 12:40:40 -0400 Subject: [PATCH] [bug] .. searches deep level of struct,make jdlink work for Octave 4.2 and 5.2 --- jdlink.m | 17 +++++++++++------ jsonpath.m | 6 ++++-- loadbj.m | 6 +++++- test/run_jsonlab_test.m | 14 +++++++------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/jdlink.m b/jdlink.m index 3142bff..b4d61a1 100644 --- a/jdlink.m +++ b/jdlink.m @@ -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) diff --git a/jsonpath.m b/jsonpath.m index 715fefb..6c4fee4 100644 --- a/jsonpath.m +++ b/jsonpath.m @@ -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 @@ -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); diff --git a/loadbj.m b/loadbj.m index 71866b7..62a8f31 100644 --- a/loadbj.m +++ b/loadbj.m @@ -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 diff --git a/test/run_jsonlab_test.m b/test/run_jsonlab_test.m index 6f00334..3906d3a 100644 --- a/test/run_jsonlab_test.m +++ b/test/run_jsonlab_test.m @@ -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);