Skip to content

Commit

Permalink
initial support for _DataLink_ of online/local file with JSONPath ref
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Mar 30, 2022
1 parent 2936461 commit 07c58f3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
30 changes: 16 additions & 14 deletions jdatadecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -461,28 +461,30 @@
end

%% handle data link
if(isfield(data,N_('_DataLink_')))
if(ischar(data.N('_DataLink_')))
datalink=data.N('_DataLink_');
[pat, ref]=regexp(datalink, '(^.+)(:($\d*\..*)*', 'match', 'tokens');
if(~isempty(pat) && ~isempty(ref))
[fpath, fname, fext]=fileparts(ref{1});
if(opt.maxlinklevel>0 && isfield(data,N_('_DataLink_')))
if(ischar(data.(N_('_DataLink_'))))
datalink=data.(N_('_DataLink_'));
ref=regexp(datalink, '^(?<proto>[a-zA-Z]+://)*(?<path>[^:]+)(?<delim>\:)*(?<jsonpath>\$\d*\..*)*', 'names');
if(~isempty(ref.path))
uripath=[ref.proto ref.path];
[fpath, fname, fext]=fileparts(uripath);
opt.maxlinklevel=opt.maxlinklevel-1;
switch(lower(fext))
case {'.json','.jnii','.jdt','.jdat','.jmsh','.jnirs'}
newdata=loadjson([fpath, filesep, fname], varargin{:});
newdata=loadjson(uripath, opt);
case {'.bjd' ,'.bnii','.jdb','.jbat','.bmsh','.bnirs', '.jamm'}
newdata=loadbj([fpath, filesep, fname], varargin{:});
newdata=loadbj(uripath, opt);
case {'.ubj'}
newdata=loadubjson([fpath, filesep, fname], varargin{:});
newdata=loadubjson(uripath, opt);
case {'.msgpack'}
newdata=loadmsgpack([fpath, filesep, fname], varargin{:});
newdata=loadmsgpack(uripath, opt);
case {'.h5','.hdf5','.snirf'} % this requires EasyH5 toolbox
newdata=loadh5([fpath, filesep, fname], varargin{:});
newdata=loadh5(uripath, opt);
otherwise
warning('datalink file is not supported');
warning('_DataLink_ file type is not supported');
end
if(length(ref)>=2)
newdata=getfromjsonpath(newdata,ref{2}{1});
if(~isempty(ref.jsonpath))
newdata=getfromjsonpath(newdata,ref.jsonpath);
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@
end
catch
try
string = urlread(['file://',fname]);
string = urlread(fname);
catch
string = urlread(['file://',fullfile(pwd,fname)]);
end
end
elseif(regexpi(fname,'^\s*(http|https|ftp|file)://'))
string = urlread(fname);
else
error_pos('input file does not exist');
end
Expand Down
4 changes: 4 additions & 0 deletions test/run_jsonlab_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ function run_jsonlab_test(tests)
test_jsonlab('output float format',@savejson,pi,'[3.142]','FloatFormat','%5.3f');
test_jsonlab('remove singlet array',@savejson,{struct('a',1),5},'[{"a":1},5]','compact',1,'SingletArray',0);
test_jsonlab('keep singlet array',@savejson,{struct('a',1),5},'[[{"a":[1]}],[5]]','compact',1,'SingletArray',1);
test_jsonlab('test no datalink',@savejson,loadjson(savejson('a',struct(encodevarname('_DataLink_'),...
'../examples/example2.json:$.glossary.title'))),'{"a":[{"_DataLink_":"..\/examples\/example2.json:$.glossary.title"}]}','compact',1,'SingletArray',1);
test_jsonlab('test maxlinklevel',@savejson,loadjson(savejson('a',struct(encodevarname('_DataLink_'),...
'../examples/example2.json:$.glossary.title')),'maxlinklevel',1),'{"a":"example glossary"}','compact',1,'SingletArray',1);
end


Expand Down

0 comments on commit 07c58f3

Please sign in to comment.