-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Storing floats, and round trip serialisation/deserialisation diffs #572
Labels
kind: question
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
Comments
You can set the value type for floating-point numbers to #include "json.hpp"
using myjson = nlohmann::basic_json<std::map,
std::vector,
std::string,
bool,
std::int64_t,
std::uint64_t,
float>;
int main()
{
myjson test;
test["float_value"] = 0.1f;
test["double_value"] = 0.1;
std::cout << test << std::endl;
} Output: {"double_value":0.1,"float_value":0.1} |
That's an excellent suggestion - i'm not needing to mix double and floats so that should work fine for me. I'll give it a go. |
nlohmann
added
the
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
label
May 3, 2017
If you need further assistance, just let me know. |
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
I'm interested in knowing what the best advice is for representing floats in json format - as far as I am aware, the spec includes double support, but not floats.
If I add a float value to a json container, and serialise it, the result gets extended to double, and hence the serialised representation is not what you would expect if floats were supported directly, but the API makes them look like they are a 1st class datatype, so we see stuff like this:
The reason this causes me issues is that I want to round trip some configuration - read floats parameters from json files, and test that if I were to serialise them, I get out what went in. I could carefully convert my floats to doubles myself, rather than relying on the assignment operator (and hence pass in only doubles which will be serialised correctly), but i'm wondering if there is a different/better way of achieving this?
The text was updated successfully, but these errors were encountered: