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
45 changes: 30 additions & 15 deletions include/nlohmann/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ class serializer
o->write_characters("{\n", 2);

// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
const auto new_indent = reserve_indent(current_indent, indent_step);

// first n-1 elements
auto i = val.m_data.m_value.object->cbegin();
Expand Down Expand Up @@ -198,11 +194,7 @@ class serializer
o->write_characters("[\n", 2);

// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
const auto new_indent = reserve_indent(current_indent, indent_step);

// first n-1 elements
for (auto i = val.m_data.m_value.array->cbegin();
Expand Down Expand Up @@ -259,11 +251,7 @@ class serializer
o->write_characters("{\n", 2);

// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
const auto new_indent = reserve_indent(current_indent, indent_step);

o->write_characters(indent_string.c_str(), new_indent);

Expand Down Expand Up @@ -373,6 +361,33 @@ class serializer
}
}

/*!
@brief compute the next indentation level and grow the indentation string

Computes the new indentation level @a current_indent + @a indent_step and
grows the indentation string (by doubling its size, but at least to the new
level) so that it can be used as a source for writing that many indentation
characters. The newly added characters use the configured @ref indent_char.

@param[in] current_indent the current indentation level
@param[in] indent_step the number of characters to indent per level

@return the new indentation level @a current_indent + @a indent_step
*/
unsigned int reserve_indent(const unsigned int current_indent, const unsigned int indent_step)
{
const unsigned int new_indent = current_indent + indent_step;
// a very large indent_step can wrap the unsigned accumulation on deep
// nesting, which would silently truncate the indentation
JSON_ASSERT(new_indent >= current_indent);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<std::size_t>(new_indent)), indent_char);
}
JSON_ASSERT(indent_string.size() >= new_indent);
return new_indent;
}

JSON_PRIVATE_UNLESS_TESTED:
/*!
@brief dump escaped string
Expand Down
45 changes: 30 additions & 15 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19299,11 +19299,7 @@ class serializer
o->write_characters("{\n", 2);

// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
const auto new_indent = reserve_indent(current_indent, indent_step);

// first n-1 elements
auto i = val.m_data.m_value.object->cbegin();
Expand Down Expand Up @@ -19372,11 +19368,7 @@ class serializer
o->write_characters("[\n", 2);

// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
const auto new_indent = reserve_indent(current_indent, indent_step);

// first n-1 elements
for (auto i = val.m_data.m_value.array->cbegin();
Expand Down Expand Up @@ -19433,11 +19425,7 @@ class serializer
o->write_characters("{\n", 2);

// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
const auto new_indent = reserve_indent(current_indent, indent_step);

o->write_characters(indent_string.c_str(), new_indent);

Expand Down Expand Up @@ -19547,6 +19535,33 @@ class serializer
}
}

/*!
@brief compute the next indentation level and grow the indentation string

Computes the new indentation level @a current_indent + @a indent_step and
grows the indentation string (by doubling its size, but at least to the new
level) so that it can be used as a source for writing that many indentation
characters. The newly added characters use the configured @ref indent_char.

@param[in] current_indent the current indentation level
@param[in] indent_step the number of characters to indent per level

@return the new indentation level @a current_indent + @a indent_step
*/
unsigned int reserve_indent(const unsigned int current_indent, const unsigned int indent_step)
{
const unsigned int new_indent = current_indent + indent_step;
// a very large indent_step can wrap the unsigned accumulation on deep
// nesting, which would silently truncate the indentation
JSON_ASSERT(new_indent >= current_indent);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<std::size_t>(new_indent)), indent_char);
}
JSON_ASSERT(indent_string.size() >= new_indent);
return new_indent;
}

JSON_PRIVATE_UNLESS_TESTED:
/*!
@brief dump escaped string
Expand Down
35 changes: 35 additions & 0 deletions tests/src/unit-inspection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,41 @@ TEST_CASE("object inspection")
CHECK(binary.dump(1024).size() == 2086);
}

SECTION("indentation and resize")
{
SECTION("array")
{
const auto j_array = json::parse("[[[[[[]]]]]]");
// check right size after indentation triggering a resize
CHECK(j_array.dump(1024).size() == 25622);
// check if right indentation symbol is used
CHECK(j_array.dump(1024, '\t')[4096] == '\t');
// check resize is large enough
CHECK(j_array.dump(10000).size() == 250022);
}

SECTION("object")
{
const auto j_object = json::parse(R"({"":{"":{"":{"":{"":{}}}}}})");
// check right size after indentation triggering a resize
CHECK(j_object.dump(1024).size() == 25642);
// check if right indentation symbol is used
CHECK(j_object.dump(1024, '\t')[4096] == '\t');
// check resize is large enough
CHECK(j_object.dump(10000).size() == 250042);
}

SECTION("binary")
{
const auto j_binary = json::binary({1, 2, 3}, 128);
// check right size after indentation triggering a resize
CHECK(j_binary.dump(1024).size() == 2086);
CHECK(j_binary.dump(1024, '\t')[1024] == '\t');
// check resize is large enough
CHECK(j_binary.dump(10000).size() == 20038);
}
}

SECTION("dump and floating-point numbers")
{
auto s = json(42.23).dump();
Expand Down
18 changes: 10 additions & 8 deletions tests/src/unit-serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,16 @@ TEST_CASE("dump for basic_json with long double number_float_t")
SECTION("round-trip dump/parse")
{
constexpr std::array<long double, 13> values =
{{
0.0L, -0.0L, 1.0L, -1.0L,
0.5L, -0.5L, 1.5L, -2.25L,
1.23e45L, 1.23e-45L,
(std::numeric_limits<long double>::min)(),
std::numeric_limits<long double>::lowest(),
(std::numeric_limits<long double>::max)()
}};
{
{
0.0L, -0.0L, 1.0L, -1.0L,
0.5L, -0.5L, 1.5L, -2.25L,
1.23e45L, 1.23e-45L,
(std::numeric_limits<long double>::min)(),
std::numeric_limits<long double>::lowest(),
(std::numeric_limits<long double>::max)()
}
};

for (long double v : values)
{
Expand Down
Loading