Skip to content

Commit 067bbaa

Browse files
committed
[sync] update jsonlab to the latest
1 parent 8eec27c commit 067bbaa

9 files changed

+103
-60
lines changed

base64decode.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
output = zmat(varargin{1}, 0, 'base64');
3535
return
3636
elseif (isoctavemesh)
37-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
37+
try
38+
output = matlab.net.base64decode(varargin{1});
39+
catch
40+
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
41+
end
3842
end
3943

4044
error(javachk('jvm'));

gzipdecode.m

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@
3636
if (nargin == 0)
3737
error('you must provide at least 1 input');
3838
end
39-
if (exist('zmat', 'file') == 2 || exist('zmat', 'file') == 3)
39+
40+
nozmat = getvarfrom({'caller', 'base'}, 'NO_ZMAT');
41+
42+
if ((exist('zmat', 'file') == 2 || exist('zmat', 'file') == 3) && (isempty(nozmat) || nozmat == 0))
4043
if (nargin > 1)
4144
[varargout{1:nargout}] = zmat(varargin{1}, varargin{2:end});
4245
else
4346
[varargout{1:nargout}] = zmat(varargin{1}, 0, 'gzip', varargin{2:end});
4447
end
4548
return
4649
elseif (isoctavemesh)
47-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
50+
[varargout{1:nargout}] = octavezmat(varargin{1}, 0, 'gzip');
51+
return
4852
end
4953
error(javachk('jvm'));
5054

gzipencode.m

+13-5
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,28 @@
3535
error('you must provide at least 1 input');
3636
end
3737

38-
if (exist('zmat', 'file') == 2 || exist('zmat', 'file') == 3)
38+
nozmat = getvarfrom({'caller', 'base'}, 'NO_ZMAT');
39+
40+
if ((exist('zmat', 'file') == 2 || exist('zmat', 'file') == 3) && (isempty(nozmat) || nozmat == 0))
3941
[varargout{1:nargout}] = zmat(varargin{1}, 1, 'gzip');
4042
return
4143
elseif (isoctavemesh)
42-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
44+
[varargout{1:nargout}] = octavezmat(varargin{1}, 1, 'gzip');
45+
return
4346
end
4447

4548
error(javachk('jvm'));
4649

47-
if (ischar(varargin{1}))
48-
varargin{1} = uint8(varargin{1});
50+
input = varargin{1}(:)';
51+
if (ischar(input))
52+
input = uint8(input);
53+
elseif (isa(input, 'string'))
54+
input = uint8(char(input));
55+
else
56+
input = typecast(input, 'uint8');
4957
end
5058

51-
input = typecast(varargin{1}(:)', 'uint8');
59+
input = typecast(input, 'uint8');
5260

5361
buffer = java.io.ByteArrayOutputStream();
5462
gzip = java.util.zip.GZIPOutputStream(buffer);

jdatadecode.m

+7-24
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
end
112112

113113
%% handle array data
114-
if (isfield(data, N_('_ArrayType_')) && (isfield(data, N_('_ArrayData_')) || isfield(data, N_('_ArrayZipData_'))))
114+
if (isfield(data, N_('_ArrayType_')) && (isfield(data, N_('_ArrayData_')) || (isfield(data, N_('_ArrayZipData_')) && ~isstruct(data.(N_('_ArrayZipData_'))))))
115115
newdata = cell(len, 1);
116116
for j = 1:len
117117
if (isfield(data, N_('_ArrayZipSize_')) && isfield(data, N_('_ArrayZipData_')))
@@ -477,29 +477,12 @@
477477
end
478478
if (~isempty(ref.path))
479479
uripath = [ref.proto ref.path];
480-
[fpath, fname, fext] = fileparts(uripath);
481-
opt.maxlinklevel = opt.maxlinklevel - 1;
482-
switch (lower(fext))
483-
case {'.json', '.jnii', '.jdt', '.jdat', '.jmsh', '.jnirs'}
484-
newdata = loadjson(uripath, opt);
485-
case {'.bjd', '.bnii', '.jdb', '.jbat', '.bmsh', '.bnirs', '.pmat'}
486-
newdata = loadbj(uripath, opt, 'Base64', 0);
487-
case {'.ubj'}
488-
newdata = loadubjson(uripath, opt, 'Base64', 0);
489-
case {'.msgpack'}
490-
newdata = loadmsgpack(uripath, opt, 'Base64', 0);
491-
case {'.h5', '.hdf5', '.snirf'} % this requires EasyH5 toolbox
492-
newdata = loadh5(uripath, opt);
493-
otherwise
494-
% _DataLink_ url does not specify type, assuming JSON format
495-
if (regexpi(datalink, '^\s*(http|https|ftp|file)://'))
496-
newdata = loadjson(uripath, opt);
497-
else
498-
warning('_DataLink_ url is not supported');
499-
end
500-
end
501-
if (~isempty(ref.jsonpath))
502-
newdata = getfromjsonpath(newdata, ref.jsonpath);
480+
[newdata, fname] = jdlink(uripath);
481+
if (exist(fname, 'file'))
482+
opt.maxlinklevel = opt.maxlinklevel - 1;
483+
if (~isempty(ref.jsonpath))
484+
newdata = jsonpath(newdata, ref.jsonpath);
485+
end
503486
end
504487
end
505488
end

jdataencode.m

+36-11
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@
101101

102102
if (iscell(item))
103103
newitem = cell2jd(item, varargin{:});
104+
elseif (isa(item, 'jdict'))
105+
newitem = obj2jd(item(), varargin{:});
104106
elseif (isstruct(item))
105107
newitem = struct2jd(item, varargin{:});
106-
elseif (isnumeric(item) || islogical(item))
108+
elseif (isnumeric(item) || islogical(item) || isa(item, 'timeseries'))
107109
newitem = mat2jd(item, varargin{:});
108110
elseif (ischar(item) || isa(item, 'string'))
109111
newitem = mat2jd(item, varargin{:});
110-
elseif (isa(item, 'containers.Map'))
112+
elseif (isa(item, 'containers.Map') || isa(item, 'dictionary'))
111113
newitem = map2jd(item, varargin{:});
112114
elseif (isa(item, 'categorical'))
113115
newitem = cell2jd(cellstr(item), varargin{:});
@@ -166,13 +168,21 @@
166168
end
167169
end
168170
else % keep as a map and only encode its values
169-
if (strcmp(item.KeyType, 'char'))
171+
if (isa(item, 'dictionary'))
172+
newitem = dictionary();
173+
elseif (strcmp(item.KeyType, 'char'))
170174
newitem = containers.Map();
171175
else
172176
newitem = containers.Map('KeyType', item.KeyType, 'ValueType', 'any');
173177
end
174-
for i = 1:length(names)
175-
newitem(names{i}) = obj2jd(item(names{i}), varargin{:});
178+
if (isa(item, 'dictionary'))
179+
for i = 1:length(names)
180+
newitem(names(i)) = obj2jd(item(names(i)), varargin{:});
181+
end
182+
else
183+
for i = 1:length(names)
184+
newitem(names{i}) = obj2jd(item(names{i}), varargin{:});
185+
end
176186
end
177187
end
178188

@@ -185,6 +195,18 @@
185195
zipmethod = varargin{1}.compression;
186196
minsize = varargin{1}.compressarraysize;
187197

198+
if (isa(item, 'timeseries'))
199+
if (item.TimeInfo.isUniform && item.TimeInfo.Increment == 1)
200+
if (ndims(item.Data) == 3 && size(item.Data, 1) == 1 && size(item.Data, 2) == 1)
201+
item = permute(item.Data, [2 3 1]);
202+
else
203+
item = squeeze(item.Data);
204+
end
205+
else
206+
item = [item.Time squeeze(item.Data)];
207+
end
208+
end
209+
188210
% 2d numerical (real/complex/sparse) arrays with _ArrayShape_ encoding enabled
189211
if (varargin{1}.usearrayshape && ndims(item) == 2 && ~isvector(item))
190212
encoded = 1;
@@ -394,12 +416,15 @@
394416

395417
%% -------------------------------------------------------------------------
396418
function newitem = any2jd(item, varargin)
397-
398-
N = @(x) N_(x, varargin{:});
399-
newitem.(N('_DataInfo_')) = struct('MATLABObjectClass', class(item), 'MATLABObjectSize', size(item));
400-
newitem.(N('_ByteStream_')) = getByteStreamFromArray(item); % use undocumented matlab function
401-
if (varargin{1}.base64)
402-
newitem.(N('_ByteStream_')) = char(base64encode(newitem.(N('_ByteStream_'))));
419+
try
420+
N = @(x) N_(x, varargin{:});
421+
newitem.(N('_DataInfo_')) = struct('MATLABObjectClass', class(item), 'MATLABObjectSize', size(item));
422+
newitem.(N('_ByteStream_')) = getByteStreamFromArray(item); % use undocumented matlab function
423+
if (varargin{1}.base64)
424+
newitem.(N('_ByteStream_')) = char(base64encode(newitem.(N('_ByteStream_'))));
425+
end
426+
catch
427+
error('any2jd: failed to convert object of type %s', class(item));
403428
end
404429

405430
%% -------------------------------------------------------------------------

loadbidstsv.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
fid = fid((endpos + 1):end);
4747
end
4848
else
49-
fid = fopen(tsvfile, 'rb');
49+
fid = fopen(tsvfile, 'rt');
5050
header = fgetl(fid);
5151
header = regexprep(header, '\s*$', '');
5252
end

loadbj.m

+28-14
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
else
108108
string = urlread(fname);
109109
end
110-
elseif (~isempty(fname) && any(fname(1) == '[{SCHiUIulmLMhdDTFZN'))
110+
elseif (~isempty(fname) && any(fname(1) == '[{SCBHiUIulmLMhdDTFZN'))
111111
string = fname;
112112
else
113113
error('input file does not exist or buffer is invalid');
@@ -166,10 +166,10 @@
166166
else
167167
[data{jsoncount}, pos] = parse_array(inputstr, pos, opt);
168168
end
169-
case {'S', 'C', 'H', 'i', 'U', 'I', 'u', 'l', 'm', 'L', 'M', 'h', 'd', 'D', 'T', 'F', 'Z', 'N'}
169+
case {'S', 'C', 'B', 'H', 'i', 'U', 'I', 'u', 'l', 'm', 'L', 'M', 'h', 'd', 'D', 'T', 'F', 'Z', 'N'}
170170
[data{jsoncount}, pos] = parse_value(inputstr, pos, [], opt);
171171
otherwise
172-
error_pos('Root level structure must start with a valid marker "{[SCHiUIulmLMhdDTFZN"', inputstr, pos);
172+
error_pos('Root level structure must start with a valid marker "{[SCBHiUIulmLMhdDTFZN"', inputstr, pos);
173173
end
174174
if (jsoncount >= maxobjid)
175175
break
@@ -219,8 +219,8 @@
219219
[data{i}, pos] = parse_value(inputstr, pos, type, varargin{:});
220220
end
221221
adv = pos - adv;
222-
case 'C'
223-
data = inputstr(pos:pos + count);
222+
case {'C', 'B'}
223+
data = inputstr(pos:pos + count - 1);
224224
adv = count;
225225
case {'T', 'F', 'N'}
226226
error_pos(sprintf('For security reasons, optimized type %c is disabled at position %%d', type), inputstr, pos);
@@ -247,6 +247,7 @@
247247
pos = parse_char(inputstr, pos, '[');
248248
object = cell(0, 1);
249249
dim = [];
250+
iscolumn = 0;
250251
type = '';
251252
count = -1;
252253
[cc, pos] = next_char(inputstr, pos);
@@ -263,6 +264,9 @@
263264
error_pos('ND array size specifier does not support embedding', inputstr, pos);
264265
end
265266
varargin{1}.noembedding_ = 1;
267+
if (pos + 1 < length(inputstr) && inputstr(pos + 1) == '[')
268+
iscolumn = 1;
269+
end
266270
[dim, pos] = parse_array(inputstr, pos, varargin{:});
267271
count = prod(double(dim));
268272
varargin{1}.noembedding_ = 0;
@@ -274,8 +278,12 @@
274278
if (~isempty(type))
275279
if (count >= 0)
276280
[object, adv] = parse_block(inputstr, pos, type, count, varargin{:});
277-
if (~isempty(dim))
278-
object = permute(reshape(object, fliplr(dim(:)')), length(dim):-1:1);
281+
if (~isempty(dim) && length(dim) > 1)
282+
if (iscolumn == 0)
283+
object = permute(reshape(object, fliplr(dim(:)')), length(dim):-1:1);
284+
else
285+
object = reshape(object, dim);
286+
end
279287
end
280288
pos = pos + adv;
281289
return
@@ -302,9 +310,13 @@
302310
[val, pos] = parse_value(inputstr, pos, [], varargin{:});
303311
end
304312
object{end + 1} = val;
305-
[cc, pos] = next_char(inputstr, pos);
306-
if cc == ']'
313+
if count > 0 && length(object) >= count
307314
break
315+
else
316+
[cc, pos] = next_char(inputstr, pos);
317+
if cc == ']' || (count > 0 && length(object) >= count)
318+
break
319+
end
308320
end
309321
end
310322
end
@@ -374,14 +386,14 @@
374386
function [str, pos] = parseStr(inputstr, pos, type, varargin)
375387
if (isempty(type))
376388
type = inputstr(pos);
377-
if type ~= 'S' && type ~= 'C' && type ~= 'H'
389+
if type ~= 'S' && type ~= 'C' && type ~= 'B' && type ~= 'H'
378390
error_pos('String starting with S expected at position %d', inputstr, pos);
379391
else
380392
pos = pos + 1;
381393
end
382394
end
383395

384-
if (type == 'C')
396+
if (type == 'C' || type == 'B')
385397
str = inputstr(pos);
386398
pos = pos + 1;
387399
return
@@ -428,7 +440,7 @@
428440
varargout{3} = {};
429441
end
430442
switch (cc)
431-
case {'S', 'C', 'H'}
443+
case {'S', 'C', 'B', 'H'}
432444
[varargout{1:2}] = parseStr(inputstr, varargout{2}, type, varargin{:});
433445
return
434446
case '['
@@ -484,8 +496,10 @@
484496
object = [];
485497
end
486498
count = -1;
499+
type = [];
487500
[cc, pos] = next_char(inputstr, pos);
488501
if (cc == '$')
502+
type = inputstr(pos + 1);
489503
pos = pos + 2;
490504
end
491505
[cc, pos] = next_char(inputstr, pos);
@@ -518,7 +532,7 @@
518532
mmap{end}{2} = [mmap{end}{2}, pos - mmap{end}{2}];
519533
mmap = [mmap(:); newmmap(:)];
520534
else
521-
[val, pos] = parse_value(inputstr, pos, [], varargin{:});
535+
[val, pos] = parse_value(inputstr, pos, type, varargin{:});
522536
end
523537
num = num + 1;
524538
if (usemap)
@@ -533,7 +547,7 @@
533547
object.(str) = val;
534548
end
535549
[cc, pos] = next_char(inputstr, pos);
536-
if (count >= 0 && num >= count) || cc == '}'
550+
if ((count >= 0 && num >= count) || (~isempty(cc) && cc == '}'))
537551
break
538552
end
539553
end

savebj.m

+6-1
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,14 @@
870870
%% -------------------------------------------------------------------------
871871
function val = S_(str, varargin)
872872
ismsgpack = varargin{1}.messagepack;
873+
isdebug = varargin{1}.debug;
873874
Smarker = varargin{1}.SM_;
874875
if (length(str) == 1)
875-
val = [Smarker(1) str];
876+
if (isdebug)
877+
val = [Smarker(1) sprintf('<%d>', str)];
878+
else
879+
val = [Smarker(1) str];
880+
end
876881
else
877882
if (ismsgpack)
878883
val = [Imsgpk_(length(str), 218, 160, varargin{:}) str];

savejson.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
%% -------------------------------------------------------------------------
290290
function txt = obj2json(name, item, level, varargin)
291291

292-
if (iscell(item) || isa(item, 'string'))
292+
if (iscell(item) || (isa(item, 'string') && numel(item) > 1))
293293
txt = cell2json(name, item, level, varargin{:});
294294
elseif (isa(item, 'jdict'))
295295
txt = obj2json(name, item, level, varargin{:});

0 commit comments

Comments
 (0)