Skip to content

Commit

Permalink
add any2jd, pass opt to name check, add more options
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Oct 18, 2019
1 parent f97de9b commit 11712b7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 36 deletions.
96 changes: 66 additions & 30 deletions jdataencode.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,23 @@
%
% input:
% data: a structure (array) or cell (array) to be encoded.
% options: (optional) a struct or Param/value pairs for user specified options
% options: (optional) a struct or Param/value pairs for user
% specified options (first in [.|.] is the default)
% Base64: [0|1] if set to 1, _ArrayZipData_ is assumed to
% be encoded with base64 format and need to be
% decoded first. This is needed for JSON but not
% UBJSON data
% Prefix: [x0x5F|x] for JData files loaded via loadjson/loadubjson, the
% default JData keyword prefix is 'x0x5F'(default);
% if the json file is loaded using matlab2018's
% jsondecode(), the prefix is 'x'.
% UseArrayZipSize: [1|0] if set to 1, _ArrayZipSize_ will be added to
% store the "pre-processed" data dimensions, i.e.
% the original data stored in _ArrayData_, and then flaten
% _ArrayData_ into a row vector using row-major
% order; if set to 0, a 2D _ArrayData_ will be used
% MapAsStruct: [0|1] if set to 1, convert containers.Map into
% struct; otherwise, keep it as map
% Compression: ['zlib'|'gzip','lzma','lz4','lz4hc'] - use zlib method
% to compress data array
% CompressArraySize: [100|int]: only to compress an array if the
Expand All @@ -39,7 +55,7 @@
jdata=obj2jd(data,opt);

%%-------------------------------------------------------------------------
function newitem=obj2jd( item,varargin)
function newitem=obj2jd(item,varargin)

if(iscell(item))
newitem=cell2jd(item,varargin{:});
Expand All @@ -51,6 +67,8 @@
newitem=map2jd(item,varargin{:});
elseif(isa(item,'categorical'))
newitem=cell2jd(cellstr(item),varargin{:});
elseif(isa(item,'function_handle'))
newitem=struct2jd(functions(item),varargin{:});
elseif(islogical(item) || isnumeric(item))
newitem=mat2jd(item,varargin{:});
elseif(isa(item,'table'))
Expand Down Expand Up @@ -88,11 +106,17 @@
function newitem=map2jd(item,varargin)

names=item.keys;
newitem=containers.Map;
for i=1:length(names)
newitem(names{i})=obj2jd(item(names{i}),varargin{:});
if(jsonopt('MapAsStruct',0,varargin{:}))
newitem=struct;
for i=1:length(names)
newitem(N_(names{i},varargin{:}))=obj2jd(item(names{i}),varargin{:});
end
else
newitem=containers.Map;
for i=1:length(names)
newitem(names{i})=obj2jd(item(names{i}),varargin{:});
end
end

%%-------------------------------------------------------------------------
function newitem=mat2jd(item,varargin)
if(isempty(item) || isa(item,'string') || ischar(item) || (isvector(item) && isreal(item) && ~issparse(item)))
Expand All @@ -107,76 +131,88 @@
item=uint8(item);
end

newitem=struct(N_('_ArraySize_'),size(item),N_('_ArrayType_'),class(item));
N=@(x) N_(x,varargin{:});

newitem=struct(N('_ArraySize_'),size(item),N('_ArrayType_'),class(item));

if(isreal(item))
if(issparse(item))
fulldata=full(item(find(item)));
newitem.(N_('_ArrayIsSparse_'))=true;
newitem.(N_('_ArrayZipSize_'))=[2+(~isvector(item)),length(fulldata)];
newitem.(N('_ArrayIsSparse_'))=true;
newitem.(N('_ArrayZipSize_'))=[2+(~isvector(item)),length(fulldata)];
if(isvector(item))
newitem.(N_('_ArrayData_'))=[find(item)', fulldata(:)'];
newitem.(N('_ArrayData_'))=[find(item)', fulldata(:)'];
else
[ix,iy]=find(item);
newitem.(N_('_ArrayData_'))=[ix(:)' , iy(:)', fulldata(:)'];
newitem.(N('_ArrayData_'))=[ix(:)' , iy(:)', fulldata(:)'];
end
else
item=permute(item,ndims(item):-1:1);
newitem.(N_('_ArrayData_'))=item(:)';
newitem.(N('_ArrayData_'))=item(:)';
end
else
newitem.(N_('_ArrayIsComplex_'))=true;
newitem.(N('_ArrayIsComplex_'))=true;
if(issparse(item))
fulldata=full(item(find(item)));
newitem.(N_('_ArrayIsSparse_'))=true;
newitem.(N_('_ArrayZipSize_'))=[3+(~isvector(item)),length(fulldata)];
newitem.(N('_ArrayIsSparse_'))=true;
newitem.(N('_ArrayZipSize_'))=[3+(~isvector(item)),length(fulldata)];
if(isvector(item))
newitem.(N_('_ArrayData_'))=[find(item)', real(fulldata(:)'), imag(fulldata(:)')];
newitem.(N('_ArrayData_'))=[find(item)', real(fulldata(:)'), imag(fulldata(:)')];
else
[ix,iy]=find(item);
newitem.(N_('_ArrayData_'))=[ix(:)' , iy(:)' , real(fulldata(:)'), imag(fulldata(:)')];
newitem.(N('_ArrayData_'))=[ix(:)' , iy(:)' , real(fulldata(:)'), imag(fulldata(:)')];
end
else
newitem.(N_('_ArrayZipSize_'))=[2,numel(item)];
newitem.(N_('_ArrayData_'))=[real(item(:)'), imag(item(:)')];
newitem.(N('_ArrayZipSize_'))=[2,numel(item)];
newitem.(N('_ArrayData_'))=[real(item(:)'), imag(item(:)')];
end
end

if(jsonopt('UseArrayZipSize',1,varargin{:})==0)
data=newitem.(N('_ArrayData_'));
data=reshape(data,fliplr(newitem.(N('_ArrayZipSize_'))));
newitem.(N('_ArrayData_'))=permute(data,ndims(data):-1:1);
end
if(~isempty(zipmethod) && numel(item)>minsize)
compfun=str2func([zipmethod 'encode']);
newitem.(N_('_ArrayZipType_'))=lower(zipmethod);
newitem.(N_('_ArrayZipSize_'))=size(newitem.(N_('_ArrayData_')));
newitem.(N_('_ArrayZipData_'))=compfun(typecast(newitem.(N_('_ArrayData_')),'uint8'));
newitem.(N('_ArrayZipType_'))=lower(zipmethod);
newitem.(N('_ArrayZipSize_'))=size(newitem.(N('_ArrayData_')));
newitem.(N('_ArrayZipData_'))=compfun(typecast(newitem.(N('_ArrayData_')),'uint8'));
newitem=rmfield(newitem,N_('_ArrayData_'));
if(jsonopt('Base64',1,varargin{:}))
newitem.(N_('_ArrayZipData_'))=char(base64encode(newitem.(N_('_ArrayZipData_'))));
if(jsonopt('Base64',0,varargin{:}))
newitem.(N('_ArrayZipData_'))=char(base64encode(newitem.(N('_ArrayZipData_'))));
end
end

%%-------------------------------------------------------------------------
function newitem=table2jd(item,varargin)
newitem=struct;
newitem(N_('_TableRows_'))=item.Properties.RowNames';
newitem(N_('_TableCols_'))=item.Properties.VariableNames;
newitem(N_('_TableRecords_'))=table2cell(item);
newitem(N('_TableRows_',varargin{:}))=item.Properties.RowNames';
newitem(N('_TableCols_',varargin{:}))=item.Properties.VariableNames;
newitem(N('_TableRecords_',varargin{:}))=table2cell(item);

%%-------------------------------------------------------------------------
function newitem=graph2jd(item,varargin)
newitem=struct;
nodedata=table2struct(item.Nodes);
if(isfield(nodedata,'Name'))
nodedata=rmfield(nodedata,'Name');
newitem.(N_('_GraphNodes_'))=containers.Map(item.Nodes.Name,num2cell(nodedata),'uniformValues',false);
newitem.(N_('_GraphNodes_',varargin{:}))=containers.Map(item.Nodes.Name,num2cell(nodedata),'uniformValues',false);
else
newitem.(N_('_GraphNodes_'))=containers.Map(1:max(item.Edges.EndNodes(:)),num2cell(nodedata),'uniformValues',false);
newitem.(N_('_GraphNodes_',varargin{:}))=containers.Map(1:max(item.Edges.EndNodes(:)),num2cell(nodedata),'uniformValues',false);
end
edgenodes=item.Edges.EndNodes;
edgedata=table2struct(item.Edges);
if(isfield(edgedata,'EndNodes'))
edgedata=rmfield(edgedata,'EndNodes');
end
edgenodes(:,3)=num2cell(edgedata);
newitem.(N_('_GraphEdges_'))=edgenodes;
newitem.(N_('_GraphEdges_',varargin{:}))=edgenodes;

%%-------------------------------------------------------------------------
function newitem=any2jd(item,varargin)
newitem.(N_('_DataInfo_',varargin{:}))=struct('MATLABObjectClass',class(item),'MATLABObjectSize',size(item));
newitem.(N_('_ByteStream_',varargin{:}))=getByteStreamFromArray(item); % use undocumented matlab function

%%-------------------------------------------------------------------------
function newname=N_(name,varargin)
Expand Down
6 changes: 0 additions & 6 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@
opt.Compression=dozip;
end

if(isfield(opt,'norowbracket'))
warning('Option ''NoRowBracket'' is depreciated, please use ''SingletArray'' and set its value to not(NoRowBracket)');
if(~isfield(opt,'singletarray'))
opt.singletarray=not(opt.norowbracket);
end
end
rootisarray=0;
rootlevel=1;
forceroot=jsonopt('ForceRootName',0,opt);
Expand Down

0 comments on commit 11712b7

Please sign in to comment.