From 6c44724662d77bc6ea6bc2aee318e43b3a42469a Mon Sep 17 00:00:00 2001 From: Pavel Roslovets Date: Thu, 18 Jul 2019 15:02:01 +0300 Subject: [PATCH 1/2] Add Encoding option to loadjson and savejson --- README.txt | 7 ++++++- loadjson.m | 14 ++++++++++++-- savejson.m | 9 ++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) 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..34aef76 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. @@ -201,7 +203,12 @@ fid = fopen(filename, 'wb'); fwrite(fid,json); else - fid = fopen(filename, 'wt'); + 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); From b21a6c297019ceaf50939e696ed3507dd15e44f2 Mon Sep 17 00:00:00 2001 From: Pavel Roslovets Date: Sun, 21 Jul 2019 16:33:38 +0300 Subject: [PATCH 2/2] Fix identation --- savejson.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/savejson.m b/savejson.m index 34aef76..ef88140 100644 --- a/savejson.m +++ b/savejson.m @@ -200,16 +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 - encoding = jsonopt('Encoding','',opt); + encoding = jsonopt('Encoding','',opt); if(isempty(encoding)) fid = fopen(filename,'wt'); else fid = fopen(filename,'wt','n',encoding); end - fwrite(fid,json,'char'); + fwrite(fid,json,'char'); end fclose(fid); end