Skip to content

JSON Number(double) Insufficient for Long NodeID #5716

@wangyoucao577

Description

@wangyoucao577

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions