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

Key gets modified if it contains "\" #395

Closed
singhgaurav1302 opened this issue Dec 29, 2017 · 3 comments
Closed

Key gets modified if it contains "\" #395

singhgaurav1302 opened this issue Dec 29, 2017 · 3 comments

Comments

@singhgaurav1302
Copy link

singhgaurav1302 commented Dec 29, 2017

json_object_object_add(object, "abc\def\ghi", value);
::std::string data(json_object_to_json_string(object));
cout << data << endl;

if the key contains "\", the content of key is getting changed to "abc\/def\/ghi" which is not intended.

@hawicz
Copy link
Member

hawicz commented Dec 29, 2017

Your code can't possibly compile cleanly in the form that you listed it here: \d and \g are undefined escape sequences. If you ignore the compiler warnings, and try to use it anyway, then the backslashes are just dropped, and you end up with a string that just contains "abcdefghi".

On the other hand, if you have forward slashes in the string, then the inclusion of the backslashes in the result from json_object_to_json_string() is the intended output.
Please see json_object_to_json_string_ext() and JSON_C_TO_STRING_NOSLASHESCAPE if you want to change that behavior.

@hawicz hawicz closed this as completed Dec 29, 2017
@singhgaurav1302
Copy link
Author

Hello,
Sorry for backward slashes, actually I am dealing with forward slashes only as you mentioned.
But the version of json we are using does not have JSON_C_TO_STRING_NOSLASHESCAPE.
As per the file json_c_version.h I am using JSON_C_VERSION "0.12.99" and i guess fix to escape forward slashes was introduced in later versions.

Is there any workaround in the version I am using.

@hawicz
Copy link
Member

hawicz commented Jan 3, 2018

That flag was added in commit 316da85, back at the end of 2015. It sounds like you're working off of a very old development version of json-c (somewhere between 2014/04/10 and 2015/11/28) and you should upgrade.
You could try backporting that change to your version, or just hack up your version of json-c to always skip the escaping of forward-slashes, but you'd be missing out on hundreds of other changes and bug fixes, so I wouldn't recommend it.

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