diff --git a/README.txt b/README.txt index a83ad6b..0b1731f 100644 --- a/README.txt +++ b/README.txt @@ -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, @@ -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: diff --git a/loadjson.m b/loadjson.m index 29d0617..ff8ce7b 100644 --- a/loadjson.m +++ b/loadjson.m @@ -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, @@ -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]); @@ -93,7 +104,6 @@ esc = find(inputstr=='"' | inputstr=='\' ); % comparable to: regexp(inputstr, '["\\]'); index_esc = 1; - opt=varargin2struct(varargin{:}); opt.arraytoken_=arraytoken; opt.arraytokenidx_=arraytokenidx; diff --git a/savejson.m b/savejson.m index a556698..ef88140 100644 --- a/savejson.m +++ b/savejson.m @@ -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. @@ -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