diff --git a/loadjson.m b/loadjson.m index ea9c7a0..af5fbfc 100644 --- a/loadjson.m +++ b/loadjson.m @@ -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, @@ -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 @@ -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; diff --git a/loadubjson.m b/loadubjson.m index 06472a4..a17162f 100644 --- a/loadubjson.m +++ b/loadubjson.m @@ -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 @@ -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); @@ -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;