提交 a382a93a 编写于 作者: N Niels

Merge pull request #38 from wtsi-hgi/feature/small-float-serialization

Feature/small float serialization
......@@ -1882,7 +1882,8 @@ class basic_json
recursively. Note that
- strings and object keys are escaped using escape_string()
- numbers are converted to a string before output using std::to_string()
- integer numbers are converted to a string before output using std::to_string()
- floating-point numbers are converted to a string using "%g" format
@param prettyPrint whether the output shall be pretty-printed
@param indentStep the indent level
......@@ -1990,7 +1991,12 @@ class basic_json
case (value_t::number_float):
{
return std::to_string(m_value.number_float);
// 15 digits of precision allows round-trip IEEE 754 string->double->string
unsigned int sz = (unsigned int)std::snprintf(nullptr, 0, "%.15g", m_value.number_float);
std::vector<char> buf(sz + 1);
std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
string_t formatted = buf.data();
return formatted;
}
default:
......
......@@ -1160,6 +1160,12 @@ TEST_CASE("object inspection")
CHECK(s.find("42.23") != std::string::npos);
}
SECTION("dump and small floating-point numbers")
{
auto s = json(1.23456e-78).dump();
CHECK(s.find("1.23456e-78") != std::string::npos);
}
SECTION("dump and non-ASCII characters")
{
CHECK(json("ä").dump() == "\"ä\"");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册