Skip to content

Commit

Permalink
add UseMap option to avoid key name conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Qianqian Fang committed Feb 10, 2020
1 parent 05eaed9 commit 6984111
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions loadjson.m
Expand Up @@ -42,6 +42,8 @@
% arrays; setting to 3 will return to a 2D cell
% array of 1D vectors; setting to 4 will return a
% 3D cell array.
% UseMap [0|1]: if set to 1, loadjson uses a containers.Map to
% store map objects; otherwise use a struct object
% ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.
% ParseStringArray [0|1]: if set to 0, loadjson converts "string arrays"
% (introduced in MATLAB R2016b) to char arrays; if set to 1,
Expand Down Expand Up @@ -410,7 +412,12 @@
%%-------------------------------------------------------------------------
function [object, pos, index_esc] = parse_object(inputstr, pos, esc, index_esc, varargin)
pos=parse_char(inputstr, pos, '{');
object = [];
usemap=jsonopt('UseMap',0,varargin{:});
if(usemap)
object = containers.Map();
else
object = [];
end
[cc,pos]=next_char(inputstr,pos);
if cc ~= '}'
while 1
Expand All @@ -420,7 +427,11 @@
end
pos=parse_char(inputstr, pos, ':');
[val, pos,index_esc] = parse_value(inputstr, pos, esc, index_esc, varargin{:});
object.(encodevarname(str,varargin{:}))=val;
if(usemap)
object(str)=val;
else
object.(encodevarname(str,varargin{:}))=val;
end
[cc,pos]=next_char(inputstr,pos);
if cc == '}'
break;
Expand Down
15 changes: 13 additions & 2 deletions loadubjson.m
Expand Up @@ -29,6 +29,8 @@
% the "name" tag is treated as a string. To load
% these UBJSON data, you need to manually set this
% flag to 1.
% UseMap [0|1]: if set to 1, loadjson uses a containers.Map to
% store map objects; otherwise use a struct object
% FormatVersion [2|float]: set the JSONLab format version; since
% v2.0, JSONLab uses JData specification Draft 1
% for output format, it is incompatible with all
Expand Down Expand Up @@ -304,7 +306,12 @@
%%-------------------------------------------------------------------------
function [object, pos] = parse_object(inputstr, pos, varargin)
pos=parse_char(inputstr,pos,'{');
object = [];
usemap=jsonopt('UseMap',0,varargin{:});
if(usemap)
object = containers.Map();
else
object = [];
end
type='';
count=-1;
[cc, pos]=next_char(inputstr,pos);
Expand Down Expand Up @@ -332,7 +339,11 @@
end
[val, pos] = parse_value(inputstr, pos, varargin{:});
num=num+1;
object.(encodevarname(str,varargin{:}))=val;
if(usemap)
object(str)=val;
else
object.(encodevarname(str,varargin{:}))=val;
end
[cc, pos]=next_char(inputstr,pos);
if cc == '}' || (count>=0 && num>=count)
break;
Expand Down

0 comments on commit 6984111

Please sign in to comment.