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

very basic: fetching string value/content without quotes #853

Closed
rnsanchez opened this issue Dec 3, 2017 · 7 comments
Closed

very basic: fetching string value/content without quotes #853

rnsanchez opened this issue Dec 3, 2017 · 7 comments
Assignees
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@rnsanchez
Copy link

This is kind of a usage issue and not a bug/feature request. I could be overlooking something trivial as I am very new to the library; if this is the case, please point in the right direcion/documentation.

  • What is the issue you have?

When printing/using a value from a string element, the string is quoted:

$ ./test 
"value"
  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
    #include <iostream>
    #include "json.hpp"
    using json = nlohmann::json;
    int main(int argc, char *argv[])
    {
        json j = R"({"one": "value"})"_json;
        std::cout << j["one"] << '\n';
    }
  • What is the expected behavior?

Printing/fetching value as opposed to "value".

I understand that this could be a simple matter of picking out a substring, but searching in the documentation and existing issues (closed and open), I didn't find an idiomatic way of simply extracting the original string (no quotes as quotes are not escaped in the JSON blob).

GCC (7.2.0).

  • Did you use a released version of the library or the version from the develop branch?

Git master, cc937de

@rnsanchez
Copy link
Author

One thing I just noticed is that this could be a non-issue at all.

In another code I'm working on, checking with gdb:

(gdb) p d
$1 = "2017-11-30"
(gdb) p d.c_str()
$3 = 0x63c958 "2017-11-30"
(gdb) x/12xc d.c_str()
0x63c958:	50 '2'	48 '0'	49 '1'	55 '7'	45 '-'	49 '1'	49 '1'	45 '-'
0x63c960:	51 '3'	48 '0'	0 '\000'	0 '\000'

There weren't quotes. Only when std::cout'ing them.

I must be overlooking something very obvious.

@gregmarr
Copy link
Contributor

gregmarr commented Dec 4, 2017

The type of j["one"] is json, not std::string, so when you print it, it's quoted. If you want the bare string, you need to specify that: j["one"].get<std::string>().

@nlohmann
Copy link
Owner

nlohmann commented Dec 4, 2017

You could also write

std::string s = j["one"];

for an implicit conversion which avoids you writing .get<std::string>().

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Dec 4, 2017
@rnsanchez
Copy link
Author

Thank you both for the clarification.

As a suggestion, it could be useful to include such an example in the usage tutorial, as std::cout'ing numbers will seem to "just work". I'm glad @gregmarr explained about the type that actually gets passed to cout (possibly what I was overlooking?).

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

using json = nlohmann::json;

int main(int argc, char *argv[])
{
	json j = R"({"one": "value", "two": 1.2345})"_json;

	std::cout << j["one"] << '\n';
	std::cout << j["one"].get<std::string>() << '\n';
	std::cout << j["two"] << '\n';
}

Output:

"value"
value
1.2345

@nlohmann
Copy link
Owner

nlohmann commented Dec 6, 2017

I added a clarification in the README file.

@nlohmann nlohmann closed this as completed Dec 6, 2017
@nlohmann nlohmann self-assigned this Dec 6, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone Dec 6, 2017
@Anthony-J-Garot
Copy link

I find this notation a bit ugly:

j["one"].get<std::string>()

So I have been doing this:

std::string(j["one"])

As a suggestion, would you consider adding .to_string() or .as_string(), which arguably is nicer than .get<std::string>() ?

@nlohmann
Copy link
Owner

I would actually avoid adding such a function, because it the API is already bloated. You can write such a function yourself as it would not need to access private members.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants