neon icon indicating copy to clipboard operation
neon copied to clipboard

Big integers should be returned as string (~JSON_BIGINT_AS_STRING)

Open JanTvrdik opened this issue 11 years ago • 8 comments

I think that it is better to return large integers as string instead of casting them to float.

What do you think? Any tips, how to efficiently decide whether string contains integer larger than MAX_INT?

JanTvrdik avatar Apr 09 '14 13:04 JanTvrdik

Any tips, how to efficiently decide whether string contains integer larger than MAX_INT?

Some cute regexp?

Majkl578 avatar Apr 09 '14 15:04 Majkl578

This is what I have now – https://github.com/JanTvrdik/neon/commit/c302826d18bc3c3d2fb731cf908aca10498fce86

JanTvrdik avatar Apr 09 '14 15:04 JanTvrdik

What to do with numbers such as 0x123456789123456781345678913? I would prefer to return them as decimal string however I'm not aware of any efficient solution to perform the conversion.

JanTvrdik avatar Apr 09 '14 18:04 JanTvrdik

What about E_USER_WARNING on big number? To force user enclose it in quotes as a string. JSON has the JSON_BIGINT_AS_STRING only for interoperability between 32-bit and 64-bit systems.

milo avatar Apr 09 '14 19:04 milo

What to do with numbers such as 0x123456789123456781345678913? I would prefer to return them as decimal string however I'm not aware of any efficient solution to perform the conversion.

The only sane way I know in PHP is the GMP extension.

milo avatar Apr 09 '14 19:04 milo

What about E_USER_WARNING on big number? To force user enclose it in quotes as a string.

Please, don't. JSON NEON is not something you always manually construct in the application, it's rather given from elsewhere, really not something you control directly (thus should not trigger an error).

Majkl578 avatar Apr 09 '14 19:04 Majkl578

Please, don't. JSON is not something you manually construct

  1. We're talking about NEON here 2) JSON RFC allows a JSON parser to restrict the range of numbers it can handle

But still agreed, warning is the solution I would like to avoid.

JanTvrdik avatar Apr 09 '14 19:04 JanTvrdik

There is no sane way how to handle big numbers or have you forgotten about very large decimals?

It should be string representation of given number or native PHP representation if possible. This is the simple solution

We can add custom number parser into mix as a fallback or parameter with default set to sane parser. Quite easy, hard problem of what to do with numbers will be solved by user.

In my magic ball I see this heading to custom number parser with default implementation that handles numbers of all colours and sizes.

mishak87 avatar Apr 10 '14 01:04 mishak87