Skip to content

Commit 44f6036

Browse files
authored
Fix converting vector of pairs to JSON using json_auto (#1771)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 6075e56 commit 44f6036

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/core/json/include/sourcemeta/core/json_auto.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
#include <type_traits> // std::false_type, std::true_type, std::void_t, std::is_enum_v, std::underlying_type_t, std::is_same_v, std::is_base_of_v, std::remove_cvref_t
1111
#include <utility> // std::pair
1212

13+
// Forward declarations (added as needed)
14+
#ifndef DOXYGEN
15+
namespace sourcemeta::core {
16+
template <typename L, typename R>
17+
auto to_json(const std::pair<L, R> &value) -> JSON;
18+
}
19+
#endif
20+
1321
namespace sourcemeta::core {
1422

1523
/// @ingroup json
@@ -107,13 +115,12 @@ auto to_json(const T &value) -> JSON {
107115
template <typename T>
108116
requires std::is_enum_v<T>
109117
auto to_json(const T value) -> JSON {
110-
return to_json<std::underlying_type_t<T>>(
111-
static_cast<std::underlying_type_t<T>>(value));
118+
return to_json(static_cast<std::underlying_type_t<T>>(value));
112119
}
113120

114121
/// @ingroup json
115122
template <typename T> auto to_json(const std::optional<T> &value) -> JSON {
116-
return value.has_value() ? to_json<T>(value.value()) : JSON{nullptr};
123+
return value.has_value() ? to_json(value.value()) : JSON{nullptr};
117124
}
118125

119126
/// @ingroup json
@@ -123,7 +130,7 @@ auto to_json(typename T::const_iterator begin, typename T::const_iterator end)
123130
// TODO: Extend `make_array` to optionally take iterators, etc
124131
auto result{JSON::make_array()};
125132
for (auto iterator = begin; iterator != end; ++iterator) {
126-
result.push_back(to_json<typename T::value_type>(*iterator));
133+
result.push_back(to_json(*iterator));
127134
}
128135

129136
return result;
@@ -164,8 +171,7 @@ auto to_json(typename T::const_iterator begin, typename T::const_iterator end)
164171
-> JSON {
165172
auto result{JSON::make_object()};
166173
for (auto iterator = begin; iterator != end; ++iterator) {
167-
result.assign(iterator->first,
168-
to_json<typename T::mapped_type>(iterator->second));
174+
result.assign(iterator->first, to_json(iterator->second));
169175
}
170176

171177
return result;

test/json/json_to_json_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ TEST(JSON_to_json, vector_without_iterators_and_transform) {
117117
EXPECT_EQ(result, expected);
118118
}
119119

120+
TEST(JSON_to_json, vector_of_pairs) {
121+
const std::vector<std::pair<std::string, std::size_t>> value{
122+
{"foo", 1}, {"bar", 2}, {"baz", 3}};
123+
const auto result{sourcemeta::core::to_json(value)};
124+
125+
const auto expected{sourcemeta::core::parse_json(R"JSON([
126+
[ "foo", 1 ],
127+
[ "bar", 2 ],
128+
[ "baz", 3 ]
129+
])JSON")};
130+
131+
EXPECT_EQ(result, expected);
132+
}
133+
120134
TEST(JSON_to_json, map_with_iterators) {
121135
const std::map<std::string, std::size_t> value{
122136
{"foo", 1}, {"bar", 2}, {"baz", 3}};

0 commit comments

Comments
 (0)