Skip to content

Commit

Permalink
support empty name, which is valid in JSON, fix #79
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Mar 14, 2022
1 parent 9ab040a commit 46bbfa9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 36 deletions.
4 changes: 4 additions & 0 deletions decodevarname.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
h2u=@hex2unicode;
newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${h2u($2)}');
else
if(isunpack && strcmp(name,'x0x0_'))
newname='';
return;
end
pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');
pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');
if(isempty(pos))
Expand Down
2 changes: 1 addition & 1 deletion loadbj.m
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@
[str, pos] = parse_name(inputstr, pos, varargin{:});
end
if isempty(str)
error_pos('Name of value at position %d cannot be empty', inputstr, pos);
str='x0x0_'; % empty name is valid in BJData/UBJSON, decodevarname('x0x0_') restores '\0'
end
if(nargout>2)
varargin{1}.jsonpath_=[origpath,'.',str];
Expand Down
4 changes: 2 additions & 2 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,8 @@
if cc ~= '}'
while 1
[str, pos, index_esc] = parseStr(inputstr, pos, esc, index_esc, varargin{:});
if isempty(str)
pos=error_pos('Name of value at position %d cannot be empty',inputstr,pos);
if isempty(str) && ~usemap
str='x0x0_'; % empty name is valid in JSON, decodevarname('x0x0_') restores '\0'
end
[pos, w1, w2]=parse_char(inputstr, pos, ':');
if(nargout>3)
Expand Down
14 changes: 3 additions & 11 deletions savebj.m
Original file line number Diff line number Diff line change
Expand Up @@ -477,27 +477,19 @@
else
om0=Omarker{1};
end
len=prod(dim);
forcearray= (len>1 || (varargin{1}.singletarray==1 && level>0));

if(~isempty(name))
if(forcearray)
txt=[N_(decodevarname(name,varargin{:}),varargin{:}) om0];
end
txt=[N_(decodevarname(name,varargin{:}),varargin{:}) om0];
else
if(forcearray)
txt=om0;
end
txt=om0;
end
for i=1:dim(1)
if(~isempty(names{i}))
txt=[txt obj2ubjson(names{i},val{i},...
level+(dim(1)>1),varargin{:})];
end
end
if(forcearray)
txt=[txt Omarker{2}];
end
txt=[txt Omarker{2}];

%%-------------------------------------------------------------------------
function txt=str2ubjson(name,item,level,varargin)
Expand Down
37 changes: 15 additions & 22 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,12 @@
return;
end

len=prod(dim);
forcearray= (len>1 || (varargin{1}.singletarray==1 && level>0));
ws=varargin{1}.whitespaces_;
padding0=repmat(ws.tab,1,level);
nl=ws.newline;

if(isempty(item))
if(~isempty(name))
if(isempty(item))
if(~isempty(name))
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":[]'};
else
txt={padding0, '[]'};
Expand All @@ -486,30 +484,25 @@
return;
end
if(~isempty(name))
if(forcearray)
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":{', nl};
end
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":{', nl};
else
if(forcearray)
txt={padding0, '{', nl};
end
txt={padding0, '{', nl};
end

for i=1:dim(1)
if(~isempty(names{i}))
txt{end+1}=obj2json(names{i},val{i},...
level+(dim(1)>1),varargin{:});
if(i<length(names))
txt{end+1}=',';
end
if(i<dim(1))
txt{end+1}=nl;
end
if(isempty(names{i}))
txt{end+1}=obj2json('x0x0_',val{i},level+(dim(1)>1),varargin{:});
else
txt{end+1}=obj2json(names{i},val{i},level+(dim(1)>1),varargin{:});
end
if(i<length(names))
txt{end+1}=',';
end
if(i<dim(1))
txt{end+1}=nl;
end
end
if(forcearray)
txt(end+1:end+3)={nl,padding0,'}'};
end
txt(end+1:end+3)={nl,padding0,'}'};
txt = sprintf('%s',txt{:});

%%-------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions test/run_jsonlab_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ function run_jsonlab_test(tests)
test_jsonlab('string type',@savejson,string(sprintf('jdata\n\b\ashall\tprevail')),'["jdata\n\b\ashall\tprevail"]','compact',1);
test_jsonlab('string array',@savejson,[string('jdata'),string('shall'),string('prevail')],'["jdata","shall","prevail"]','compact',1);
end
test_jsonlab('empty name',@savejson,loadjson('{"":""}'),'{"":""}','compact',1);
test_jsonlab('empty name with map',@savejson,loadjson('{"":""}','usemap',1),'{"":""}','compact',1);
test_jsonlab('row vector',@savejson,[1,2,3],'[1,2,3]');
test_jsonlab('column vector',@savejson,[1;2;3],'[[1],[2],[3]]','compact',1);
test_jsonlab('mixed array',@savejson,{'a',1,0.9},'["a",1,0.9]','compact',1);
Expand Down Expand Up @@ -182,6 +184,8 @@ function run_jsonlab_test(tests)
test_jsonlab('string type',@savebj,string(sprintf('jdata\n\b\ashall\tprevail')),sprintf('[SU<21>jdata\n\b\ashall\tprevail]'),'debug',1);
test_jsonlab('string array',@savebj,[string('jdata');string('shall');string('prevail')],'[[SU<5>jdataSU<5>shallSU<7>prevail]]','debug',1);
end
test_jsonlab('empty name',@savebj,loadbj(['{U' 0 'U' 2 '}']),'{U<0>U<2>}','debug',1);
test_jsonlab('empty name with map',@savebj,loadbj(['{U' 0 'U' 2 '}'],'usemap',1),'{U<0>U<2>}','debug',1);
test_jsonlab('row vector',@savebj,[1,2,3],'[$U#U<3><1><2><3>','debug',1);
test_jsonlab('column vector',@savebj,[1;2;3],'[$U#[$U#U<2><3><1><1><2><3>','debug',1);
test_jsonlab('mixed array',@savebj,{'a',1,0.9},'[CaU<1>D<0.9>]','debug',1);
Expand Down

0 comments on commit 46bbfa9

Please sign in to comment.