Skip to content

Commit 5dba1de

Browse files
committed
[bug] .. searches deep level of struct,make jdlink work for Octave 4.2 and 5.2
1 parent fea481e commit 5dba1de

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

jdlink.m

+11-6
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,23 @@
8888
if (opt.showlink)
8989
fprintf(1, 'downloading from URL: %s\n', uripath);
9090
end
91-
rawdata = webread(uripath, weboptions('ContentType', 'binary'));
91+
9292
fname = [cachepath{1} filesep filename];
9393
fpath = fileparts(fname);
9494
if (~exist(fpath, 'dir'))
9595
mkdir(fpath);
9696
end
97-
fid = fopen(fname, 'wb');
98-
if (fid == 0)
99-
error('can not save URL to cache at path %s', fname);
97+
if (exist('websave'))
98+
websave(fname, uripath);
99+
else
100+
rawdata = urlread(uripath);
101+
fid = fopen(fname, 'wb');
102+
if (fid == 0)
103+
error('can not save URL to cache at path %s', fname);
104+
end
105+
fwrite(fid, uint8(rawdata));
106+
fclose(fid);
100107
end
101-
fwrite(fid, uint8(rawdata));
102-
fclose(fid);
103108
newdata = loadjd(fname, opt);
104109
elseif (~iscell(cachepath) && exist(cachepath, 'file'))
105110
if (opt.showlink)

jsonpath.m

+4-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
obj = {input(pathname)};
135135
end
136136
end
137-
if (~exist('obj', 'var') && deepscan)
137+
if (~exist('obj', 'var') || deepscan)
138138
if (isa(input, 'containers.Map'))
139139
items = keys(input);
140140
else
@@ -143,7 +143,9 @@
143143
for idx = 1:length(items)
144144
if (isa(input, 'containers.Map'))
145145
[val, isfound] = getonelevel(input(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);
146-
elseif (length(input) > 1) % struct array
146+
elseif (isa(input, 'table'))
147+
[val, isfound] = getonelevel({input.(items{idx})}, [paths{1:pathid - 1} {['..' pathname]}], pathid);
148+
elseif (isstruct(input) && length(input) > 1) % struct array
147149
[val, isfound] = getonelevel({input.(items{idx})}, [paths{1:pathid - 1} {['..' pathname]}], pathid);
148150
else
149151
[val, isfound] = getonelevel(input.(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);

loadbj.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@
102102
string = fread(fid, jsonopt('MaxBuffer', inf, opt), 'uint8=>char')';
103103
fclose(fid);
104104
elseif (all(fname < 128) && ~isempty(regexpi(fname, '^\s*(http|https|ftp|file)://')))
105-
string = char(webread(fname, weboptions('ContentType', 'binary')))';
105+
if (exist('webread'))
106+
string = char(webread(fname, weboptions('ContentType', 'binary')))';
107+
else
108+
string = urlread(fname);
109+
end
106110
elseif (~isempty(fname) && any(fname(1) == '[{SCHiUIulmLMhdDTFZN'))
107111
string = fname;
108112
else

test/run_jsonlab_test.m

+7-7
Original file line numberDiff line numberDiff line change
@@ -376,18 +376,18 @@ function run_jsonlab_test(tests)
376376
fprintf('Test JSONPath\n');
377377
fprintf(sprintf('%s\n', char(ones(1, 79) * 61)));
378378

379-
testdata = struct('book', struct('title', {'Minch', 'Qui-Gon', 'Ben'}, 'author', {'Yoda', 'Jinn', 'Kenobi'}), 'game', struct('title', 'Mario'));
379+
testdata = struct('book', struct('title', {'Minch', 'Qui-Gon', 'Ben'}, 'author', {'Yoda', 'Jinn', 'Kenobi'}), 'game', struct('title', 'Mario', 'new', struct('title', 'Minecraft')));
380380
test_jsonlab('jsonpath of .key', @savejson, jsonpath(testdata, '$.game.title'), '"Mario"', 'compact', 1);
381381
test_jsonlab('jsonpath of ..key', @savejson, jsonpath(testdata, '$.book..title'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
382-
test_jsonlab('jsonpath of ..key cross objects', @savejson, jsonpath(testdata, '$..title'), '["Minch","Qui-Gon","Ben","Mario"]', 'compact', 1);
382+
test_jsonlab('jsonpath of ..key cross objects', @savejson, jsonpath(testdata, '$..title'), '["Minch","Qui-Gon","Ben","Mario","Minecraft"]', 'compact', 1);
383383
test_jsonlab('jsonpath of [index]', @savejson, jsonpath(testdata, '$..title[1]'), '["Qui-Gon"]', 'compact', 1);
384-
test_jsonlab('jsonpath of [-index]', @savejson, jsonpath(testdata, '$..title[-1]'), '["Mario"]', 'compact', 1);
384+
test_jsonlab('jsonpath of [-index]', @savejson, jsonpath(testdata, '$..title[-1]'), '["Minecraft"]', 'compact', 1);
385385
test_jsonlab('jsonpath of [start:end]', @savejson, jsonpath(testdata, '$..title[0:2]'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
386386
test_jsonlab('jsonpath of [:end]', @savejson, jsonpath(testdata, '$..title[:2]'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
387-
test_jsonlab('jsonpath of [start:]', @savejson, jsonpath(testdata, '$..title[1:]'), '["Qui-Gon","Ben","Mario"]', 'compact', 1);
388-
test_jsonlab('jsonpath of [-start:-end]', @savejson, jsonpath(testdata, '$..title[-2:-1]'), '["Ben","Mario"]', 'compact', 1);
389-
test_jsonlab('jsonpath of [-start:]', @savejson, jsonpath(testdata, '$..title[:-3]'), '["Minch","Qui-Gon"]', 'compact', 1);
390-
test_jsonlab('jsonpath of [:-end]', @savejson, jsonpath(testdata, '$..title[-1:]'), '["Mario"]', 'compact', 1);
387+
test_jsonlab('jsonpath of [start:]', @savejson, jsonpath(testdata, '$..title[1:]'), '["Qui-Gon","Ben","Mario","Minecraft"]', 'compact', 1);
388+
test_jsonlab('jsonpath of [-start:-end]', @savejson, jsonpath(testdata, '$..title[-2:-1]'), '["Mario","Minecraft"]', 'compact', 1);
389+
test_jsonlab('jsonpath of [-start:]', @savejson, jsonpath(testdata, '$..title[:-3]'), '["Minch","Qui-Gon","Ben"]', 'compact', 1);
390+
test_jsonlab('jsonpath of [:-end]', @savejson, jsonpath(testdata, '$..title[-1:]'), '["Minecraft"]', 'compact', 1);
391391
test_jsonlab('jsonpath of object with [index]', @savejson, jsonpath(testdata, '$.book[1]'), '{"title":"Qui-Gon","author":"Jinn"}', 'compact', 1);
392392
test_jsonlab('jsonpath of element after [index]', @savejson, jsonpath(testdata, '$.book[1:2].author'), '["Jinn","Kenobi"]', 'compact', 1);
393393
test_jsonlab('jsonpath of [*] and deep scan', @savejson, jsonpath(testdata, '$.book[*]..author'), '["Yoda","Jinn","Kenobi"]', 'compact', 1);

0 commit comments

Comments
 (0)