Skip to content

Commit 7dd018b

Browse files
committed
add SingletCell option, add SingletArray to replace NoRowBracket
1 parent aa37e53 commit 7dd018b

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

examples/jsonlab_basictest.matlab

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
< M A T L A B >
3-
Copyright 1984-2007 The MathWorks, Inc.
4-
Version 7.4.0.287 (R2007a)
5-
January 29, 2007
2+
< M A T L A B (R) >
3+
Copyright 1984-2010 The MathWorks, Inc.
4+
Version 7.11.0.584 (R2010b) 64-bit (glnxa64)
5+
August 16, 2010
66

77

88
To get started, type one of these: helpwin, helpdesk, or demo.
@@ -529,14 +529,18 @@ ans =
529529
"lenny": 5.00,
530530
"squeeze": 6.00,
531531
"wheezy": 7.00
532-
},
532+
}
533+
],
534+
[
533535
{
534536
"Ubuntu": [
535537
"Kubuntu",
536538
"Xubuntu",
537539
"Lubuntu"
538540
]
539-
},
541+
}
542+
],
543+
[
540544
[10.04,10.10,11.04,11.10]
541545
]
542546
]
@@ -546,7 +550,7 @@ ans =
546550
>>
547551
json2data =
548552

549-
debian: {{1x3 cell}}
553+
debian: {{1x1 cell} {1x1 cell} [10.0400 10.1000 11.0400 11.1000]}
550554

551555
>> >>
552556
%=================================================
@@ -579,21 +583,25 @@ ans =
579583
3
580584
]
581585
],
582-
7
583-
],
584-
[
585586
[
586587
4,
587588
5
588589
],
589590
[
590-
8,
591-
9
591+
6
592592
]
593593
],
594594
[
595-
6,
596-
10
595+
[
596+
7
597+
],
598+
[
599+
8,
600+
9
601+
],
602+
[
603+
10
604+
]
597605
]
598606
]
599607
}
@@ -602,7 +610,7 @@ ans =
602610
>>
603611
json2data =
604612

605-
data2json: {{1x2 cell} [2x2 double] [6 10]}
613+
data2json: {{1x3 cell} {1x3 cell}}
606614

607615
>> >>
608616
%=================================================
@@ -660,4 +668,4 @@ json2data =
660668

661669
data2json: {{1x2 cell} {1x2 cell} {1x2 cell}}
662670

663-
>> >> >> >>
671+
>> >> >> >>

examples/jsonlab_selftest.matlab

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
< M A T L A B >
3-
Copyright 1984-2007 The MathWorks, Inc.
4-
Version 7.4.0.287 (R2007a)
5-
January 29, 2007
2+
< M A T L A B (R) >
3+
Copyright 1984-2010 The MathWorks, Inc.
4+
Version 7.11.0.584 (R2010b) 64-bit (glnxa64)
5+
August 16, 2010
66

77

88
To get started, type one of these: helpwin, helpdesk, or demo.
@@ -150,5 +150,5 @@ GlossEntry{UIDSUSGMLUSortAsSUSGMLU GlossTermSU$Standard Generalized Markup L
150150
CloseDoc()}]}}}}
151151
===============================================
152152
>> example4.json
153-
{Udata[{Usample{Urhoi}}{Usample{Urhoi}}[[$i#U
153+
{Udata[{Usample{Urhoi}}{Usample{Urhoi}}[[$i#U[$i#U[$i#U][SUPaperSUScissorsSUStone][CaSUb\SUc"SUd\"SUe"[SUf\"[SUg[\SUh[\"]]}
154154
>>

savejson.m

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@
4141
% parts, and also "_ArrayIsComplex_":1 is added.
4242
% opt.ParseLogical [0|1]: if this is set to 1, logical array elem
4343
% will use true/false rather than 1/0.
44-
% opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single
44+
% opt.SingletArray [0|1]: if this is set to 1, arrays with a single
4545
% numerical element will be shown without a square
4646
% bracket, unless it is the root object; if 0, square
4747
% brackets are forced for any numerical arrays.
48+
% opt.SingletCell [1|0]: if 1, always enclose a cell with "[]"
49+
% even it has only one element; if 0, brackets
50+
% are ignored when a cell has only 1 element.
4851
% opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson
4952
% will use the name of the passed obj variable as the
5053
% root object name; if obj is an expression and
@@ -97,11 +100,17 @@
97100
varname=inputname(2);
98101
end
99102
if(length(varargin)==1 && ischar(varargin{1}))
100-
opt=struct('FileName',varargin{1});
103+
opt=struct('filename',varargin{1});
101104
else
102105
opt=varargin2struct(varargin{:});
103106
end
104107
opt.IsOctave=exist('OCTAVE_VERSION','builtin');
108+
if(isfield(opt,'norowbracket'))
109+
warning('Option ''NoRowBracket'' is depreciated, please use ''SingletArray'' and set its value to not(NoRowBracket)');
110+
if(~isfield(opt,'singletarray'))
111+
opt.singletarray=not(opt.norowbracket);
112+
end
113+
end
105114
rootisarray=0;
106115
rootlevel=1;
107116
forceroot=jsonopt('ForceRootName',0,opt);
@@ -184,7 +193,8 @@
184193
padding0=repmat(ws.tab,1,level);
185194
padding2=repmat(ws.tab,1,level+1);
186195
nl=ws.newline;
187-
if(len>1)
196+
bracketlevel=~jsonopt('singletcell',1,varargin{:});
197+
if(len>bracketlevel)
188198
if(~isempty(name))
189199
txt=sprintf('%s"%s": [%s',padding0, checkname(name,varargin{:}),nl); name='';
190200
else
@@ -202,7 +212,7 @@
202212
txt=sprintf('%s%s[%s',txt,padding2,nl);
203213
end
204214
for j=1:dim(2)
205-
txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+(len>1),varargin{:}));
215+
txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+(len>bracketlevel),varargin{:}));
206216
if(j<dim(2))
207217
txt=sprintf('%s%s',txt,sprintf(',%s',nl));
208218
end
@@ -215,7 +225,7 @@
215225
end
216226
%if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
217227
end
218-
if(len>1)
228+
if(len>bracketlevel)
219229
txt=sprintf('%s%s%s]',txt,nl,padding0);
220230
end
221231

@@ -231,7 +241,7 @@
231241
dim=size(item);
232242
end
233243
len=numel(item);
234-
forcearray= (len>1 || (jsonopt('NoRowBracket',1,varargin{:})==0 && level>0));
244+
forcearray= (len>1 || (jsonopt('SingletArray',0,varargin{:})==1 && level>0));
235245
ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'));
236246
ws=jsonopt('whitespaces_',ws,varargin{:});
237247
padding0=repmat(ws.tab,1,level);
@@ -359,15 +369,15 @@
359369
padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl);
360370
end
361371
else
362-
if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)
372+
if(numel(item)==1 && jsonopt('SingletArray',0,varargin{:})==0 && level>0)
363373
numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']','');
364374
else
365375
numtxt=matdata2json(item,level+1,varargin{:});
366376
end
367377
if(isempty(name))
368378
txt=sprintf('%s%s',padding1,numtxt);
369379
else
370-
if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)
380+
if(numel(item)==1 && jsonopt('SingletArray',0,varargin{:})==0)
371381
txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt);
372382
else
373383
txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt);

saveubjson.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
% parts, and also "_ArrayIsComplex_":1 is added.
3838
% opt.ParseLogical [1|0]: if this is set to 1, logical array elem
3939
% will use true/false rather than 1/0.
40-
% opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single
40+
% opt.SingletArray [0|1]: if this is set to 1, arrays with a single
4141
% numerical element will be shown without a square
4242
% bracket, unless it is the root object; if 0, square
4343
% brackets are forced for any numerical arrays.
44+
% opt.SingletCell [1|0]: if 1, always enclose a cell with "[]"
45+
% even it has only one element; if 0, brackets
46+
% are ignored when a cell has only 1 element.
4447
% opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson
4548
% will use the name of the passed obj variable as the
4649
% root object name; if obj is an expression and
@@ -85,11 +88,17 @@
8588
varname=inputname(2);
8689
end
8790
if(length(varargin)==1 && ischar(varargin{1}))
88-
opt=struct('FileName',varargin{1});
91+
opt=struct('filename',varargin{1});
8992
else
9093
opt=varargin2struct(varargin{:});
9194
end
9295
opt.IsOctave=exist('OCTAVE_VERSION','builtin');
96+
if(isfield(opt,'norowbracket'))
97+
warning('Option ''NoRowBracket'' is depreciated, please use ''SingletArray'' and set its value to not(NoRowBracket)');
98+
if(~isfield(opt,'singletarray'))
99+
opt.singletarray=not(opt.norowbracket);
100+
end
101+
end
93102
rootisarray=0;
94103
rootlevel=1;
95104
forceroot=jsonopt('ForceRootName',0,opt);
@@ -149,8 +158,9 @@
149158
item=reshape(item,dim(1),numel(item)/dim(1));
150159
dim=size(item);
151160
end
161+
bracketlevel=~jsonopt('singletcell',1,varargin{:});
152162
len=numel(item); % let's handle 1D cell first
153-
if(len>1)
163+
if(len>bracketlevel)
154164
if(~isempty(name))
155165
txt=[N_(checkname(name,varargin{:})) '[']; name='';
156166
else
@@ -168,13 +178,13 @@
168178
txt=[txt '['];
169179
end
170180
for i=1:dim(1)
171-
txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];
181+
txt=[txt obj2ubjson(name,item{i,j},level+(len>bracketlevel),varargin{:})];
172182
end
173183
if(dim(1)>1)
174184
txt=[txt ']'];
175185
end
176186
end
177-
if(len>1)
187+
if(len>bracketlevel)
178188
txt=[txt ']'];
179189
end
180190

@@ -190,7 +200,7 @@
190200
dim=size(item);
191201
end
192202
len=numel(item);
193-
forcearray= (len>1 || (jsonopt('NoRowBracket',1,varargin{:})==0 && level>0));
203+
forcearray= (len>1 || (jsonopt('SingletArray',0,varargin{:})==1 && level>0));
194204

195205
if(~isempty(name))
196206
if(forcearray)
@@ -285,7 +295,7 @@
285295
if(isempty(name))
286296
txt=matdata2ubjson(item,level+1,varargin{:});
287297
else
288-
if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)
298+
if(numel(item)==1 && jsonopt('SingletArray',0,varargin{:})==0)
289299
numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']','');
290300
txt=[N_(checkname(name,varargin{:})) numtxt];
291301
else

0 commit comments

Comments
 (0)