-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
object.dump gives quoted string, want to use .dump() to generate javascripts. #826
Comments
I am not sure whether producing non-compliant JSON text is in the scope of this library. However, I do not fully understand your use case. Could you please provide the exact desired output for the JSON value {
"some_object": {
"happy": true,
"pi": 3.141
}
} |
As JSON, I'd like it as it is - in C++ land, I'd like a string representation of the object without the surrounding quotes, so that I can inject it into the last %s here: boost::format("var %s = %s.%s(%s)") I am building a JavaScript based on JSON data. |
So how would the JSON above look like? |
Sorry for inflicting confusion here! The JSON is something like this:
And I'd like to generate the following string as painlessly as possible, before I write it to some_script.js: var returned_value = KnownThing.named_method({ "some_object": {"happy": true, "pi": 3.141 }}); |
So you want {
"some_object": {
"happy": true,
"pi": 3.141
}
} to become { "some_object": {"happy": true, "pi": 3.141 }} ? |
Nono, Indentation is not an issue or concern here. When I call dump, I'd like I'm really struggling to make this clearer! |
How did you parse the JSON text? Did you use |
It is impossible for the code you provided to produce the string |
I've worked around it now, because I obviously can't have random symbols in the JSON (variables), so I'm representing var references as strings, and doing a search, so I'm no longer using |
Any news on this? |
I have a similar requirement. Keep the keys unquoted optionally. This can be done by passing a flag to the |
Without quotes, |
Hi Niels, I've been meaning to get back to you, apologies for not doing so sooner. In the end I traversed the values as suggested, and probably ended up with better code for what I was trying to achieve - essentially writing a script where blocks from the JSON are args to the methods (but didn't want to parse the JSON str on the other side of the JS to make the object). I moved the goalposts by changing the API I was targeting to methods accepting individual args instead of objects, to make things easier. I do think it's a bit wierd that the dump() method gives you escaped quotes though, as I've never seen a json file where the first character was a " - always { or [, but its fairly likely you've seen more exotic json that I. Quote weirdness I can deal with, and that aside, this library is brilliant, and I am grateful for it :) |
We only escape quotes inside strings, and this is required by the JSON specification, see This is not weird, it is the only correct way to produce a valid JSON text. |
Perhaps you can use something along these lines.
|
You could simplify this to code like std::string to_string(const nlohmann::json& j)
{
switch (j.type())
{
// avoid escaping string value
case value_t::string:
return j.get<std::string>();
case value_t::array:
case value_t::object:
return "";
// use dump() for all other value types
default:
return j.dump();
}
} |
I've been having similar issues. I see this question being asked a few times, so didn't wish to open another ticket. This seemed like the appropriate place to ask (if there's still life here). Full example code
outputs
The string variables have
I've tried to replace |
Just don't call dump, so the value remains of type json. It is then properly escaped. |
@nlohmann , oh my gosh. Perfect. Thank you so much. |
Feature Request
First up, amazing library. Love it. Definitely going to play well with V8 and V8PP, so well done.
What is the issue you have?
My issue is that j.dump() returns a string in quotes for objects, and I have no way to disable this. I'd like to .dump() something I can build strings with using boost::format without having to write my own dequoting method and suddenly thinking about complexity of string ops. That's your job! ;P
Please describe the steps to reproduce the issue. Can you provide a small but working code example?
Simplifying to Pseudo-esque code:
What is the expected behavior?
I don't want the quotes on my string-dumped object (others probably do).. I'd like dump to be extended with default parameter bool bQuoted=true so I can set it false.
auto formatted = boost::str(matic% j.at("some_object").dump(false)); //formatted == "var fn = function() { return <some_object_here>; }".
// no quotes
And what is the actual behavior instead?
I get the quotes, see above. see line 8505 in the header of 2.11
Which compiler and operating system are you using? Is it a supported compiler?
MSVC 2015 on Win10 Pro
Did you use a released version of the library or the version from the
develop
branch?If you experience a compilation error: can you compile and run the unit tests?
I'm using 2.11 from the releases page
Describe the feature in as much detail as possible.
make quotes surrounding dumped objects an option
Include sample usage where appropriate.
auto str = j.dump(-1, false);
The text was updated successfully, but these errors were encountered: