From b3effb609c99897d1a1e0ee1bcffca34c1820635 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 20 May 2026 21:56:32 +0200 Subject: [PATCH] :bug: use correct indentation character on resize Signed-off-by: Niels Lohmann --- include/nlohmann/detail/output/serializer.hpp | 6 ++-- single_include/nlohmann/json.hpp | 6 ++-- tests/src/unit-inspection.cpp | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 25a64d648..cdf0a9d7b 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -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); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2e16ad5b9..3a7e6fbff 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -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); diff --git a/tests/src/unit-inspection.cpp b/tests/src/unit-inspection.cpp index c70913d5b..9d6d06e23 100644 --- a/tests/src/unit-inspection.cpp +++ b/tests/src/unit-inspection.cpp @@ -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();