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

Escaped characters in keys and values #1

Closed
cmaumet opened this issue Mar 29, 2017 · 4 comments
Closed

Escaped characters in keys and values #1

cmaumet opened this issue Mar 29, 2017 · 4 comments

Comments

@cmaumet
Copy link

cmaumet commented Mar 29, 2017

Hi @gllmflndn! The following example (from our discussion at: incf-nidash/nidmresults-spm#40 (comment)):

>> json = containers.Map('nidm_DesignMatrix/prov:atLocation','./DesignMatrix.csv');
>> jsonwrite(json)

returns:

ans =
 {"nidm_DesignMatrix\/prov:atLocation":".\/DesignMatrix.csv"}

Is there any particular reason for the slashes to be escaped? I was hoping we could get:

{"nidm_DesignMatrix/prov:atLocation": "./DesignMatrix.csv"}

I am happy to investigate this and send a PR but I would like to make sure I understand when the escapes are needed.

Thank you in advance!

@gllmflndn
Copy link
Owner

From http://json.org/, it is my understanding that '/' should have a backslash escape. Both pass validation through JSONLint though. It seems to be a frequently discussed topic, e.g.:

It's all taking place there:

@cmaumet
Copy link
Author

cmaumet commented Mar 30, 2017

Thank you @gllmflndn: I was not aware of this issue! From the thread you linked to, my understanding is that both notations are valid, and that:

 {"nidm_DesignMatrix\/prov:atLocation":".\/DesignMatrix.csv"}

is equivalent to

 {"nidm_DesignMatrix/prov:atLocation":"./DesignMatrix.csv"}

In this document "control characters" are explicitly defined as U+0000 through U+001F which does not include / (U+002F).

Do you think it would make sense to use the non-escaped version by default? Or create an option to espace only if mandatory?

@gllmflndn
Copy link
Owner

JSON specification(s) are sometimes hard to follow, see:
http://seriot.ch/parsing_json.php#1
I just checked and MATLAB's jsonencode does not escape forward slashes so, for compatibility, I will just do the same here. That will solve the issue for you: how come it is a problem for you though, given that a reader would un-escape the escaped characters?

>> jsonread(jsonwrite(struct('a','./hello')))
ans = 
  struct with fields:
    a: './hello'

@gllmflndn
Copy link
Owner

Forward slashes are not escaped any more, see 3a81c3d, and this behaviour is now compatible with jsonencode.
It would be possible to add an option as suggested in pull request #2 but implementation should instead use optregistry to minimise changes throughout the file - low priority though as I hope current version fixes the issue for 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