Skip to content

Commit

Permalink
Simplify JSON generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Jul 27, 2021
1 parent c9406e7 commit 103b0ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
17 changes: 7 additions & 10 deletions td/telegram/ClientJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions tdutils/td/utils/StringBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 103b0ef

Please sign in to comment.