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

Can't encode an empty struct array #85

Closed
goretkin opened this issue Nov 21, 2022 · 1 comment
Closed

Can't encode an empty struct array #85

goretkin opened this issue Nov 21, 2022 · 1 comment

Comments

@goretkin
Copy link

>> savejson('', repmat(struct('a', 1, 'b', 2), 0, 2), 'test.json')
Index exceeds the number of array elements. Index must not exceed 0.

Error in jdataencode>N_ (line 398)
newname=[varargin{1}.prefix name];

Error in jdataencode>@(x)N_(x,varargin{:}) (line 180)
N=@(x) N_(x,varargin{:});

Error in jdataencode>mat2jd (line 181)
newitem=struct(N('_ArrayType_'),class(item),N('_ArraySize_'),size(item));

Error in jdataencode>obj2jd (line 108)
    newitem=mat2jd(item,varargin{:});

Error in jdataencode>struct2jd (line 146)
        newitem.(names{i})=obj2jd(item.(names{i}),varargin{:});

Error in jdataencode>obj2jd (line 106)
    newitem=struct2jd(item,varargin{:});

Error in jdataencode>struct2jd (line 146)
        newitem.(names{i})=obj2jd(item.(names{i}),varargin{:});

Error in jdataencode>obj2jd (line 106)
    newitem=struct2jd(item,varargin{:});

Error in jdataencode (line 98)
jdata=obj2jd(data,opt);

Error in savejson (line 183)
    obj=jdataencode(obj,'Base64',1,'UseArrayZipSize',0,opt);
 
398 newname=[varargin{1}.prefix name];
K>> 
@goretkin
Copy link
Author

It seems like this could just be handled by widening the condition on
https://github.com/fangq/jsonlab/blob/9687d17af718af0fcc6d36beb101ef2842815ba5/jdataencode.m#L136

to apply for n != 1, but this will cause the JSON to lose the information about the field names of the empty struct array.

>> bs   = repmat(struct('a', 1, 'b', 2), 0, 2)

bs = 

  0×2 empty struct array with fields:

    a
    b

>> fieldnames(bs)

ans =

  2×1 cell array

    {'a'}
    {'b'}

is there a special value to use in the JSON to represent the empty array?

@fangq fangq closed this as completed in 9ce91fc Nov 22, 2022
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

1 participant