Skip to content

Commit 417419b

Browse files
author
fangq
committed
polish coding styles based on matlab hints
git-svn-id: http://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab@492 786e58fb-9377-0410-9ff7-e4ac0ac0635c
1 parent e9eb724 commit 417419b

File tree

4 files changed

+141
-73
lines changed

4 files changed

+141
-73
lines changed

loadjson.m

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
end
130130
parse_char(':');
131131
val = parse_value(varargin{:});
132-
eval( sprintf( 'object.%s = val;', valid_field(str) ) );
132+
object.(valid_field(str))=val;
133133
if next_char == '}'
134134
break;
135135
end
@@ -156,7 +156,7 @@
156156

157157
if next_char ~= ']'
158158
if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))
159-
[endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);
159+
[endpos, e1l, e1r]=matching_bracket(inStr,pos);
160160
arraystr=['[' inStr(pos:endpos)];
161161
arraystr=regexprep(arraystr,'"_NaN_"','NaN');
162162
arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf');
@@ -228,7 +228,7 @@
228228
object=cell2mat(object')';
229229
if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)
230230
object=oldobj;
231-
elseif(size(object,1)>1 && ndims(object)==2)
231+
elseif(size(object,1)>1 && ismatrix(object))
232232
object=object';
233233
end
234234
catch
@@ -292,7 +292,8 @@ function parse_char(c)
292292
str = [str inStr(pos:esc(index_esc)-1)];
293293
pos = esc(index_esc);
294294
end
295-
nstr = length(str); switch inStr(pos)
295+
nstr = length(str);
296+
switch inStr(pos)
296297
case '"'
297298
pos = pos + 1;
298299
if(~isempty(str))
@@ -325,7 +326,8 @@ function parse_char(c)
325326
pos = pos + 5;
326327
end
327328
otherwise % should never happen
328-
str(nstr+1) = inStr(pos), keyboard
329+
str(nstr+1) = inStr(pos);
330+
keyboard;
329331
pos = pos + 1;
330332
end
331333
end
@@ -338,7 +340,7 @@ function parse_char(c)
338340
currstr=inStr(pos:min(pos+30,end));
339341
if(isoct~=0)
340342
numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end');
341-
[num, one] = sscanf(currstr, '%f', 1);
343+
[num] = sscanf(currstr, '%f', 1);
342344
delta=numstr+1;
343345
else
344346
[num, one, err, delta] = sscanf(currstr, '%f', 1);
@@ -418,12 +420,16 @@ function error_pos(msg)
418420
str=sprintf('x0x%X_%s',char(str(1)),str(2:end));
419421
end
420422
end
421-
if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end
423+
if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' )))
424+
return;
425+
end
422426
if(~isoct)
423427
str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');
424428
else
425429
pos=regexp(str,'[^0-9A-Za-z_]');
426-
if(isempty(pos)) return; end
430+
if(isempty(pos))
431+
return;
432+
end
427433
str0=str;
428434
pos0=[0 pos(:)' length(str)];
429435
str='';
@@ -465,14 +471,18 @@ function error_pos(msg)
465471
c=tokens(pos);
466472
if(c==']')
467473
level=level-1;
468-
if(isempty(e1r)) e1r=bpos(pos); end
474+
if(isempty(e1r))
475+
e1r=bpos(pos);
476+
end
469477
if(level==0)
470478
endpos=bpos(pos);
471479
return
472480
end
473481
end
474482
if(c=='[')
475-
if(isempty(e1l)) e1l=bpos(pos); end
483+
if(isempty(e1l))
484+
e1l=bpos(pos);
485+
end
476486
level=level+1;
477487
maxlevel=max(maxlevel,level);
478488
end

loadubjson.m

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,6 @@
9494
data=data{1};
9595
end
9696

97-
%%
98-
function newdata=parse_collection(id,data,obj)
99-
100-
if(jsoncount>0 && exist('data','var'))
101-
if(~iscell(data))
102-
newdata=cell(1);
103-
newdata{1}=data;
104-
data=newdata;
105-
end
106-
end
107-
10897
%%-------------------------------------------------------------------------
10998
function object = parse_object(varargin)
11099
parse_char('{');
@@ -133,7 +122,7 @@
133122
%parse_char(':');
134123
val = parse_value(varargin{:});
135124
num=num+1;
136-
eval( sprintf( 'object.%s = val;', valid_field(str) ) );
125+
object.(valid_field(str))=val;
137126
if next_char == '}' || (count>=0 && num>=count)
138127
break;
139128
end
@@ -181,7 +170,7 @@
181170

182171

183172
function object = parse_array(varargin) % JSON array is written in row-major order
184-
global pos inStr isoct
173+
global pos inStr
185174
parse_char('[');
186175
object = cell(0, 1);
187176
dim=[];
@@ -234,7 +223,7 @@
234223
object=cell2mat(object')';
235224
if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)
236225
object=oldobj;
237-
elseif(size(object,1)>1 && ndims(object)==2)
226+
elseif(size(object,1)>1 && ismatrix(object))
238227
object=object';
239228
end
240229
catch
@@ -334,7 +323,7 @@ function parse_char(c)
334323
%%-------------------------------------------------------------------------
335324

336325
function val = parse_value(varargin)
337-
global pos inStr len
326+
global pos inStr
338327

339328
switch(inStr(pos))
340329
case {'S','C','H'}
@@ -391,12 +380,16 @@ function error_pos(msg)
391380
str=sprintf('x0x%X_%s',char(str(1)),str(2:end));
392381
end
393382
end
394-
if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end
383+
if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' )))
384+
return;
385+
end
395386
if(~isoct)
396387
str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');
397388
else
398389
pos=regexp(str,'[^0-9A-Za-z_]');
399-
if(isempty(pos)) return; end
390+
if(isempty(pos))
391+
return;
392+
end
400393
str0=str;
401394
pos0=[0 pos(:)' length(str)];
402395
str='';
@@ -438,14 +431,18 @@ function error_pos(msg)
438431
c=tokens(pos);
439432
if(c==']')
440433
level=level-1;
441-
if(isempty(e1r)) e1r=bpos(pos); end
434+
if(isempty(e1r))
435+
e1r=bpos(pos);
436+
end
442437
if(level==0)
443438
endpos=bpos(pos);
444439
return
445440
end
446441
end
447442
if(c=='[')
448-
if(isempty(e1l)) e1l=bpos(pos); end
443+
if(isempty(e1l))
444+
e1l=bpos(pos);
445+
end
449446
level=level+1;
450447
maxlevel=max(maxlevel,level);
451448
end

savejson.m

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,26 @@
194194
end
195195
end
196196
for j=1:dim(2)
197-
if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end
197+
if(dim(1)>1)
198+
txt=sprintf('%s%s[%s',txt,padding2,nl);
199+
end
198200
for i=1:dim(1)
199201
txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+(len>1),varargin{:}));
200-
if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
202+
if(i<dim(1))
203+
txt=sprintf('%s%s',txt,sprintf(',%s',nl));
204+
end
205+
end
206+
if(dim(1)>1)
207+
txt=sprintf('%s%s%s]',txt,nl,padding2);
208+
end
209+
if(j<dim(2))
210+
txt=sprintf('%s%s',txt,sprintf(',%s',nl));
201211
end
202-
if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end
203-
if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
204212
%if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
205213
end
206-
if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end
214+
if(len>1)
215+
txt=sprintf('%s%s%s]',txt,nl,padding0);
216+
end
207217

208218
%%-------------------------------------------------------------------------
209219
function txt=struct2json(name,item,level,varargin)
@@ -234,12 +244,18 @@
234244
return;
235245
end
236246
if(~isempty(name))
237-
if(forcearray) txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl); end
247+
if(forcearray)
248+
txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl);
249+
end
238250
else
239-
if(forcearray) txt=sprintf('%s[%s',padding0,nl); end
251+
if(forcearray)
252+
txt=sprintf('%s[%s',padding0,nl);
253+
end
240254
end
241255
for j=1:dim(2)
242-
if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end
256+
if(dim(1)>1)
257+
txt=sprintf('%s%s[%s',txt,padding2,nl);
258+
end
243259
for i=1:dim(1)
244260
names = fieldnames(item(i,j));
245261
if(~isempty(name) && len==1 && ~forcearray)
@@ -249,19 +265,29 @@
249265
end
250266
if(~isempty(names))
251267
for e=1:length(names)
252-
txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...
253-
names{e}),level+(dim(1)>1)+1+forcearray,varargin{:}));
254-
if(e<length(names)) txt=sprintf('%s%s',txt,','); end
268+
txt=sprintf('%s%s',txt,obj2json(names{e},item(i,j).(names{e}),...
269+
level+(dim(1)>1)+1+forcearray,varargin{:}));
270+
if(e<length(names))
271+
txt=sprintf('%s%s',txt,',');
272+
end
255273
txt=sprintf('%s%s',txt,nl);
256274
end
257275
end
258276
txt=sprintf('%s%s}',txt,padding1);
259-
if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
277+
if(i<dim(1))
278+
txt=sprintf('%s%s',txt,sprintf(',%s',nl));
279+
end
260280
end
261-
if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end
262-
if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
281+
if(dim(1)>1)
282+
txt=sprintf('%s%s%s]',txt,nl,padding2);
283+
end
284+
if(j<dim(2))
285+
txt=sprintf('%s%s',txt,sprintf(',%s',nl));
286+
end
287+
end
288+
if(forcearray)
289+
txt=sprintf('%s%s%s]',txt,nl,padding0);
263290
end
264-
if(forcearray) txt=sprintf('%s%s%s]',txt,nl,padding0); end
265291

266292
%%-------------------------------------------------------------------------
267293
function txt=str2json(name,item,level,varargin)
@@ -279,24 +305,33 @@
279305
sep=ws.sep;
280306

281307
if(~isempty(name))
282-
if(len>1) txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl); end
308+
if(len>1)
309+
txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl);
310+
end
283311
else
284-
if(len>1) txt=sprintf('%s[%s',padding1,nl); end
312+
if(len>1)
313+
txt=sprintf('%s[%s',padding1,nl);
314+
end
285315
end
286-
isoct=jsonopt('IsOctave',0,varargin{:});
287316
for e=1:len
288317
val=escapejsonstring(item(e,:));
289318
if(len==1)
290319
obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"'];
291-
if(isempty(name)) obj=['"',val,'"']; end
320+
if(isempty(name))
321+
obj=['"',val,'"'];
322+
end
292323
txt=sprintf('%s%s%s%s',txt,padding1,obj);
293324
else
294325
txt=sprintf('%s%s%s%s',txt,padding0,['"',val,'"']);
295326
end
296-
if(e==len) sep=''; end
327+
if(e==len)
328+
sep='';
329+
end
297330
txt=sprintf('%s%s',txt,sep);
298331
end
299-
if(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end
332+
if(len>1)
333+
txt=sprintf('%s%s%s%s',txt,nl,padding1,']');
334+
end
300335

301336
%%-------------------------------------------------------------------------
302337
function txt=mat2json(name,item,level,varargin)
@@ -441,7 +476,9 @@
441476
else
442477
pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');
443478
pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');
444-
if(isempty(pos)) return; end
479+
if(isempty(pos))
480+
return;
481+
end
445482
str0=name;
446483
pos0=[0 pend(:)' length(name)];
447484
newname='';
@@ -460,7 +497,9 @@
460497
isoct=exist('OCTAVE_VERSION','builtin');
461498
if(isoct)
462499
vv=sscanf(OCTAVE_VERSION,'%f');
463-
if(vv(1)>=3.8) isoct=0; end
500+
if(vv(1)>=3.8)
501+
isoct=0;
502+
end
464503
end
465504
if(isoct)
466505
escapechars={'\\','\"','\/','\a','\f','\n','\r','\t','\v'};

0 commit comments

Comments
 (0)