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

One column table cannot be unserialised after serialisation #66

Closed
aardwolf-ge opened this issue Jul 7, 2020 · 2 comments
Closed

One column table cannot be unserialised after serialisation #66

aardwolf-ge opened this issue Jul 7, 2020 · 2 comments

Comments

@aardwolf-ge
Copy link

aardwolf-ge commented Jul 7, 2020

This code recreates the problem
v=[ 1; 2 ]
tab= array2table(v)
str=savejson('',tab)
jv=loadjson(str)

producing the error 'The VariableNames property must contain one name for each variable in the table.'

the json object created by savejson is (in compact form)
'{"_TableCols_":["v"],"_TableRows_":[],"_TableRecords_":[[1,2]]}'

when changed to:

'{"_TableCols_":["v"],"_TableRows_":[],"_TableRecords_":[[1],[2]]}'

it works properly, so the problem seems to be on the save side.

@aardwolf-ge aardwolf-ge changed the title One column table containing character data cannot be unserialised after serialisation One column table cannot be unserialised after serialisation Jul 7, 2020
@aardwolf-ge
Copy link
Author

aardwolf-ge commented Jul 7, 2020

this array:
[ 1; 2 ]
is serialised to this json object:
'[[1],[2]]'
while this cell array:
{ 1; 2 }
is serialised to this json object:
'[[1,2]]'
so probably the problem lies in function txt=cell2json(name,item,level,varargin)

@fangq fangq closed this as completed in ce40fdf Jul 8, 2020
@fangq
Copy link
Owner

fangq commented Jul 8, 2020

this is now fixed.

savejson encodes 4 types of arrays - numerical arrays, string arrays, cell arrays and struct arrays. Currently, only numerical arrays have explicitly defined rules on serialization of N-dimensional arrays (see my JData specification)

https://github.com/fangq/jdata/blob/master/JData_specification.md#direct-storage-of-n-d-arrays

while other arrays are not clearly defined. The result you saw on encoding cell arrays is a result of this ambiguity.

I just made the above commit to make two things clear:

  1. the serialization of cell arrays now follow the same row-major order as the numerical arrays, and
  2. jsonlab now supports ND cell array while previously only supports up to 2D

please test and let me know if you see any side effect of this fix. I've ran all unit-testing and example scripts, and they seem to work ok.

forgot to say - if there is a strong consensus that string arrays and struct arrays should be processed in a similar way, I will port this change to other relevant encoding functions.

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