🐛 use correct indentation character on resize

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-05-20 21:56:32 +02:00
parent d96329f8d6
commit b3effb609c
3 changed files with 35 additions and 6 deletions
@@ -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(indent_string.size() * 2, indent_char);
}
// first n-1 elements
@@ -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(indent_string.size() * 2, indent_char);
}
// first n-1 elements
@@ -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(indent_string.size() * 2, indent_char);
}
o->write_characters(indent_string.c_str(), new_indent);
+3 -3
View File
@@ -19302,7 +19302,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(indent_string.size() * 2, indent_char);
}
// first n-1 elements
@@ -19375,7 +19375,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(indent_string.size() * 2, indent_char);
}
// first n-1 elements
@@ -19436,7 +19436,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(indent_string.size() * 2, indent_char);
}
o->write_characters(indent_string.c_str(), new_indent);
+29
View File
@@ -245,6 +245,35 @@ TEST_CASE("object inspection")
CHECK(binary.dump(1024).size() == 2086);
}
SECTION("indentation and resize")
{
SECTION("array")
{
const auto j_array = "[[[[[[]]]]]]"_json;
// 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');
}
SECTION("object")
{
const auto j_object = R"({"":{"":{"":{"":{"":{}}}}}})"_json;
// 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');
}
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');
}
}
SECTION("dump and floating-point numbers")
{
auto s = json(42.23).dump();