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

JSON Number(double) Insufficient for Long NodeID #5716

Closed
wangyoucao577 opened this issue Apr 16, 2020 · 3 comments
Closed

JSON Number(double) Insufficient for Long NodeID #5716

wangyoucao577 opened this issue Apr 16, 2020 · 3 comments
Labels

Comments

@wangyoucao577
Copy link
Contributor

We use annotations=true to output nodes in route response, then we found the output nodes may invalid sometimes.
After investigate, it's caused by the JSON util only has one number type Number, which defined as double:

struct Number
{
Number() = default;
Number(double value_) : value{value_} {}
double value;
};

It means whatever the internal number type is, it will be converted to double before output. For some long std::uint64_t, the value will be cut in this conversion.

In our test, the nodeID 9280805980001101 will be cut to 9280805980001100, which results invalid nodeID.
Here's some test code:

// Example program
#include <iostream>
#include <string>
#include <cstdint>
#include <iomanip>

int main()
{
  std::uint64_t id = 9280805980001101;
  double d_id = static_cast<double>(id);
  std::uint64_t convert_back_id = static_cast<std::uint64_t>(d_id);
  
  std::cout << id << std::endl;
  std::cout << std::fixed << std::setprecision(6) << d_id << std::endl;
  std::cout << convert_back_id << std::endl;
}

// output: 
9280805980001101
9280805980001100.000000
9280805980001100

Full story please refer to Telenav#276

Any suggestion? Is it possible to have more Number types in the JSON util?
Many thanks in advance!

@awgrover
Copy link

The JSON spec does not appear to limit the range of a number at all(!) https://www.json.org/json-en.html. JSON Schema seems to, I think, as 32 bit signed: -2,147,483,648 ... 2,147,483,647 . Google notes a work around of

a 64-bit integer must be represented as a string in JSON requests/responses. The type property will be set to "string", but the format property will be set to "int64" to indicate that it is a 64-bit integer

As a workaround in this case, is it possible for you to use a string-id instead (i.e. trivially convert to-string for osrm, then convert to-int on return)?

@wangyoucao577
Copy link
Contributor Author

@awgrover Actually I already returned string id as a workaround(see Telenav#276 (comment) and Telenav#285).
However, refer to https://stackoverflow.com/questions/13502398/json-integers-limit-on-size, the number seems unlimited by JSON spec. I think return correctly uint64 still will be better.

Copy link

github-actions bot commented Jul 8, 2024

This issue seems to be stale. It will be closed in 30 days if no further activity occurs.

@github-actions github-actions bot added the Stale label Jul 8, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants