loadmsgpack fails on empty arrays
stfnp opened this issue · 2 comments
The function loadmsgpack
fails on files that contain an empty array. I've attached an example file for testing here. It's a MessagePack file with a single field "test" that contains an empty array. The equivalent JSON would look like this:
{
"test": []
}
Trying to load this file with loadmsgpack('test.msg')
fails with the following error:
Index exceeds the number of array elements (0).
Error in loadmsgpack>parsearray (line 225)
if(isnumeric(out{1}))
Error in loadmsgpack>parse (line 90)
[obj, idx] = parsearray(len, bytes, idx+1, varargin{:});
Error in loadmsgpack>parsemap (line 243)
[out.(encodevarname(char(key))), idx] = parse(bytes, idx, varargin{:});
Error in loadmsgpack>parse (line 85)
[obj, idx] = parsemap(len, bytes, idx+1, varargin{:});
Error in loadmsgpack (line 39)
[obj, idx] = parse(uint8(bytes(:)), 1, opt);
It seems like the parsearray
sub-function doesn't expect an empty array and fails when trying to access the first element. Inserting a simple length check, i.e. changing line 225 of loadmsgpack.m
from if(isnumeric(out{1}))
to if(len ~= 0 && isnumeric(out{1}))
seems to fix the problem.
However, I think there is also an underlying inconsistency in the handling of empty arrays by JSONLab. I've noticed that loadjson
and savejson
can actually read empty arrays without problems (they map to a 0x1 cell) and also serialize them back as empty arrays. The function savemsgpack
however serializes empty arrays as null
instead of []
. And that might be the reason why loadmsgpack
doesn't expect empty arrays and can't handle them.
thanks for catching this. now it is fixed.
The behavior differences between loadjson/savejson and loadmsgpack/savemsgpack can be adjusted by appending 'simplifycell',1
to the loadjson
command.
loadjson(savejson('',a),'simplifycell',1)
That was quick, thank you!