Skip to content

Commit

Permalink
reduce jsonopt calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Oct 24, 2019
1 parent 82c9e91 commit d4275c6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 68 deletions.
34 changes: 19 additions & 15 deletions loadmsgpack.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
else
bytes=fname;
end

opt=varargin2struct(varargin{:});
opt.simplifycell=jsonopt('SimplifyCell',0,opt);

jsoncount=1;
idx=0;
while idx <= length(bytes)
[obj, idx] = parse(uint8(bytes(:)), 1);
[obj, idx] = parse(uint8(bytes(:)), 1, opt);
data{jsoncount}=obj;
jsoncount=jsoncount+1;
end
Expand All @@ -44,11 +48,11 @@
if(iscell(data))
data=cellfun(@(x) jdatadecode(x),data,'UniformOutput',false);
elseif(isstruct(data))
data=jdatadecode(data,'Base64',0);
data=jdatadecode(data,'Base64',0, opt);
end
end

function [obj, idx] = parse(bytes, idx)
function [obj, idx] = parse(bytes, idx, varargin)
% masks:
b10000000 = 128;
b01111111 = 127;
Expand Down Expand Up @@ -78,12 +82,12 @@
elseif bitand(b11110000, currentbyte) == b10000000
% decode fixmap
len = double(bitand(b00001111, currentbyte));
[obj, idx] = parsemap(len, bytes, idx+1);
[obj, idx] = parsemap(len, bytes, idx+1, varargin{:});
return
elseif bitand(b11110000, currentbyte) == b10010000
% decode fixarray
len = double(bitand(b00001111, currentbyte));
[obj, idx] = parsearray(len, bytes, idx+1);
[obj, idx] = parsearray(len, bytes, idx+1, varargin{:});
return
elseif bitand(b11100000, currentbyte) == b10100000
% decode fixstr
Expand Down Expand Up @@ -172,16 +176,16 @@
[obj, idx] = parsestring(len, bytes, idx+5);
case 220 % array16
len = double(bytes2scalar(bytes(idx+1:idx+2), 'uint16'));
[obj, idx] = parsearray(len, bytes, idx+3);
[obj, idx] = parsearray(len, bytes, idx+3, varargin{:});
case 221 % array32
len = double(bytes2scalar(bytes(idx+1:idx+4), 'uint32'));
[obj, idx] = parsearray(len, bytes, idx+5);
[obj, idx] = parsearray(len, bytes, idx+5, varargin{:});
case 222 % map16
len = double(bytes2scalar(bytes(idx+1:idx+2), 'uint16'));
[obj, idx] = parsemap(len, bytes, idx+3);
[obj, idx] = parsemap(len, bytes, idx+3, varargin{:});
case 223 % map32
len = double(bytes2scalar(bytes(idx+1:idx+4), 'uint32'));
[obj, idx] = parsemap(len, bytes, idx+5);
[obj, idx] = parsemap(len, bytes, idx+5, varargin{:});
otherwise
error('transplant:parsemsgpack:unknowntype', ...
['Unknown type "' dec2bin(currentbyte) '"']);
Expand Down Expand Up @@ -213,10 +217,10 @@
idx = idx + len + 1;
end

function [out, idx] = parsearray(len, bytes, idx)
out = cell(1, len);
function [out, idx] = parsearray(len, bytes, idx, varargin)
out = cell(len,1);
for n=1:len
[out{n}, idx] = parse(bytes, idx);
[out{n}, idx] = parse(bytes, idx, varargin{:});
end
if(isnumeric(out{1}))
try
Expand All @@ -232,10 +236,10 @@
end
end

function [out, idx] = parsemap(len, bytes, idx)
function [out, idx] = parsemap(len, bytes, idx, varargin)
out = struct();
for n=1:len
[key, idx] = parse(bytes, idx);
[out.(encodevarname(char(key))), idx] = parse(bytes, idx);
[key, idx] = parse(bytes, idx, varargin{:});
[out.(encodevarname(char(key))), idx] = parse(bytes, idx, varargin{:});
end
end
12 changes: 8 additions & 4 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
% FileName [''|string]: a file name to save the output JSON data
% FloatFormat ['%.10g'|string]: format to show each numeric element
% of a 1D/2D array;
% IntFormat ['%d'|string]: format to display integer elements
% of a 1D/2D array;
% ArrayIndent [1|0]: if 1, output explicit data array with
% precedent indentation; if 0, no indentation
% ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D
Expand Down Expand Up @@ -145,6 +147,8 @@
opt.intformat=jsonopt('IntFormat','%d',opt);
opt.floatformat=jsonopt('FloatFormat','%.10g',opt);
opt.unpackhex=jsonopt('UnpackHex',1,opt);
opt.arraytostruct=jsonopt('ArrayToStruct',0,opt);
opt.num2cell_=0;

if(jsonopt('PreEncode',1,opt))
obj=jdataencode(obj,'Base64',1,'UseArrayZipSize',0,opt);
Expand Down Expand Up @@ -263,7 +267,7 @@
if(~iscell(item) && ~isa(item,'string'))
error('input is not a cell or string array');
end
isnum2cell=jsonopt('num2cell_',0,varargin{:});
isnum2cell=varargin{1}.num2cell_;
if(isnum2cell)
item=squeeze(item);
else
Expand Down Expand Up @@ -523,7 +527,7 @@
isnest=varargin{1}.nestarray;

if(((isnest==0) && length(size(item))>2) || issparse(item) || ~isreal(item) || ...
(isempty(item) && any(size(item))) ||jsonopt('ArrayToStruct',0,varargin{:}) || (~isempty(dozip) && numel(item)>zipsize))
(isempty(item) && any(size(item))) || varargin{1}.arraytostruct || (~isempty(dozip) && numel(item)>zipsize))
if(isempty(name))
txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',...
padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl);
Expand Down Expand Up @@ -662,13 +666,13 @@
nl=ws.newline;
isnest=varargin{1}.nestarray;
format=varargin{1}.formatversion;
isnum2cell=jsonopt('num2cell_',0,varargin{:});
isnum2cell=varargin{1}.num2cell_;

if(~isvector(mat) && isnest==1)
if(format>1.9 && isnum2cell==0)
mat=permute(mat,ndims(mat):-1:1);
end
varargin{:}.num2cell_=1;
varargin{1}.num2cell_=1;
txt=cell2json('',num2cell(mat,1),level-1,varargin{:});
return;
else
Expand Down
Loading

0 comments on commit d4275c6

Please sign in to comment.