File tree Expand file tree Collapse file tree 3 files changed +29
-7
lines changed Expand file tree Collapse file tree 3 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -188,7 +188,9 @@ JSON. The detailed help info for the four functions can be found below:
188188 opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.
189189 opt.ParseStringArray [0|1]: if set to 0, loadjson converts "string arrays"
190190 (introduced in MATLAB R2016b) to char arrays; if set to 1,
191- loadjson skips this conversion.
191+ loadjson skips this conversion.
192+ opt.Encoding ['']: json file encoding. Support all encodings of
193+ fopen() function
192194
193195 output:
194196 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:
289291 compressed binary array data.
290292 opt.CompressArraySize [100|int]: only to compress an array if the total
291293 element count is larger than this number.
294+ opt.Encoding ['']: json file encoding. Support all encodings of
295+ fopen() function
296+
292297 opt can be replaced by a list of ('param',value) pairs. The param
293298 string is equivallent to a field in opt and is case sensitive.
294299 output:
Original file line number Diff line number Diff line change 5353% for output format, it is incompatible with all
5454% previous releases; if old output is desired,
5555% please set FormatVersion to 1.9 or earlier.
56+ % opt.Encoding ['']: json file encoding. Support all encodings of
57+ % fopen() function
5658%
5759% output:
5860% dat: a cell array, where {...} blocks are converted into cell arrays,
6971% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
7072%
7173
74+ opt= varargin2struct(varargin{: });
75+
7276 if (regexp(fname ,' ^\s*(?:\[.*\])|(?:\{.*\})\s*$' ,' once' ))
7377 string= fname ;
7478 elseif (exist(fname ,' file' ))
7579 try
76- string = fileread(fname );
80+ encoding = jsonopt(' Encoding' ,' ' ,opt );
81+ if (isempty(encoding ))
82+ string = fileread(fname );
83+ else
84+ fid = fopen(fname ,' r' ,' n' ,encoding );
85+ string = fread(fid ,' *char' )' ;
86+ fclose(fid );
87+ end
7788 catch
7889 try
7990 string = urlread([' file://' ,fname ]);
93104 esc = find(inputstr ==' "' | inputstr ==' \' ); % comparable to: regexp(inputstr, '["\\]');
94105 index_esc = 1 ;
95106
96- opt= varargin2struct(varargin{: });
97107 opt.arraytoken_=arraytoken ;
98108 opt.arraytokenidx_=arraytokenidx ;
99109
Original file line number Diff line number Diff line change 8888% for output format, it is incompatible with all
8989% previous releases; if old output is desired,
9090% please set FormatVersion to 1.9 or earlier.
91+ % opt.Encoding ['']: json file encoding. Support all encodings of
92+ % fopen() function
9193%
9294% opt can be replaced by a list of ('param',value) pairs. The param
9395% string is equivallent to a field in opt and is case sensitive.
198200filename= jsonopt(' FileName' ,' ' ,opt );
199201if (~isempty(filename ))
200202 if (jsonopt(' SaveBinary' ,0 ,opt )==1 )
201- fid = fopen(filename , ' wb' );
202- fwrite(fid ,json );
203+ fid = fopen(filename , ' wb' );
204+ fwrite(fid ,json );
203205 else
204- fid = fopen(filename , ' wt' );
205- fwrite(fid ,json ,' char' );
206+ encoding = jsonopt(' Encoding' ,' ' ,opt );
207+ if (isempty(encoding ))
208+ fid = fopen(filename ,' wt' );
209+ else
210+ fid = fopen(filename ,' wt' ,' n' ,encoding );
211+ end
212+ fwrite(fid ,json ,' char' );
206213 end
207214 fclose(fid );
208215end
You can’t perform that action at this time.
0 commit comments