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

Some strings returned as "string" class instead of "char" in recent matlab versions #50

Closed
jobh opened this issue Dec 12, 2018 · 4 comments

Comments

@jobh
Copy link

jobh commented Dec 12, 2018

Arrays of strings are parsed by matlab's eval, and since they use double quotes they end up as string instead of char starting from 2017a.
A simple fix is to convert them back at the end, something like the following in loadjson.m (after parsing):

% The eval in parse_array creates strings, not char arrays, in R2017a+. Convert
% back (in try block, because the method probably doesn't exist in earlier versions)
try
    data = controllib.internal.util.hString2Char(data);
catch
end
@fangq
Copy link
Member

fangq commented Feb 8, 2019

hi @jobh, thanks for the comment. can you give me a short example showing this issue? I am also curious if adverse-effect if we leave it as "string"?

@pch-envision
Copy link

pch-envision commented Mar 12, 2019

Since char and string have different behaviors in Matlab, you could encounter adverse effects in a downstream program which uses the loaded JSON object. A simple example would be a function which checks the input type using ischar - this would return zero if the variable in question is a string.

Since JSON does not permit single quote strings, the only way to resolve string vs. char would be to encode the type with the variable. Note that the neither built-in jsondecode nor mps.json.decode differentiate between char/string; everything is converted to char on deserialization.

@fangq
Copy link
Member

fangq commented Mar 12, 2019

right now, I don't see a problem storing strings as char array in JSON since there is no difference in JSON's specification. I do see a problem when one tries to store a string array, jsonlab can raise an error, but I think it should be fixed now after this commit by treating string arrays as a cell.

https://github.com/fangq/jsonlab/commit/ed2645e558c510b806c738de8b9dc71096a708ae

@fangq fangq closed this as completed in a8fde38 Mar 13, 2019
@fangq
Copy link
Member

fangq commented Mar 13, 2019

fixed. A user now can use ParseStringArray=1 or 0 to choose parsing string array or convert to char array. default is covert to char array (ParseStringArray=0). Try

jdata=loadjson('{"a": ["a1", "a10", 2, "a100"]}')
jdata = 

  struct with fields:

    a: {'a1'  'a10'  [2]  'a100'}

and

jdata=loadjson('{"a": ["a1", "a10", 2, "a100"]}','ParseStringArray',1)

jdata = 

  struct with fields:

    a: {["a1"]  ["a10"]  [2]  ["a100"]}

an alternative is to disable the fastarrayparser using

jdata=loadjson('{"a": ["a1", "a10", 2, "a100"]}','FastArrayParser',0)

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

3 participants