You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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:Trying to load this file with
loadmsgpack('test.msg')
fails with the following error: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 ofloadmsgpack.m
fromif(isnumeric(out{1}))
toif(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
andsavejson
can actually read empty arrays without problems (they map to a 0x1 cell) and also serialize them back as empty arrays. The functionsavemsgpack
however serializes empty arrays asnull
instead of[]
. And that might be the reason whyloadmsgpack
doesn't expect empty arrays and can't handle them.The text was updated successfully, but these errors were encountered: