-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Method to get string representations of values #642
Comments
I understand the need, but I'm not sure we can modify the behavior of We could add a templated method But this can add confusion (remembering which of |
That sounds reasonable.
What if the new template method is named |
That seems correct. It'd be great if you could experiment a bit with this, don't hesitate to open a PR! We shall discuss this in more depth, with @nlohmann and others too. |
The problem is that json j = "{\"field\": 1234}"_json;
std::string s = j["field"].get_as<std::string>(); would work but code like json j = "{\"field\": \"1234\"}"_json;
int i = j["field"].get_as<int>(); would not. If we take the I've just looked up the |
Indeed, the main issue I see if we go down that road, is that we must handle all cases ( I don't know what the JSON standard says about conversions, and @nlohmann knows a lot more about it than me. |
This seems like a perfectly reasonable and simple helper function for a user to implement if they need this functionality. I don't know that it's important enough to put it in the main library, as it seems like a pretty specific need.
|
@gregmarr Thanks, I'll go with that for now. |
Note that dump also escapes characters in the string. I agree with @theodelrieu that a full-fledged "convert anything to anything" function would add unnecessary complexity to the library that makes it simpler to use by a few, but also harder to maintain and test. As @gregmarr pointed out, there is also no need for a library function as you can implement your own getter. |
Anything to be done here? |
any news for a get_as ? |
@nlohmann A similar use case. Is it possible to store an integer/float as string instead of using std::stod/stoll for conversion (maybe for all interger/floats present in json string)?
It is needed because string to double conversion using stod cause precision loss. |
There is no precision loss when the number can be represented as double. See https://json.nlohmann.me/features/types/number_handling/ |
Yes, the issue is when number can't per represented precisely as double.
|
No, this is not possible. |
I agree with rnehra01 that this function is needed when we handle a very large double while the conversion will lose precision. |
@nlohmann such possibility still should be possible. If my json contains invalid double value e.g. Not to mention that some bad API in case it wont fit in double will replace it by string. So api isn't stable with types. I know edge case. In general great lib. |
That should throw a runtime error. I think the SAX API is the only way to do it at the moment and yes – it is quite involved. |
I've come across occasions where I needed to "stringify" a single value.
I know that there is
dump()
to serialize the whole object but it doesn't work as I would expected it (string values are enclosed in double quotes). Actually I knowdump()
is not meant for representing a single value as string that's why I want to suggest to add a method responsible for converting the value to a string.My own implementation that I'm currently using looks like following:
So, basically I want code like this to work.
Could you imagine having such a method?
Maybe it is possible to specialize the
get()
method to get such a behaviour?The text was updated successfully, but these errors were encountered: