You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
@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
Activity
gregmarr commentedon Aug 1, 2018
Does this do what you want?
json_val.get<std::string>()nlohmann commentedon 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.haydenflinner commentedon Aug 3, 2018
@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 commentedon Aug 4, 2018
@haydenflinner Do you need further assistance or can we close this issue?
haydenflinner commentedon Aug 7, 2018
Thank you!