Skip to content

Commit

Permalink
make demos to run with jdataencode, but still have issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Oct 22, 2019
1 parent 874945f commit 0f870c0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 49 deletions.
43 changes: 32 additions & 11 deletions jdataencode.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
newitem=table2jd(item,varargin{:});
elseif(isa(item,'digraph') || isa(item,'graph'))
newitem=graph2jd(item,varargin{:});
elseif(~isoctavemesh)
newitem=any2jd(item,varargin{:});
elseif(isobject(item))
newitem=matlabobject2jd(item,varargin{:});
else
newitem=item;
end
Expand Down Expand Up @@ -128,7 +128,9 @@
%%-------------------------------------------------------------------------
function newitem=mat2jd(item,varargin)

if(isempty(item) || isa(item,'string') || ischar(item) || (isvector(item) && isreal(item) && ~issparse(item)))
if(isempty(item) || isa(item,'string') || ischar(item) || ...
(isvector(item) && isreal(item) && ~issparse(item)) || ...
jsonopt('NestArray',0,varargin{:}))
newitem=item;
return;
end
Expand All @@ -142,15 +144,15 @@

N=@(x) N_(x,varargin{:});

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

if(isreal(item))
if(issparse(item))
fulldata=full(item(find(item)));
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(:)'];
Expand All @@ -166,14 +168,14 @@
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('_ArrayData_'))=[real(item(:))', imag(item(:))'];
end
end

Expand All @@ -198,9 +200,9 @@
function newitem=table2jd(item,varargin)

newitem=struct;
newitem(N_('_TableRows_',varargin{:}))=item.Properties.RowNames';
newitem(N_('_TableCols_',varargin{:}))=item.Properties.VariableNames;
newitem(N_('_TableRecords_',varargin{:}))=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)
Expand Down Expand Up @@ -229,6 +231,25 @@
newitem.(N_('_GraphEdges_',varargin{:}))=edgenodes;
end

%%-------------------------------------------------------------------------
function newitem=matlabobject2jd(item,varargin)
try
if numel(item) == 0 %empty object
newitem = struct();
elseif numel(item) == 1 %
newitem = char(item);
else
propertynames = properties(item);
for p = 1:numel(propertynames)
for o = numel(item):-1:1 % aray of objects
newitem(o).(propertynames{p}) = item(o).(propertynames{p});
end
end
end
catch
newitem=any2jd(item,varargin{:});
end

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

Expand Down
2 changes: 1 addition & 1 deletion loadmsgpack.m
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
for n=1:len
[out{n}, idx] = parse(bytes, idx);
end
if(true)
if(isnumeric(out{1}))
try
oldobj=out;
out=cell2mat(out);
Expand Down
41 changes: 21 additions & 20 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
opt.IsOctave=isoctavemesh;

if(jsonopt('PreEncode',0,opt))
obj=jdataencode(obj,opt);
obj=jdataencode(obj,'Base64',1,'UseArrayZipSize',0,opt);
end

dozip=jsonopt('Compression','',opt);
Expand Down Expand Up @@ -235,12 +235,10 @@
txt=matlabtable2json(name,item,level,varargin{:});
elseif(isa(item,'graph') || isa(item,'digraph'))
txt=struct2json(name,jdataencode(item),level,varargin{:});
elseif(isobject(item))
txt=matlabobject2json(name,item,level,varargin{:});
else
if(isoctavemesh)
txt=matlabobject2json(name,item,level,varargin{:});
else
txt=any2json(name,item,level,varargin{:});
end
txt=any2json(name,item,level,varargin{:});
end

%%-------------------------------------------------------------------------
Expand Down Expand Up @@ -614,21 +612,24 @@

%%-------------------------------------------------------------------------
function txt=matlabobject2json(name,item,level,varargin)
if numel(item) == 0 %empty object
st = struct();
elseif numel(item) == 1 %
st = struct();
txt = str2json(name, char(item), level, varargin(:));
return
else
propertynames = properties(item);
for p = 1:numel(propertynames)
for o = numel(item):-1:1 % aray of objects
st(o).(propertynames{p}) = item(o).(propertynames{p});
end
try
if numel(item) == 0 %empty object
st = struct();
elseif numel(item) == 1 %
txt = str2json(name, char(item), level, varargin(:));
return
else
propertynames = properties(item);
for p = 1:numel(propertynames)
for o = numel(item):-1:1 % aray of objects
st(o).(propertynames{p}) = item(o).(propertynames{p});
end
end
end
txt=struct2json(name,st,level,varargin{:});
catch
txt = any2json(name,item, level, varargin(:));
end
txt=struct2json(name,st,level,varargin{:});

%%-------------------------------------------------------------------------
function txt=matlabtable2json(name,item,level,varargin)
Expand Down Expand Up @@ -712,7 +713,7 @@
%%-------------------------------------------------------------------------
function txt=any2json(name,item,level,varargin)
st=containers.Map();
st('_DataInfo_')=struct('MATLABObjectClass',class(item),'MATLABObjectSize',size(item));;
st('_DataInfo_')=struct('MATLABObjectClass',class(item),'MATLABObjectSize',size(item));
st('_ByteStream_')=char(base64encode(getByteStreamFromArray(item)));

if(isempty(name))
Expand Down
37 changes: 20 additions & 17 deletions saveubjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
opt.IsOctave=isoctavemesh;

if(jsonopt('PreEncode',0,opt))
obj=jdataencode(obj,opt);
obj=jdataencode(obj,'Base64',0,opt);
end

dozip=jsonopt('Compression','',opt);
Expand Down Expand Up @@ -235,12 +235,10 @@
txt=matlabtable2ubjson(name,item,level,varargin{:});
elseif(isa(item,'graph') || isa(item,'digraph'))
txt=struct2ubjson(name,jdataencode(item),level,varargin{:});
elseif(isobject(item))
txt=matlabobject2ubjson(name,item,level,varargin{:});
else
if(isoctavemesh)
txt=matlabobject2ubjson(name,item,level,varargin{:});
else
txt=any2ubjson(name,item,level,varargin{:});
end
txt=any2ubjson(name,item,level,varargin{:});
end

%%-------------------------------------------------------------------------
Expand Down Expand Up @@ -613,19 +611,24 @@

%%-------------------------------------------------------------------------
function txt=matlabobject2ubjson(name,item,level,varargin)
st = struct();
if numel(item) > 0 %non-empty object
% "st = struct(item);" would produce an inmutable warning, because it
% make the protected and private properties visible. Instead we get the
% visible properties
propertynames = properties(item);
for p = 1:numel(propertynames)
for o = numel(item):-1:1 % aray of objects
st(o).(propertynames{p}) = item(o).(propertynames{p});
end
try
if numel(item) == 0 %empty object
st = struct();
elseif numel(item) == 1 %
txt = str2ubjson(name, char(item), level, varargin(:));
return
else
propertynames = properties(item);
for p = 1:numel(propertynames)
for o = numel(item):-1:1 % aray of objects
st(o).(propertynames{p}) = item(o).(propertynames{p});
end
end
end
txt = struct2ubjson(name,st,level,varargin{:});
catch
txt = any2ubjson(name,item, level, varargin(:));
end
txt=struct2ubjson(name,st,level,varargin{:});

%%-------------------------------------------------------------------------
function txt=matdata2ubjson(mat,level,varargin)
Expand Down

0 comments on commit 0f870c0

Please sign in to comment.