dropbox/json11

Can the integer type be generalized?

seewalker opened this issue · 3 comments

I'm trying to use this json serializer with long ints and unsigned ints and such. When I try to make Json objects using the initialization syntax with these other integer types, I see errors like:

conversion from ‘long int’ to ‘const json11::Json’ is ambiguous

Is there any interest in supporting other integer types? This project uses c++11, so it would be possible to use templates along with the "integral" concept. Maybe the JsonInt class could become a JsonInt class where std::is_integal is true.

If this idea won't work with this code or the maintainers aren't interested, my project will use a version of json11 with a duplicate a lot of code and having a lot of s/Int/Long and such. If the decision of this conversation is not to use a template+concept approach, let me know if I should submit a pull request for a version of this project with extra boilerplate for other integer types.

Hi @seewalker. That kind of a templated approach to generic integers seems doable, but out of scope for json11. Support only for specific numeric types was an early design decision which is explained in a comment here: https://github.com/dropbox/json11/blob/master/json11.hpp#L16

In the interest of maximal compatibility, json11 will implicitly take the number types which it knows are compatible, and require the user to perform explicit casts in cases where truncation/rounding might be necessary.

Thanks for the answer. A remark for anybody who visits this page with the same thoughts I had: as mentioned in the link pretty much any number type can be safely cast as a double so that's the lowest-common denominator solution that makes sense.

That's true for most number types, but be cautious that it's not necessarily safe for a 64-bit (or larger) integer (because a double contains only 53 bits of precision, so would need to round), or for a long double (which would also need to round).