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

to_string? #1181

Closed
haydenflinner opened this issue Aug 1, 2018 · 5 comments
Closed

to_string? #1181

haydenflinner opened this issue Aug 1, 2018 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@haydenflinner
Copy link

  • Describe the feature in as much detail as possible.

Given a json object of any primitive type, I'd like to be able to call something like std::to_string on it, allowing me to turn it into a string without the quotes/escaping that dump would put on a string.
So far the closest I've gotten to the behavior I want is a big switch statement on the type of the json object, where I call std::to_string(json_val.get<thetype>()) in each case except string and raise an exception on non-primitive types.

So if I have the object { 'lokey' : 'hival' }, I want to be able to save the string "hival" (where the quotes ust denote the string, in memory would just be hival) and not "\"hival\"". I would like the same function to implicitly format bools and ints to strings, as well. I understand that this is asking for opinionated behavior, but I think it would be helpful for brevity if you were, for example, converting from one JSON schema to another, where the same thing has a different type in the two schema. At the least, letting me get just the string of a string value instead of the escaped version would be enough for me.

I'm very sorry if there's already something like this in the library, I looked through the documentation multiple times over the last few days before arriving at my switch statement.

Thanks for the awesome library!

@gregmarr
Copy link
Contributor

gregmarr commented Aug 1, 2018

At the least, letting me get just the string of a string value instead of the escaped version would be enough for me.

Does this do what you want? json_val.get<std::string>()

@nlohmann
Copy link
Owner

nlohmann commented Aug 1, 2018

The library does not provide an automatic stringification for all value types. As @gregmarr points out, the (unescaped) string value can be queried with .get<std::string>(). Other primitive types (null, number, boolean) need to be converted by the user.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Aug 1, 2018
@haydenflinner
Copy link
Author

@gregmarr You're right, I completely forgot that I had used that in my switch-case statement, haha.

Probably a good decision to make us do this explicitly, I've already been bitten by some of the implicitness allowed in this library and it seems like you're taking steps to move away from that, no sense introducing more :P

@nlohmann
Copy link
Owner

nlohmann commented Aug 4, 2018

@haydenflinner Do you need further assistance or can we close this issue?

@haydenflinner
Copy link
Author

Thank you!

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

3 participants