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

Invalid deserialization #5

Open
wlad031 opened this issue Sep 9, 2017 · 3 comments
Open

Invalid deserialization #5

wlad031 opened this issue Sep 9, 2017 · 3 comments

Comments

@wlad031
Copy link

wlad031 commented Sep 9, 2017

Hello

I have the following code:

class A {
public:
    float f;

    A() {}

    A(float f) :
            f(f) {}
};

namespace json_dto {
    template<typename JSON_IO>
    void json_io(JSON_IO& io, A a) {
        io
        & json::mandatory("f", a.f);
    }
}

After

auto v1 = A(10.8);
auto v = json_dto::to_json(v1);
std::cout << v << std::endl;
auto v2 = json_dto::from_json<A>(v);
std::cout << json_dto::to_json(v2) << std::endl;

I get
{"f":10.800000190734864}
{"f":1041031353073664.0}

Could you please help, where is my mistake?

@ngrodzitski
Copy link
Owner

ngrodzitski commented Sep 9, 2017 via email

@wlad031
Copy link
Author

wlad031 commented Sep 9, 2017

Thank you, it was my fault.

One more question: is there a way to set precision for float and double values (for getting {"f":10.8})?

@ngrodzitski
Copy link
Owner

in a current version there is no way to set precision. A cuple of times I've faced such issue I simply do such rounding myself.
But seems like it is a not a rare cese, so I'll think of a some kind of format option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants