Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/nlohmann/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class serializer
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<typename string_t::size_type>(new_indent)), ' ');
}

// first n-1 elements
Expand Down Expand Up @@ -201,7 +201,7 @@ class serializer
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<typename string_t::size_type>(new_indent)), ' ');
}

// first n-1 elements
Expand Down Expand Up @@ -262,7 +262,7 @@ class serializer
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<typename string_t::size_type>(new_indent)), ' ');
}

o->write_characters(indent_string.c_str(), new_indent);
Expand Down
6 changes: 3 additions & 3 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19835,7 +19835,7 @@ class serializer
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<typename string_t::size_type>(new_indent)), ' ');
}

// first n-1 elements
Expand Down Expand Up @@ -19908,7 +19908,7 @@ class serializer
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<typename string_t::size_type>(new_indent)), ' ');
}

// first n-1 elements
Expand Down Expand Up @@ -19969,7 +19969,7 @@ class serializer
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<typename string_t::size_type>(new_indent)), ' ');
}

o->write_characters(indent_string.c_str(), new_indent);
Expand Down
30 changes: 30 additions & 0 deletions tests/src/unit-serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,36 @@ TEST_CASE("serialization")
}
}

SECTION("dump with large indentation")
{
// the indentation buffer starts at 512 characters and grows on demand;
// a width that needs more room than a single doubling provides must keep
// growing the buffer rather than emit past its end
const unsigned int width = 1100;

SECTION("object")
{
const json j = {{"outer", {{"inner", 1}}}};
const std::string s = j.dump(static_cast<int>(width));
CHECK(s.find('\n' + std::string(width, ' ') + "\"outer\"") != std::string::npos);
CHECK(s.find('\n' + std::string(2 * width, ' ') + "\"inner\"") != std::string::npos);
}

SECTION("array")
{
const json j = json::array({json::array({1})});
const std::string s = j.dump(static_cast<int>(width));
CHECK(s.find('\n' + std::string(2 * width, ' ') + '1') != std::string::npos);
}

SECTION("binary")
{
const json j = json::binary({1, 2, 3});
const std::string s = j.dump(static_cast<int>(width));
CHECK(s.find('\n' + std::string(width, ' ') + "\"bytes\"") != std::string::npos);
}
}

SECTION("to_string")
{
auto test = [&](std::string const & input, std::string const & expected)
Expand Down
Loading