Skip to content

Commit

Permalink
Fix json rendering of large osm ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Iman committed Jan 12, 2025
1 parent 3614af7 commit e5df0c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 4 additions & 1 deletion include/util/json_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ template <typename Out> struct Renderer
// `fmt::memory_buffer` stores first 500 bytes in the object itself(i.e. on stack in this
// case) and then grows using heap if needed
fmt::memory_buffer buffer;
fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{:.10g}"), number.value);
if (static_cast<std::uint64_t>(number.value) == number.value)
fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{}"), static_cast<std::uint64_t>(number.value));
else
fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{:.10g}"), number.value);

write(buffer.data(), buffer.size());
}
Expand Down
9 changes: 7 additions & 2 deletions unit_tests/util/json_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,13 @@ BOOST_AUTO_TEST_CASE(test_json_issue_6531)
BOOST_CHECK_EQUAL(output, "0.1234567892");

output.clear();
renderer(123456789123456789.);
BOOST_CHECK_EQUAL(output, "1.234567891e+17");
renderer(12345678912345678.);
BOOST_CHECK_EQUAL(output, "12345678912345678");

// handle large osm ids
output.clear();
renderer(1000396615812);
BOOST_CHECK_EQUAL(output, "1000396615812");
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e5df0c4

Please sign in to comment.