Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadmsgpack fails on empty arrays #63

Closed
stfnp opened this issue May 3, 2020 · 2 comments
Closed

loadmsgpack fails on empty arrays #63

stfnp opened this issue May 3, 2020 · 2 comments

Comments

@stfnp
Copy link

stfnp commented May 3, 2020

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.

@fangq fangq closed this as completed in 82dfdcc May 3, 2020
@fangq
Copy link
Member

fangq commented May 3, 2020

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)

@stfnp
Copy link
Author

stfnp commented May 3, 2020

That was quick, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants