Skip to content

Commit

Permalink
json_formatter: Make formatter::write work for std::pair
Browse files Browse the repository at this point in the history
Previously the un/associative container overloads for `formatter::write`
were broken because it failed to find an overload for
`write(output_stream, pair)`.

To fix this we make the `write(output_stream<char>&, state, Iter, Iter)`
overload actually `write(output_stream, state, pair)` so that the
existing overload that handles `pair` can be found.

Further we fix the fallback `write(output_stream, state, T)` overload to
call `formatter::write` recurisvely with the state stripped instead of
calling `to_json`. This keeps the recursive zero-copy nature of
`formatter::write` intact.

All of the above mirrors how the existing overloads for `to_json`
already work.
  • Loading branch information
StephanDollberg committed Sep 17, 2024
1 parent 03cdf0b commit db31414
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/seastar/json/formatter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ class formatter {
static future<> write(output_stream<char>& stream, state s, Iter i, Iter e) {
return do_with(true, [&stream, s, i, e] (bool& first) {
return stream.write(begin(s)).then([&first, &stream, s, i, e] {
return do_for_each(i, e, [&first, &stream] (auto& m) {
return do_for_each(i, e, [&first, &stream, s] (auto& m) {
auto f = (first) ? make_ready_future<>() : stream.write(",");
first = false;
return f.then([&m, &stream] {
return write(stream, m);
return f.then([&m, &stream, s] {
return write(stream, s, m);
});
}).then([&stream, s] {
return stream.write(end(s));
Expand All @@ -120,7 +120,7 @@ class formatter {
// fallback template
template<typename T>
static future<> write(output_stream<char>& stream, state, const T& t) {
return stream.write(to_json(t));
return write(stream, t);
}

public:
Expand Down

0 comments on commit db31414

Please sign in to comment.