Skip to content

Commit 747c99b

Browse files
committed
fix string indentation, add option EmptyArrayAsNull, fix #91
1 parent cf57326 commit 747c99b

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

savejson.m

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
% SingletCell [1|0]: if 1, always enclose a cell with "[]"
5454
% even it has only one element; if 0, brackets
5555
% are ignored when a cell has only 1 element.
56+
% EmptyArrayAsNull [0|1]: if set to 1, convert an empty array to
57+
% JSON null object; empty cells remain mapped to []
5658
% ForceRootName [0|1]: when set to 1 and rootname is empty, savejson
5759
% will use the name of the passed obj variable as the
5860
% root object name; if obj is an expression and
@@ -157,6 +159,7 @@
157159
opt.arraytostruct = jsonopt('ArrayToStruct', 0, opt);
158160
opt.parselogical = jsonopt('ParseLogical', 0, opt);
159161
opt.arrayindent = jsonopt('ArrayIndent', 1, opt);
162+
opt.emptyarrayasnull = jsonopt('EmptyArrayAsNull', 0, opt);
160163
opt.inf = jsonopt('Inf', '"$1_Inf_"', opt);
161164
opt.nan = jsonopt('NaN', '"_NaN_"', opt);
162165
opt.num2cell_ = 0;
@@ -313,10 +316,8 @@
313316
if (~iscell(item) && ~isa(item, 'string'))
314317
error('input is not a cell or string array');
315318
end
316-
if (isa(item, 'string'))
317-
level = level - 1;
318-
end
319319
isnum2cell = varargin{1}.num2cell_;
320+
320321
if (isnum2cell)
321322
item = squeeze(item);
322323
if (~isvector(item))
@@ -325,10 +326,6 @@
325326
end
326327

327328
dim = size(item);
328-
% if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now
329-
% item=reshape(item,dim(1),numel(item)/dim(1));
330-
% dim=size(item);
331-
% end
332329
len = numel(item);
333330
ws = varargin{1}.whitespaces_;
334331
padding0 = repmat(ws.tab, 1, level);
@@ -741,7 +738,11 @@
741738
end
742739

743740
if (isempty(mat))
744-
txt = '[]';
741+
if(varargin{1}.emptyarrayasnull)
742+
txt='null';
743+
else
744+
txt = '[]';
745+
end
745746
return
746747
end
747748
if (isinteger(mat))

test/run_jsonlab_test.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ function run_jsonlab_test(tests)
157157
test_jsonlab('output float format',@savejson,pi,'[3.142]','FloatFormat','%5.3f');
158158
test_jsonlab('remove singlet array',@savejson,{struct('a',1),5},'[{"a":1},5]','compact',1,'SingletArray',0);
159159
test_jsonlab('keep singlet array',@savejson,{struct('a',1),5},'[[{"a":[1]}],[5]]','compact',1,'SingletArray',1);
160+
test_jsonlab('make null object roundtrip',@savejson,loadjson('{"a":null}'),'{"a":null}','EmptyArrayAsNull',1,'compact',1);
160161
test_jsonlab('test no datalink',@savejson,loadjson(savejson('a',struct(encodevarname('_DataLink_'),...
161162
'../examples/example2.json:$.glossary.title'))),'{"a":[{"_DataLink_":"../examples/example2.json:$.glossary.title"}]}','compact',1,'SingletArray',1);
162163
test_jsonlab('test maxlinklevel',@savejson,loadjson(savejson('a',struct(encodevarname('_DataLink_'),...

0 commit comments

Comments
 (0)