Skip to content

Commit 6f72eed

Browse files
committed
[feat] support B for byte strean in Spec Draft 3
1 parent b5c2f22 commit 6f72eed

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

loadbj.m

Lines changed: 12 additions & 10 deletions
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);
@@ -378,14 +378,14 @@
378378
function [str, pos] = parseStr(inputstr, pos, type, varargin)
379379
if (isempty(type))
380380
type = inputstr(pos);
381-
if type ~= 'S' && type ~= 'C' && type ~= 'H'
381+
if type ~= 'S' && type ~= 'C' && type ~= 'B' && type ~= 'H'
382382
error_pos('String starting with S expected at position %d', inputstr, pos);
383383
else
384384
pos = pos + 1;
385385
end
386386
end
387387

388-
if (type == 'C')
388+
if (type == 'C' || type == 'B')
389389
str = inputstr(pos);
390390
pos = pos + 1;
391391
return
@@ -432,7 +432,7 @@
432432
varargout{3} = {};
433433
end
434434
switch (cc)
435-
case {'S', 'C', 'H'}
435+
case {'S', 'C', 'B', 'H'}
436436
[varargout{1:2}] = parseStr(inputstr, varargout{2}, type, varargin{:});
437437
return
438438
case '['
@@ -488,8 +488,10 @@
488488
object = [];
489489
end
490490
count = -1;
491+
type = [];
491492
[cc, pos] = next_char(inputstr, pos);
492493
if (cc == '$')
494+
type = inputstr(pos + 1);
493495
pos = pos + 2;
494496
end
495497
[cc, pos] = next_char(inputstr, pos);
@@ -522,7 +524,7 @@
522524
mmap{end}{2} = [mmap{end}{2}, pos - mmap{end}{2}];
523525
mmap = [mmap(:); newmmap(:)];
524526
else
525-
[val, pos] = parse_value(inputstr, pos, [], varargin{:});
527+
[val, pos] = parse_value(inputstr, pos, type, varargin{:});
526528
end
527529
num = num + 1;
528530
if (usemap)
@@ -537,7 +539,7 @@
537539
object.(str) = val;
538540
end
539541
[cc, pos] = next_char(inputstr, pos);
540-
if (count >= 0 && num >= count) || cc == '}'
542+
if ((count >= 0 && num >= count) || (~isempty(cc) && cc == '}'))
541543
break
542544
end
543545
end

savebj.m

Lines changed: 6 additions & 1 deletion
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];

test/run_jsonlab_test.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function run_jsonlab_test(tests)
217217
end
218218
test_jsonlab('row vector', @savebj, [1, 2, 3], '[$U#U<3><1><2><3>', 'debug', 1);
219219
test_jsonlab('column vector', @savebj, [1; 2; 3], '[$U#[$U#U<2><3><1><1><2><3>', 'debug', 1);
220-
test_jsonlab('mixed array', @savebj, {'a', 1, 0.9}, '[CaU<1>D<0.9>]', 'debug', 1);
220+
test_jsonlab('mixed array', @savebj, {'a', 1, 0.9}, '[C<97>U<1>D<0.9>]', 'debug', 1);
221221
test_jsonlab('char array', @savebj, ['AC'; 'EG'], '[SU<2>ACSU<2>EG]', 'debug', 1);
222222
test_jsonlab('maps', @savebj, struct('a', 1, 'b', 'test'), '{U<1>aU<1>U<1>bSU<4>test}', 'debug', 1);
223223
test_jsonlab('2d array', @savebj, [1, 2, 3; 4, 5, 6], '[$U#[$U#U<2><2><3><1><2><3><4><5><6>', 'debug', 1);
@@ -241,6 +241,12 @@ function run_jsonlab_test(tests)
241241
'[{U<1>iD<1.1>U<1>dSU<3>str}{U<1>iD<1.1>U<1>dSU<3>str}]', 'debug', 1);
242242
test_jsonlab('encoded fieldnames', @savebj, struct(encodevarname('_i'), 1, encodevarname('i_'), 'str'), ...
243243
'{U<2>_iU<1>U<2>i_SU<3>str}', 'debug', 1);
244+
test_jsonlab('single byte', @savebj, loadbj(['B' 65]), 'C<65>', 'debug', 1);
245+
test_jsonlab('byte 1D vector', @savebj, loadbj(['[$B#U' 3 61 62 65]), 'SU<3>=>A', 'debug', 1);
246+
test_jsonlab('optimized byte 1D vector', @savebj, loadbj(['[$B#[$U#U' 1 4 61 62 65 66]), 'SU<4>=>AB', 'debug', 1);
247+
test_jsonlab('object with byte key', @savebj, loadbj(['{' 'i' 3 'lat' 'B' -1 'i' 4 'long' 'U' 2 'i' 3 'alt' 'B' 210 '}']), '{U<3>latC<0>U<4>longU<2>U<3>altC<210>}', 'debug', 1);
248+
test_jsonlab('optimized object with byte key', @savebj, loadbj(['{$C#U' 3 'i' 3 'lat' 10 'i' 4 'long' 9 'i' 3 'alt' 240]), '{U<3>latC<10>U<4>longC<9>U<3>altC<240>}', 'debug', 1);
249+
244250
if (exist('OCTAVE_VERSION', 'builtin') ~= 0)
245251
test_jsonlab('encoded fieldnames without decoding', @savebj, struct(encodevarname('_i'), 1, encodevarname('i_'), 'str'), ...
246252
'{U<2>_iU<1>U<2>i_SU<3>str}', 'debug', 1, 'UnpackHex', 0);

0 commit comments

Comments
 (0)