Skip to content

Commit

Permalink
add jdataencode and jdatadecode
Browse files Browse the repository at this point in the history
  • Loading branch information
Qianqian Fang committed Jun 11, 2019
1 parent f86219d commit 95b2eb0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
12 changes: 6 additions & 6 deletions struct2jdata.m → jdatadecode.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function newdata=struct2jdata(data,varargin)
function newdata=jdatadecode(data,varargin)
%
% newdata=struct2jdata(data,opt,...)
% newdata=jdatadecode(data,opt,...)
%
% convert a JData object (in the form of a struct array) into an array
%
Expand Down Expand Up @@ -31,7 +31,7 @@
% examples:
% obj=struct('_ArrayType_','double','_ArraySize_',[2 3],
% '_ArrayIsSparse_',1 ,'_ArrayData_',null);
% ubjdata=struct2jdata(obj);
% ubjdata=jdatadecode(obj);
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
Expand All @@ -42,7 +42,7 @@
newdata=data;
if(~isstruct(data))
if(iscell(data))
newdata=cellfun(@(x) struct2jdata(x),data,'UniformOutput',false);
newdata=cellfun(@(x) jdatadecode(x),data,'UniformOutput',false);
end
return;
end
Expand All @@ -55,9 +55,9 @@
for i=1:length(fn) % depth-first
for j=1:len
if(isstruct(data(j).(fn{i})))
newdata(j).(fn{i})=struct2jdata(data(j).(fn{i}));
newdata(j).(fn{i})=jdatadecode(data(j).(fn{i}));
elseif(iscell(data(j).(fn{i})))
newdata(j).(fn{i})=cellfun(@(x) struct2jdata(x),newdata(j).(fn{i}),'UniformOutput',false);
newdata(j).(fn{i})=cellfun(@(x) jdatadecode(x),newdata(j).(fn{i}),'UniformOutput',false);
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions jdataencode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function newdata=jdataencode(data,varargin)
%
% newdata=jdataencode(data,opt,...)
%
% encode special MATLAB objects (cells, structs, sparse and complex arrays,
% maps, graphs, function handles, etc) to the JData format
%
% authors:Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% data: a matlab object
% opt: (optional) a list of 'Param',value pairs for additional options.
% For all supported options, please see the help info for savejson.m
% and loadjson.m
%
% output:
% newdata: the covnerted data containing JData structures
%
% examples:
% jd=jdataencode(struct('a',rand(5)+1i*rand(5),'b',[],'c',sparse(5,5)))
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

newdata=loadjson(savejson('',data,varargin{:}),varargin{:},'JDataDecode',0);
6 changes: 3 additions & 3 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ function parse_char(inputstr, c)
end
end
parse_char(inputstr, '}');
if(isstruct(object))
object=struct2jdata(object,struct('Recursive',0));
if(isstruct(object) && jsonopt('JDataDecode',1,opt)==1)
object=jdatadecode(object,struct('Recursive',0));
end
end

Expand Down Expand Up @@ -517,4 +517,4 @@ function error_pos(msg, inputstr)
obj=reshape(obj,nextdim,numel(obj)/nextdim)';
nextidx=length(arraystr)+1;
end
end
end
4 changes: 2 additions & 2 deletions loadmsgpack.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
data=data{1};
end
if(iscell(data))
data=cellfun(@(x) struct2jdata(x),data,'UniformOutput',false);
data=cellfun(@(x) jdatadecode(x),data,'UniformOutput',false);
elseif(isstruct(data))
data=struct2jdata(data);
data=jdatadecode(data);
end
end

Expand Down
2 changes: 1 addition & 1 deletion loadubjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ function error_pos(inputstr, msg)
parse_char(inputstr, '}');
end
if(isstruct(object))
object=struct2jdata(object,struct('Recursive',0, 'Base64',0));
object=jdatadecode(object,struct('Recursive',0, 'Base64',0));
end
end

Expand Down

0 comments on commit 95b2eb0

Please sign in to comment.