-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
make number serialzation precision configurable #4591
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1272,10 +1272,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec | |
string_t dump(const int indent = -1, | ||
const char indent_char = ' ', | ||
const bool ensure_ascii = false, | ||
const error_handler_t error_handler = error_handler_t::strict) const | ||
const error_handler_t error_handler = error_handler_t::strict, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to #4513 (comment) where a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so mine is one too much? :) but sure, I understand, will look into it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, hang on, isnt that a public api breaking change? so you want me to change the public api? or add an overload? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Many others before you have been "one too much" and thus haven't been done. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There has been so many discussions about tweaking every last aspect of serializations that adding parameters is getting out of hand. |
||
const size_t precision = std::numeric_limits<size_t>::max()) const | ||
maddanio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
string_t result; | ||
serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler); | ||
serializer s(detail::output_adapter<char, string_t>(result), indent_char, precision, error_handler); | ||
|
||
if (indent >= 0) | ||
{ | ||
|
@@ -3982,8 +3983,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec | |
o.width(0); | ||
|
||
// do the actual serialization | ||
serializer s(detail::output_adapter<char>(o), o.fill()); | ||
serializer s(detail::output_adapter<char>(o), o.fill(), static_cast<size_t>(o.precision())).; | ||
s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation)); | ||
|
||
return o; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
__ _____ _____ _____ | ||
__| | __| | | | JSON for Modern C++ (test suite) | ||
| | |__ | | | | | | version 3.10.5 | ||
|_____|_____|_____|_|___| https://github.com/nlohmann/json | ||
|
||
Licensed under the MIT License <http://opensource.org/licenses/MIT>. | ||
SPDX-License-Identifier: MIT | ||
Copyright (c) 2013-2022 Niels Lohmann <http://nlohmann.me>. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The MIT license does not need to be quoted verbatim here. Just take the header that is used in all other tests: // __ _____ _____ _____
// __| | __| | | | JSON for Modern C++ (supporting code)
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013 - 2024 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copy pasted from another test, but yes |
||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
#include "doctest_compatibility.h" | ||
|
||
#include <cmath> | ||
#include <iomanip> | ||
#include <nlohmann/json.hpp> | ||
#include <sstream> | ||
#include <string> | ||
using json = nlohmann::json; | ||
|
||
#include <set> | ||
|
||
TEST_CASE("precision<nlohmann::json>") | ||
{ | ||
json j; | ||
j = M_PI; | ||
auto to_string = [](const nlohmann::json & j, size_t precision) | ||
{ | ||
std::stringstream ss; | ||
ss << std::setprecision(precision) << j; | ||
return ss.str(); | ||
}; | ||
auto s1 = to_string(j, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity, I would also add a test here for the default behavior of no explicitly set precision. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, but tbh I will then just adapt the test to what I observer, since I have no expectation for that case |
||
auto s2 = to_string(j, 2); | ||
auto s3 = to_string(j, 3); | ||
CHECK(s1 == "3.1"); | ||
CHECK(s2 == "3.14"); | ||
CHECK(s3 == "3.141"); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.