Simplify JSON generation.

This commit is contained in:
levlam 2021-07-27 09:41:11 +03:00
parent c9406e71c3
commit 103b0efcaa
2 changed files with 12 additions and 10 deletions

View File

@ -58,21 +58,18 @@ static string from_response(const td_api::Object &object, const string &extra, i
auto buf = StackAllocator::alloc(1 << 18);
JsonBuilder jb(StringBuilder(buf.as_slice(), true), -1);
jb.enter_value() << ToJson(object);
auto slice = jb.string_builder().as_cslice();
auto &sb = jb.string_builder();
auto slice = sb.as_cslice();
CHECK(!slice.empty() && slice.back() == '}');
string str;
str.reserve(slice.size() + (extra.empty() ? 0 : 10 + extra.size()) + (client_id == 0 ? 0 : 14 + 10));
str.append(slice.begin(), slice.size() - 1);
sb.pop_back();
if (!extra.empty()) {
str += ",\"@extra\":";
str += extra;
sb << ",\"@extra\":" << extra;
}
if (client_id != 0) {
str += ",\"@client_id\":";
str += to_string(client_id);
sb << ",\"@client_id\":" << client_id;
}
str += '}';
return str;
sb << '}';
return sb.as_cslice().str();
}
static TD_THREAD_LOCAL string *current_output;

View File

@ -27,6 +27,11 @@ class StringBuilder {
error_flag_ = false;
}
void pop_back() {
CHECK(current_ptr_ > begin_ptr_);
current_ptr_--;
}
MutableCSlice as_cslice() {
if (current_ptr_ >= end_ptr_ + RESERVED_SIZE) {
std::abort(); // shouldn't happen