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

json stringify #1532

Closed
eladpotok opened this issue Mar 20, 2019 · 12 comments
Closed

json stringify #1532

eladpotok opened this issue Mar 20, 2019 · 12 comments
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)

Comments

@eladpotok
Copy link

The library works very good for my needs. But there is an addition that could be very helpful.
I need a way to stringify the result from the json.dump() function.

For every " or ' that appears in the json, need to be escaped -- " or '.

Thanks,

@nlohmann
Copy link
Owner

Could you please provide an example? Like a JSON value, the result of dump and what you would expect?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Mar 20, 2019
@eladpotok
Copy link
Author

eladpotok commented Mar 20, 2019

Yes.
In case I have this json:

{ "key": "text" , "data": "hello world!" }

I want it to became:

"{ \"key\": \"text\" , \"data\": \"hello world!\" }

and the same for '' to became '\'.
in other words; the json should became to string so if I print it, I will see the special characters .

@nlohmann
Copy link
Owner

I see. But the resulting string would not be valid JSON text, so adding the backslashes would not make sense for this library.

What you could try (though it would not touch ') would be. This:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main() {
    json j = R"( { "key": "text" , "data": "hello world!" } )"_json;
    std::cout << json(j.dump()).dump() << std::endl;
}

result:

"{\"data\":\"hello world!\",\"key\":\"text\"}"

@nlohmann nlohmann added solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) and removed state: needs more info the author of the issue needs to provide more details labels Mar 20, 2019
@nlohmann
Copy link
Owner

I think adding the backslashes is out of scope of the library, so I add the "won't fix" label.

@gregmarr
Copy link
Contributor

Sounds like you want this:

json j = ....;
std::string s = j.dump();
json j2(s);  // Create a new JSON object containing the dumped string.
std::string quoted = j2.dump(); // Dump the new JSON object, which quotes the string.

@gregmarr
Copy link
Contributor

Now that I read the previous comments better, this looks to be the same basic idea that @nlohmann posted, including not quoting single quotes.

@eladpotok
Copy link
Author

eladpotok commented Mar 20, 2019 via email

@nlohmann
Copy link
Owner

This function is used internally to escape certain characters as required by

It is called by the dump function.

@nlohmann
Copy link
Owner

@eladpotok Do you need further assistance on this issue?

@jaredgrubb
Copy link
Contributor

I actually think providing escape_string as a new free-function that people could use for their own purposes (like this one) could be a handy feature.

@gregmarr
Copy link
Contributor

gregmarr commented Mar 28, 2019

Is that free function any more than this? (I know this is completely unconstrained, and doesn't guarantee strings, but just for the basic idea).

template<typename T> auto escape_string(T &&str) {
    return nlohmann::json{std::forward<T>(str)}.dump();
}

@jaredgrubb
Copy link
Contributor

Yes, it's that, but avoids the mandatory copy (in the const& case). Also, you could argue whether or not the surrounding quote marks should be included or not ("hello" or hello). It's literally just making the "dump_string" function a non-private free function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

No branches or pull requests

4 participants