Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ JSON. The detailed help info for the four functions can be found below:
opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.
opt.ParseStringArray [0|1]: if set to 0, loadjson converts "string arrays"
(introduced in MATLAB R2016b) to char arrays; if set to 1,
loadjson skips this conversion.
loadjson skips this conversion.
opt.Encoding ['']: json file encoding. Support all encodings of
fopen() function

output:
dat: a cell array, where {...} blocks are converted into cell arrays,
Expand Down Expand Up @@ -289,6 +291,9 @@ JSON. The detailed help info for the four functions can be found below:
compressed binary array data.
opt.CompressArraySize [100|int]: only to compress an array if the total
element count is larger than this number.
opt.Encoding ['']: json file encoding. Support all encodings of
fopen() function

opt can be replaced by a list of ('param',value) pairs. The param
string is equivallent to a field in opt and is case sensitive.
output:
Expand Down
14 changes: 12 additions & 2 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
% for output format, it is incompatible with all
% previous releases; if old output is desired,
% please set FormatVersion to 1.9 or earlier.
% opt.Encoding ['']: json file encoding. Support all encodings of
% fopen() function
%
% output:
% dat: a cell array, where {...} blocks are converted into cell arrays,
Expand All @@ -69,11 +71,20 @@
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

opt=varargin2struct(varargin{:});

if(regexp(fname,'^\s*(?:\[.*\])|(?:\{.*\})\s*$','once'))
string=fname;
elseif(exist(fname,'file'))
try
string = fileread(fname);
encoding = jsonopt('Encoding','',opt);
if(isempty(encoding))
string = fileread(fname);
else
fid = fopen(fname,'r','n',encoding);
string = fread(fid,'*char')';
fclose(fid);
end
catch
try
string = urlread(['file://',fname]);
Expand All @@ -93,7 +104,6 @@
esc = find(inputstr=='"' | inputstr=='\' ); % comparable to: regexp(inputstr, '["\\]');
index_esc = 1;

opt=varargin2struct(varargin{:});
opt.arraytoken_=arraytoken;
opt.arraytokenidx_=arraytokenidx;

Expand Down
15 changes: 11 additions & 4 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
% for output format, it is incompatible with all
% previous releases; if old output is desired,
% please set FormatVersion to 1.9 or earlier.
% opt.Encoding ['']: json file encoding. Support all encodings of
% fopen() function
%
% opt can be replaced by a list of ('param',value) pairs. The param
% string is equivallent to a field in opt and is case sensitive.
Expand Down Expand Up @@ -198,11 +200,16 @@
filename=jsonopt('FileName','',opt);
if(~isempty(filename))
if(jsonopt('SaveBinary',0,opt)==1)
fid = fopen(filename, 'wb');
fwrite(fid,json);
fid = fopen(filename, 'wb');
fwrite(fid,json);
else
fid = fopen(filename, 'wt');
fwrite(fid,json,'char');
encoding = jsonopt('Encoding','',opt);
if(isempty(encoding))
fid = fopen(filename,'wt');
else
fid = fopen(filename,'wt','n',encoding);
end
fwrite(fid,json,'char');
end
fclose(fid);
end
Expand Down