From bf8ccc20e9d3bf17083e9e9ab8703c11f3eaf82a Mon Sep 17 00:00:00 2001 From: Harinath Nampally Date: Wed, 22 Jan 2025 15:28:51 -0500 Subject: [PATCH] fix compilation issue (#4613) * fix compilation issue Signed-off-by: Harinath Nampally * add unit test Signed-off-by: Harinath Nampally * fix ci failure check Signed-off-by: Harinath Nampally --------- Signed-off-by: Harinath Nampally --- include/nlohmann/detail/json_pointer.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- tests/src/unit-alt-string.cpp | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 0482eabf54..b11b13af3d 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -750,7 +750,7 @@ class json_pointer // iterate array and use index as reference string for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i) { - flatten(detail::concat(reference_string, '/', std::to_string(i)), + flatten(detail::concat(reference_string, '/', std::to_string(i)), value.m_data.m_value.array->operator[](i), result); } } @@ -769,7 +769,7 @@ class json_pointer // iterate object and use keys as reference string for (const auto& element : *value.m_data.m_value.object) { - flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result); + flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result); } } break; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index cda42a6e34..07dea3c4ab 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15239,7 +15239,7 @@ class json_pointer // iterate array and use index as reference string for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i) { - flatten(detail::concat(reference_string, '/', std::to_string(i)), + flatten(detail::concat(reference_string, '/', std::to_string(i)), value.m_data.m_value.array->operator[](i), result); } } @@ -15258,7 +15258,7 @@ class json_pointer // iterate object and use keys as reference string for (const auto& element : *value.m_data.m_value.object) { - flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result); + flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result); } } break; diff --git a/tests/src/unit-alt-string.cpp b/tests/src/unit-alt-string.cpp index 4a9a608ffc..ae0a280036 100644 --- a/tests/src/unit-alt-string.cpp +++ b/tests/src/unit-alt-string.cpp @@ -359,4 +359,12 @@ TEST_CASE("alternative string type") alt_json const j2 = {"foo", "bam"}; CHECK(alt_json::diff(j1, j2).dump() == "[{\"op\":\"replace\",\"path\":\"/1\",\"value\":\"bam\"},{\"op\":\"remove\",\"path\":\"/2\"}]"); } + + SECTION("flatten") + { + // a JSON value + const alt_json j = alt_json::parse(R"({"foo": ["bar", "baz"]})"); + const auto j2 = j.flatten(); + CHECK(j2.dump() == R"({"/foo/0":"bar","/foo/1":"baz"})"); + } }