Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[COMMIT seastar master] json: Make formatter call write for jsonable

1 view
Skip to first unread message

Commit Bot

<bot@cloudius-systems.com>
unread,
Jun 17, 2024, 12:25:21 AM6/17/24
to seastar-dev@googlegroups.com, Stephan Dollberg
From: Stephan Dollberg <stephan....@gmail.com>
Committer: Stephan Dollberg <ste...@redpanda.com>
Branch: master

json: Make formatter call write for jsonable

`write(output_stream<char>&, const jsonable&)` would call
`jsonable::to_json` which would just serialize the whole object (and its
children) to a string and then dump that onto the `output_stream`.

Instead call `jsonable::write(output_stream<char>&)` which will
recursively call `write` (for the `jsonbase` subclasses) and hence
propagate the benefits of writing to the output stream directly.

To make sure that the object is alive for the duration of the call we
change the overload to copy its argument.

---
diff --git a/include/seastar/json/formatter.hh b/include/seastar/json/formatter.hh
--- a/include/seastar/json/formatter.hh
+++ b/include/seastar/json/formatter.hh
@@ -330,9 +330,12 @@ public:
* @param obj the date_time to format
* @return the given json object in a json format
*/
- static future<> write(output_stream<char>& s, const jsonable& obj) {
- return s.write(to_json(obj));
- }
+ template <std::derived_from<jsonable> Jsonable>
+ static future<> write(output_stream<char>& s, Jsonable obj) {
+ return do_with(std::move(obj), [&s] (const auto& obj) {
+ return obj.write(s);
+ });
+ }

/**
* return a json formatted unsigned long
diff --git a/tests/unit/httpd_test.cc b/tests/unit/httpd_test.cc
--- a/tests/unit/httpd_test.cc
+++ b/tests/unit/httpd_test.cc
@@ -735,18 +735,18 @@ std::string get_value(int size) {
/*
* A helper object that map to a big json string
* in the format of:
- * {"valu": "aaa....aa", "valu": "aaa....aa", "valu": "aaa....aa"...}
+ * {"valu":"aaa....aa","valu":"aaa....aa","valu":"aaa....aa"...}
*
* The object can have an arbitrary size in multiplication of 10000 bytes
* */
struct extra_big_object : public json::json_base {
json::json_element<sstring>* value;
extra_big_object(size_t size) {
value = new json::json_element<sstring>;
- // size = brackets + (name + ": " + get_value) * n + ", " * (n-1)
- // size = 2 + (name + 6 + get_value) * n - 2
+ // size = brackets + ("name" + ":" + get_value()) * n + "," * (n-1)
+ // size = 2 + (valu + 4 + get_value) * n - 1
value->_name = "valu";
- *value = get_value(9990);
+ *value = get_value(9992);
for (size_t i = 0; i < size/10000; i++) {
_elements.emplace_back(value);
}
@@ -769,7 +769,9 @@ struct extra_big_object : public json::json_base {
SEASTAR_TEST_CASE(json_stream) {
std::vector<extra_big_object> vec;
size_t num_objects = 1000;
- size_t total_size = num_objects * 1000001 + 1;
+ // each object is 1000001 bytes plus a comma for all but the last and plus
+ // two for the [] backets
+ size_t total_size = num_objects * 1000002 + 1;
for (size_t i = 0; i < num_objects; i++) {
vec.emplace_back(1000000);
}
Reply all
Reply to author
Forward
0 new messages