Skip to content

Commit 9d84425

Browse files
committed
Merge branch 'master' of https://github.com/fangq/jsonlab
2 parents cdb218d + 6f0e740 commit 9d84425

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

README.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff 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:

loadjson.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
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,
@@ -69,11 +71,20 @@
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]);
@@ -93,7 +104,6 @@
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

savejson.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
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.
@@ -198,11 +200,16 @@
198200
filename=jsonopt('FileName','',opt);
199201
if(~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);
208215
end

0 commit comments

Comments
 (0)