forked from jacklxc/Virtual-Rat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmloads.m
136 lines (119 loc) · 3.66 KB
/
mloads.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function out = mloads(jstr, varargin)
% out = mdumps(obj, ['compress'])
% function that takes a matlab object (cell array, struct, vector) and converts it into json.
% It also creates a "sister" json object that describes the type and dimension of the "leaf" elements.
if isempty(jstr)
out = {};
return;
end
if ischar(jstr)
decompress = false;
elseif char(jstr(1))=='{'
decompress = false;
jstr = char(jstr(:))';
else
decompress = true;
end
decompress = utils.inputordefault('decompress',decompress,varargin);
if decompress
jstr = char(utils.zlibdecode(jstr));
end
jstr = regexprep(jstr, '\<NaN\>', 'null');
try
bigJ = jsondecode(jstr);
builtin_flag = true;
catch
bigJ = json.fromjson(jstr);
builtin_flag = false;
end
out = bigJ.vals;
meta = bigJ.info;
if builtin_flag
out = applyinfo_bi(out, meta);
else
out = applyinfo(out, meta);
end
end
function vals = applyinfo(vals, meta)
if isfield(meta,'type__')
% Then we are a leaf node
tsize =double([meta.dim__{1} meta.dim__{2}]);
tnumel = prod(tsize);
switch(meta.type__)
case {'cell', 'struct'}
for cx = 1:tnumel
vals{cx} = applyinfo(vals{cx}, meta.cell__{cx});
end
if strcmp(meta.type__, 'struct') % This is a struct array
vals = [vals{:}];
end
vals = reshape(vals, tsize);
case 'char'
vals = char(vals);
case 'double'
if tnumel == 1
vals = double(vals);
else
vals = double([vals{:}]);
vals = reshape(vals, tsize);
end
otherwise
f = @(x) cast(x, meta.type__);
if tnumel == 1 || strcmp(meta.type__, 'char')
vals = f(vals);
else
vals = cellfun(f, vals);
% vals = cell2mat(vals);
vals = reshape(vals, tsize);
end
end
else
fnames = fieldnames(meta);
for fx = 1:numel(fnames)
vals.(fnames{fx}) = applyinfo(vals.(fnames{fx}), meta.(fnames{fx}));
end
end
end
function vals = applyinfo_bi(vals, meta)
if iscell(meta)
meta = meta{1};
end
if isfield(meta,'type__')
% Then we are a leaf node
tsize =meta.dim__(:)';
tnumel = prod(tsize);
switch(meta.type__)
case {'cell', 'struct'}
newvals=cell(tnumel,1);
for cx = 1:tnumel
if iscell(vals)
newvals{cx} = applyinfo_bi(vals{cx}, meta.cell__(cx));
else
newvals{cx} = applyinfo_bi(vals(cx), meta.cell__(cx));
end
end
if strcmp(meta.type__, 'struct') % This is a struct array
newvals = [newvals{:}];
end
vals = reshape(newvals, tsize);
case 'char'
vals = char(vals);
case 'double'
vals = reshape(vals, tsize);
otherwise
f = @(x) cast(x, meta.type__);
if tnumel == 1 || strcmp(meta.type__, 'char')
vals = f(vals);
else
vals = cellfun(f, vals);
% vals = cell2mat(vals);
vals = reshape(vals, tsize);
end
end
else
fnames = fieldnames(meta);
for fx = 1:numel(fnames)
vals.(fnames{fx}) = applyinfo_bi(vals.(fnames{fx}), meta.(fnames{fx}));
end
end
end