3

When my program saves something in json like this:

 json_object_object_add(jObj_my, "cats/dogs", json_object_new_double(cats/dogs));

the result in the .json file is:

"cats\/dogs" : some_double_number

How can i avoid it to print "\/" instead of "/"?

alk
  • 69,737
  • 10
  • 105
  • 255
user3717434
  • 215
  • 4
  • 19
  • why -1 ? I searched it and couldn't find a solution. – user3717434 Jun 16 '17 at 11:37
  • 1
    May be try to protect the character `'/'` by putting `'\'` behind it. Not sure it could work though. As for the downvote, it might be because you are posting pseudo code. Try to provide a [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve) – Badda Jun 16 '17 at 11:52
  • @Badda, i tried escaping it but compiler gives warning and prints it the same way. – user3717434 Jun 16 '17 at 11:58

1 Answers1

8

The json-c library's code in its GitHub repository has a flag to make escaping of / optional.

If you do not want the generated string to escape this, use the JSON_C_TO_STRING_NOSLASHESCAPE flag, like this:

#include <stdio.h>
#include <json.h>

int main(int argc, char **argv)
{
    json_object *my_string;

    my_string = json_object_new_string("/foo/bar/baz");
    printf("my_string=%s\n", json_object_get_string(my_string));
    printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
    printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
    json_object_put(my_string);

    return 0;
}

example adapted from https://github.com/json-c/json-c/blob/master/tests/test1.c#L155

Saving this in slashtest.c, compiling it, and running it produces:

$ gcc -Wall slashtest.c -L/usr/local/lib -l:libjson-c.a -I/usr/local/include/json-c
$ ./a.out
my_string=/foo/bar/baz
my_string.to_string()="\/foo\/bar\/baz"
my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"

Escaping / in JSON is legal and arguably may be useful, see this post about it: JSON: why are forward slashes escaped?

Note that this flag was added to the library's code in 2015, but that, somehow the change didn't make it in the latest current json-c-0.12.1 release made in Jun 7, 2016. I am unsure why.

So to use it, you will have to get the code from GitHub, and compile it.

Tardis
  • 465
  • 2
  • 10
  • thanks, but when i compile it says JSON_C_TO_STRING_NOSLASHESCAPE is not defined. I checked also json-c documentation(i have version 0.12) and couldn't find this macro. There are some other macros defined but non of them is for escaping. – user3717434 Jun 16 '17 at 20:43
  • @user3717434: it is defined in the [library's code on GitHub](https://github.com/json-c/json-c/blob/818156f6f79c1c34e6e596798822846bcaea7b9f/json_object.h#L70), not in the 0.12 release, with the code changes in [json_object.c](https://github.com/json-c/json-c/blob/e76ea3777260ddc9d19285460ad250ca6de17769/json_object.c#L106) needed for it to work. So to use it, you need to get the whole code of the library from GitHub, compile and install the library using the README.md instructions to replace your 0.12 installation, and then try compiling your code with this flag. – Tardis Jun 17 '17 at 08:40
  • with source code from GitHub it works but it prints something worse :) `json_object *mystr = json_object_new_string("cats/dogs"); json_object *myObj = json_object_new_object(); json_object_object_add(myObj, json_object_to_json_string_ext(mystr, JSON_C_TO_STRING_NOSLASHESCAPE),json_object_new_double(5.6));` it prints `"\"cats\/\/dogs\""` – user3717434 Jun 17 '17 at 11:58
  • I checked-out the code from GitHub and tested my example. I does produce a non escaped slash as a result of `json_object_to_json_string_ext()`. I updated my answer to show that. In your example, if you want "cats/dogs" to be the key and the double 5.6 to be the value, just do: `json_object *myObj = json_object_new_object(); json_object_object_add(myObj, "cats/dogs", json_object_new_double(5.6)); printf("res = %s\n", json_object_to_json_string_ext(myObj, JSON_C_TO_STRING_NOSLASHESCAPE));` and it will print: `res = {"cats/dogs":5.5999999999999996}` – Tardis Jun 19 '17 at 09:43
  • Thanks for answer, it was helpful to me. I was adding base64 encoded string into json file and i faced this problem. With flag suggested in your answer helped me to get rid of the problem. json_object *jfile = json_object_new_object(); json_object *jb64 = json_object_new_string(base64_encoded_signature) json_object_object_add(jfile, "sign", j64); json_object_to_file_ext(filename, jfile, JSON_C_TO_STRING_NOSLASHESCAPE); – Sujal Sheth Aug 07 '20 at 07:13