You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Treating it as a string would violate the JSON specification as far as I can tell and once you reserialize, you'd end up with "3879..." (a string value) instead of 3879 (an integer value). JavaScript and some C++ parsers might convert it to a floating-point number but this is not ideal either since it is a lossy process (you won't be able to retrieve the original integer). We could map it to a "big integer" class of some kind... but that's inconvenient for the users.
What is your purpose with such a document?
You can't, portably, represent such larger integers in C/C++ on any system I know of.
It can be decimal128 numbers with up to 34-digit mantissas, bank account numbers or IDs in decimal representation, etc.
Packing them to a string is one of safest options. Some implementations that just parse the input to a big number can be vulnerable on a malicious input.
Packing them to a string is one of safest options.
Yes. I would still like to have them flagged as "bigint" or something and not stored as strings per se.
So we could have a type "bigint" that actually returns a string when you query the value. It would be up to the user to figure out what do with the string.
Yes. I would still like to have them flagged as "bigint" or something and not stored as strings per se.
So we could have a type "bigint" that actually returns a string when you query the value. It would be up to the user to figure out what do with the string.
It would be useful to be able to direct simdjson to treat all numbers like that (double and integer) regardless of their actual precision in the input with some compile-time flag.
A common use case that comes up a lot when dealing with JSON libraries is a JSON market data stream of cryptocurrency exchanges; some of them send the price/quantity as doubles instead of strings and you want to be able to parse that directly into a decimal class without losing precision (since getting the decimal precision correct with currency data is critical). RapidJSON for example has an optional parser flag kParseNumbersAsStringsFlag which does exactly this.
Yes. Offering the option of parsing numbers as strings is entirely doable.
Is this something that one could temporarily hack locally with small modifications to the master version of simdjson until official support is available, or does it require some fundamental internal changes? It's the thing we miss most about RapidJSON when switching to simdjson.
Actually, I think raw_json_token() provides most of the value for me. I can make an informed decision, how to parse the number if we get it from specific API.
What your library could provide is faster parsing because it's known to be fast to parse other things. The question of what you can provide to parse the class that I will implement tomorrow is non-trivial.
One thing that comes to mind is you can pre-parse things like sign, integer and fractional part, exponent part(because you can probably do that for doubles anyway) and provide them as strings and as integers.
Also, you can make an API a bit nicer for these cases by allowing to get arbitrary types if they provide some API (or registered sonehow). So that I can write something along the lines of static_cast<MyDecimal>(json) or json.get_number<MyDecimal>()
Also, you can make an API a bit nicer for these cases by allowing to get arbitrary types if they provide some API (or registered sonehow). So that I can write something along the lines of static_cast(json) or json.get_number()
In standard C++, we have 64-bit, 32-bit, 16-bit and 8-bit integers and well as 64-bit, 32-bit, and 16-bit floating-point integers. None of them can represent the value provided.
We can try with a 64-bit integer (uint64_t). The number233897314173811950000 which is 0xcadfa3000000001b0, is much larger than what an unsigned 64-bit integer can store, which is 0xffffffffffffffff, so no luck there. There are 128-bit integers as extension of some systems (e.g., GCC), but they are not standard (e.g., won't work with Visual Studio).
You can also parse it as a double (64-bit floating point integer):
This will print 233897314173811949568 or 26591046*2**43.
From the string "233897314173811950000", you parse the integer 233897314173811949568. Is that what you wanted? The simdjson library will do it for you if that's what you want, as demonstrated above...
You have to decide what you want to do with it. If you want to store it using your own type, then you can get access to the raw type and then provide your own parser (e.g., to MyDecimalType or to MyBigIntegerType). The simdjson library provides this functionality as well.
What the simdjson library does not provide is a custom big-integer or decimal type, because that's not standard. We do, however, make it easy for you to do your own parsing if you so desire. Please see the relevant section of our documentation.
I'd like to suggest that handling integers exceeding 64 bits by default as floats, unless explicitly opted out, could be a more accommodating approach. It's worth considering the essence of JSON, which stands for JavaScript Object Notation.
It's notable that every JavaScript engine currently treats very large numbers as floats unless a dedicated BigInt type is specified. For instance, when executing 2341341341234134112341343 in V8, SpiderMonkey, or JavaScriptCore, the result is 2.3413413412341342e+24. To prevent data loss, one would need to use 2341341341234134112341343n, which is not part of the JSON spec and cannot be serialized or deserialized. Conversely, attempting JSON.parse("2341341341234134112341343") would yield the same float value.
I would if I could. My c++ skills are virtually non-existent. I've created a PR in the rust port that enable this behaviour via a feature flag: simd-lite/simd-json#363
Activity
lemire commentedon May 14, 2019
Treating it as a string would violate the JSON specification as far as I can tell and once you reserialize, you'd end up with "3879..." (a string value) instead of 3879 (an integer value). JavaScript and some C++ parsers might convert it to a floating-point number but this is not ideal either since it is a lossy process (you won't be able to retrieve the original integer). We could map it to a "big integer" class of some kind... but that's inconvenient for the users.
What is your purpose with such a document?
You can't, portably, represent such larger integers in C/C++ on any system I know of.
crazyxman commentedon May 14, 2019
Thank you. Maybe it's wrong to do so.
This idea comes from the fourth parameter of the PHP function json_decode() when options=JSON_BIGINT_AS_STRING
Https://www.php.net/manual/en/function.json-decode.php
lemire commentedon May 14, 2019
I am reopening because I don't think this is "wrong".
[-]Analyze integer overflow recommendations[/-][+]Handle "large integers"[/+]lemire commentedon May 14, 2019
Of course, it would help to have a good motivation for this problem.
plokhotnyuk commentedon May 14, 2019
It can be decimal128 numbers with up to 34-digit mantissas, bank account numbers or IDs in decimal representation, etc.
Packing them to a string is one of safest options. Some implementations that just parse the input to a big number can be vulnerable on a malicious input.
lemire commentedon May 14, 2019
Yes. I would still like to have them flagged as "bigint" or something and not stored as strings per se.
So we could have a type "bigint" that actually returns a string when you query the value. It would be up to the user to figure out what do with the string.
lemire commentedon Sep 2, 2019
Note that we now a support for 64-bit unsigned integers.
Of course, we still cannot represent big integers like 38793000000000000001.
hedgefund commentedon Apr 25, 2020
It would be useful to be able to direct simdjson to treat all numbers like that (double and integer) regardless of their actual precision in the input with some compile-time flag.
A common use case that comes up a lot when dealing with JSON libraries is a JSON market data stream of cryptocurrency exchanges; some of them send the price/quantity as doubles instead of strings and you want to be able to parse that directly into a decimal class without losing precision (since getting the decimal precision correct with currency data is critical). RapidJSON for example has an optional parser flag
kParseNumbersAsStringsFlagwhich does exactly this.lemire commentedon Apr 25, 2020
Yes. Offering the option of parsing numbers as strings is entirely doable.
hedgefund commentedon Apr 26, 2020
Is this something that one could temporarily hack locally with small modifications to the master version of simdjson until official support is available, or does it require some fundamental internal changes? It's the thing we miss most about RapidJSON when switching to simdjson.
lemire commentedon Apr 26, 2020
@hedgefund
Our code base is "advanced C++" but it is super clean. It is designed to be maintainable.
See these lines...
https://github.com/simdjson/simdjson/blob/master/src/generic/stage2_build_tape.h#L165-L179
Basically, rewiring "parse_number" so that it handles the number as a string... is what you want to do... Not hard.
It won't take 5 minutes. But it won't take 5 months. Might take a few days... depending on your C++ skills.
If you are pretty good at C++, you might even be able to do a PR that we could integrate soon.
Look for the HACKING.md file first.
16 remaining items
AlexeyDmitriev commentedon Jul 1, 2021
Actually, I think raw_json_token() provides most of the value for me. I can make an informed decision, how to parse the number if we get it from specific API.
What your library could provide is faster parsing because it's known to be fast to parse other things. The question of what you can provide to parse the class that I will implement tomorrow is non-trivial.
One thing that comes to mind is you can pre-parse things like sign, integer and fractional part, exponent part(because you can probably do that for doubles anyway) and provide them as strings and as integers.
Also, you can make an API a bit nicer for these cases by allowing to get arbitrary types if they provide some API (or registered sonehow). So that I can write something along the lines of
static_cast<MyDecimal>(json)orjson.get_number<MyDecimal>()lemire commentedon Jul 2, 2021
Pull request invited!
mbasmanova commentedon Dec 15, 2023
Seeing similar issue in Velox with 233897314173811950000: facebookincubator/velox#8066
lemire commentedon Dec 15, 2023
@mbasmanova Reproducing my answer here...
In standard C++, we have 64-bit, 32-bit, 16-bit and 8-bit integers and well as 64-bit, 32-bit, and 16-bit floating-point integers. None of them can represent the value provided.
We can try with a 64-bit integer (
uint64_t). The number233897314173811950000which is0xcadfa3000000001b0, is much larger than what an unsigned 64-bit integer can store, which is0xffffffffffffffff, so no luck there. There are 128-bit integers as extension of some systems (e.g., GCC), but they are not standard (e.g., won't work with Visual Studio).You can also parse it as a double (64-bit floating point integer):
This will print 233897314173811949568 or
26591046*2**43.From the string
"233897314173811950000", you parse the integer 233897314173811949568. Is that what you wanted? The simdjson library will do it for you if that's what you want, as demonstrated above...You have to decide what you want to do with it. If you want to store it using your own type, then you can get access to the raw type and then provide your own parser (e.g., to MyDecimalType or to MyBigIntegerType). The simdjson library provides this functionality as well.
What the simdjson library does not provide is a custom big-integer or decimal type, because that's not standard. We do, however, make it easy for you to do your own parsing if you so desire. Please see the relevant section of our documentation.
richarddd commentedon Jan 10, 2024
I'd like to suggest that handling integers exceeding 64 bits by default as floats, unless explicitly opted out, could be a more accommodating approach. It's worth considering the essence of JSON, which stands for JavaScript Object Notation.
It's notable that every JavaScript engine currently treats very large numbers as floats unless a dedicated BigInt type is specified. For instance, when executing
2341341341234134112341343in V8, SpiderMonkey, or JavaScriptCore, the result is2.3413413412341342e+24. To prevent data loss, one would need to use2341341341234134112341343n, which is not part of the JSON spec and cannot be serialized or deserialized. Conversely, attemptingJSON.parse("2341341341234134112341343")would yield the same float value.lemire commentedon Jan 10, 2024
@richarddd I think that's what @Yuhta proposed in facebookincubator/velox#8066 (comment)
PR invited.
richarddd commentedon Jan 10, 2024
I would if I could. My c++ skills are virtually non-existent. I've created a PR in the rust port that enable this behaviour via a feature flag: simd-lite/simd-json#363
lemire commentedon Jan 31, 2024
I really like PR #2115 by @saleyn
lemire commentedon Feb 26, 2024
Closed via #2139
This will be part of the next release.
feat: Add DOM tape support for big integers (opt-in)
feat: Add DOM tape support for big integers (opt-in)
feat: Add DOM tape support for big integers (opt-in)
feat: Add DOM tape support for big integers (opt-in)
feat: Add DOM tape support for big integers (opt-in) (#2640)