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

Storing floats, and round trip serialisation/deserialisation diffs #572

Closed
cesaref opened this issue May 1, 2017 · 3 comments
Closed

Storing floats, and round trip serialisation/deserialisation diffs #572

cesaref opened this issue May 1, 2017 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@cesaref
Copy link

cesaref commented May 1, 2017

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:

nlohmann::json test;

test["float_value"] = 0.1f;
test["double_value"] = 0.1;

std::ostringstream oss;

oss << test;

ASSERT_EQ(oss.str(), "{\"double_value\":0.1,\"float_value\":0.100000001490116}");

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?

@nlohmann
Copy link
Owner

nlohmann commented May 1, 2017

You can set the value type for floating-point numbers to float like this:

#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}

@cesaref
Copy link
Author

cesaref commented May 1, 2017

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 nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 3, 2017
@nlohmann
Copy link
Owner

nlohmann commented May 5, 2017

If you need further assistance, just let me know.

@nlohmann nlohmann closed this as completed May 5, 2017
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

2 participants