Skip to content

Commit

Permalink
upgrade jsonlab to 2.9.8-git20220210
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Feb 10, 2022
1 parent a7673f5 commit f498ca2
Show file tree
Hide file tree
Showing 20 changed files with 537 additions and 174 deletions.
6 changes: 5 additions & 1 deletion encodevarname.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
%

if(~isvarname(str(1)))
str=sprintf('x0x%X_%s',char(str(1))+0,str(2:end));
if(exist('unicode2native','builtin'))
str=sprintf('x0x%s_%s',sprintf('%X',unicode2native(str(1))),str(2:end));
else
str=sprintf('x0x%X_%s',char(str(1))+0,str(2:end));
end
end
if(isvarname(str))
return;
Expand Down
2 changes: 1 addition & 1 deletion img2mesh.m
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ function miLoadVol_Callback(hObject, eventdata, handles)
nodedata.vol=loadmc2(fullfile(path,file),eval(res{1}),res{2});
nodetype.hasvol=1;
elseif(regexp(file,'\.[Uu][Bb][Jj]$'))
nodedata=loadubjson(fullfile(path,file));
nodedata=loadbj(fullfile(path,file));
if(isstruct(nodedata) && isfield(nodedata,'vol'))
nodetype.hasvol=1;
end
Expand Down
4 changes: 2 additions & 2 deletions jdatadecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
if(isfield(data,N_('_ArrayZipType_')))
zipmethod=data(j).(N_('_ArrayZipType_'));
end
if(~isempty(strmatch(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc'})))
if(ismember(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc'}))
decompfun=str2func([zipmethod 'decode']);
arraytype=data(j).(N_('_ArrayType_'));
chartype=0;
Expand Down Expand Up @@ -441,7 +441,7 @@
end

%% handle bytestream and arbitrary matlab objects
if(isfield(data,N_('_ByteStream_')) && isfield(data,N_('_DataInfo_'))==2)
if(isfield(data,N_('_ByteStream_')) && isfield(data,N_('_DataInfo_')))
newdata=cell(len,1);
for j=1:len
if(isfield(data(j).(N_('_DataInfo_')),'MATLABObjectClass'))
Expand Down
2 changes: 1 addition & 1 deletion jload.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
% parse the json file and then decode the output by
% jdatadecode; input file must have a suffix of .jdt
%
% all options for loadubjson/loadjson (depends on file suffix)
% all options for loadbj/loadjson (depends on file suffix)
% can be used to adjust the parsing options
%
% output:
Expand Down
5 changes: 3 additions & 2 deletions jsave.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
% fname: (optional) output file name; if not given, save to 'jamdata.jamm'
% if fname has a '.json' or '.jdt' suffix, a text-based
% JSON/JData file will be created (slow); if the suffix is '.jamm' or
% '.jdb', a Binary JData (https://github.com/fangq/bjdata/) file will be created.
% '.jdb', a Binary JData (https://github.com/NeuroJSON/bjdata/) file will be created.
% opt: (optional) a struct to store parsing options, opt can be replaced by
% a list of ('param',value) pairs - the param string is equivallent
% to a field in opt. opt can have the following
Expand Down Expand Up @@ -65,7 +65,8 @@

metadata=struct('CreateDate',datestr(now,29),...
'CreateTime',datestr(now,'hh:mm:ss'),...
'OriginalName',filename);
'OriginalName',filename,...
'BJDataVersion','v1_draft-2');

vers=ver('MATLAB');
if(isempty(vers))
Expand Down
70 changes: 70 additions & 0 deletions jsonget.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function json=jsonget(fname,mmap,varargin)
%
% json=jsonget(fname,mmap,'$.jsonpath1','$.jsonpath2',...)
%
% Fast reading of JSON data records using memory-map (mmap) returned by
% loadjson and JSONPath-like keys
%
% authors:Qianqian Fang (q.fang <at> neu.edu)
% initially created on 2022/02/02
%
% input:
% fname: a JSON/BJData/UBJSON string or stream, or a file name
% mmap: memory-map returned by loadjson/loadbj of the same data
% important: mmap must be produced from the same file/string,
% otherwise calling this function may cause data corruption
% '$.jsonpath1,2,3,...': a series of strings in the form of JSONPath
% as the key to each of the record to be retrieved
%
% output:
% json: a cell array, made of elements {'$.jsonpath_i',json_string_i}
%
% examples:
% str='[[1,2],"a",{"c":2}]{"k":"test"}';
% [dat, mmap]=loadjson(str);
% savejson('',dat,'filename','mydata.json','compact',1);
% json=jsonget(str,mmap,'$.[0].[*]','$.[2].c')
% json=jsonget('mydata.json',mmap,'$.[0].[*]','$.[2].c')
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

if(regexp(fname,'^\s*(?:\[.*\])|(?:\{.*\})\s*$','once'))
inputstr=fname;
elseif(isoctavemesh)
if(exist(fname,'file'))
try
fid = fopen(fname,'rb');
inputstr = fread(fid,'char',inf)';
fclose(fid);
catch
try
inputstr = urlread(['file://',fname]);
catch
inputstr = urlread(['file://',fullfile(pwd,fname)]);
end
end
end
end

mmap=[mmap{:}];
keylist=mmap(1:2:end);

json={};
for i=1:length(varargin)
if(regexp(varargin{i},'^\$'))
[tf,loc]=ismember(varargin{i},keylist);
if(tf)
rec={'uint8',[1,mmap{loc*2}(2)], 'x'};
if(exist('inputstr','var'))
json{end+1}={varargin{i}, inputstr(mmap{loc*2}(1):mmap{loc*2}(1)+mmap{loc*2}(2)-1)};
else
fmap=memmapfile(fname,'writable',false, 'offset',mmap{loc*2}(1),'format', rec);
json{end+1}={varargin{i}, char(fmap.Data(1).x)};
end
end
end
end
89 changes: 89 additions & 0 deletions jsonset.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
function json=jsonset(fname,mmap,varargin)
%
% json=jsonset(fname,mmap,'$.jsonpath1',newval1,'$.jsonpath2','newval2',...)
%
% Fast writing of JSON data records to stream or disk using memory-map
% (mmap) returned by loadjson and JSONPath-like keys
%
% authors:Qianqian Fang (q.fang <at> neu.edu)
% initially created on 2022/02/02
%
% input:
% fname: a JSON/BJData/UBJSON string or stream, or a file name
% mmap: memory-map returned by loadjson/loadbj of the same data
% important: mmap must be produced from the same file/string,
% otherwise calling this function may cause data corruption
% '$.jsonpath1,2,3,...': a series of strings in the form of JSONPath
% as the key to each of the record to be written
%
% output:
% json: the modified JSON string or, in the case fname is a filename,
% the cell string made of jsonpaths that are successfully
% written
%
% examples:
% str='[[1,2],"a",{"c":2}]{"k":"test"}';
% [dat, mmap]=loadjson(str);
% savejson('',dat,'filename','mydata.json','compact',1);
% json=jsonset(str,mmap,'$.[2].c','5')
% json=jsonset('mydata.json',mmap,'$.[2].c','"c":5')
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

if(regexp(fname,'^\s*(?:\[.*\])|(?:\{.*\})\s*$','once'))
inputstr=fname;
else
fid=fopen(fname,'wb');
end

mmap=[mmap{:}];
keylist=mmap(1:2:end);

opt=struct;
for i=1:2:length(varargin)
if(isempty(regexp(varargin{i},'^\$', 'once')))
opt.(encodevarname(varargin{i}))=varargin{i+1};
end
end

json={};
for i=1:2:length(varargin)
if(regexp(varargin{i},'^\$'))
[tf,loc]=ismember(varargin{i},keylist);
if(tf)
rec={'uint8',[1,mmap{loc*2}(2)], 'x'};
if(ischar(varargin{i+1}))
val=varargin{i+1};
else
val=savejson('',varargin{i+1},'compact',1);
end
if(length(val)<=rec{1,2}(2))
val=[val repmat(' ',[1,rec{1,2}(2)-length(val)])];
if(exist('inputstr','var'))
inputstr(mmap{loc*2}(1):mmap{loc*2}(1)+mmap{loc*2}(2)-1)=val;
else
if(exist('memmapfile','file'))
fmap=memmapfile(fname,'writable',true,'offset',mmap{loc*2}(1),'format', rec);
fmap.x=val;
else
fseek(fid,mmap{loc*2}(1)-1,'bof');
fwrite(fid,val);
end
json{end+1}={varargin{i},val};
end
end
end
end
end

if(exist('fid','var') && fid>=0)
fclose(fid);
end

if(exist('inputstr','var'))
json=inputstr;
end
Loading

0 comments on commit f498ca2

Please sign in to comment.