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

Encoding of double variables that happen to be integral #1592

Open
dwaydwaydway opened this issue Feb 4, 2025 · 3 comments
Open

Encoding of double variables that happen to be integral #1592

dwaydwaydway opened this issue Feb 4, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@dwaydwaydway
Copy link

Hi,

I'm trying to assess if I can make my application migrate from using Rapid Json to using Glaze. And I'm noticing a behavioral difference:

Let's say I have a double variable of value 10.0

double var{10.0};

If I encode var with Rapid Json, I'll get {"var": 10.0}

Whereas if I encode the following with Glaze:

struct test {
    double var{10.0};
};

I'll get {"var": 10}

Is it possible to have a compile-time option where I can get the .0 with Glaze?

@stephenberry
Copy link
Owner

Glaze, like std::to_chars, will write out doubles with the least amount of characters necessary for round-tripping. Hence 10.0 is dumped as 10.

Are you wanting to dump floating point numbers with a decimal place so that you can determine if they are integers or floating point numbers? I'm curious what is your motivation?

We could add an option that allows you to add a std::format template for how numbers are printed out. E.g. .number_format = "{:15d}". Would you want this behavior for all doubles you are serializing?

@stephenberry stephenberry added the enhancement New feature or request label Feb 4, 2025
@dwaydwaydway
Copy link
Author

dwaydwaydway commented Feb 5, 2025

Hi @stephenberry , thanks for the reply.

The motivation is that the client side of my application is doing schema validation on the json string received. And this is causing error on the client-side checker because it assumes that the var is encoded as an integer, which is not the expected type of double.

Yes it would be useful, or rather critical in my case, to have the formatting option for floating point numbers!

If you agree that this is useful and decide to implement this, would you mind sharing when can we expect this feature roughy?

Much apreciation!

@stephenberry
Copy link
Owner

I can understand your motivation and why it may be a requirement. But, I am leery of this approach, because you are relying on specific format that is outside the scope of JSON, so your program will then fail with perfectly valid JSON.

If you only needed this support in a few places then using the glz::custom wrapper would be my recommendation right now. But, if this feature is required all over your code then I would recommend sticking with Rapid Json for a while.

I don't think I'm going to do this immediately, because I'm currently trying to finalize a new approach for handling compile time options, and this will need to be another compile time option that is a little more complex because it requires a flexible string input.

It will probably be a few months before this is implemented.

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

No branches or pull requests

2 participants