Skip to content

Commit

Permalink
Fix vector<bytes> support in JSON interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Dec 12, 2024
1 parent 6050606 commit d961806
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
9 changes: 8 additions & 1 deletion td/generate/tl_json_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace td {

using Mode = tl::TL_writer::Mode;

static bool need_bytes(const tl::simple::Type *type) {
return type->type == tl::simple::Type::Bytes ||
(type->type == tl::simple::Type::Vector && need_bytes(type->vector_value_type));
}

template <class T>
void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_header) {
sb << "void to_json(JsonValueScope &jv, "
Expand All @@ -42,6 +47,8 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he
}
if (arg.type->type == tl::simple::Type::Bytes) {
object = PSTRING() << "base64_encode(" << object << ")";
} else if (need_bytes(arg.type)) {
object = "UNSUPPORTED STORED VECTOR OF BYTES";
} else if (arg.type->type == tl::simple::Type::Bool) {
object = PSTRING() << "JsonBool{" << object << "}";
} else if (arg.type->type == tl::simple::Type::Int64) {
Expand Down Expand Up @@ -102,7 +109,7 @@ void gen_from_json_constructor(StringBuilder &sb, const T *constructor, bool is_
} else {
sb << " {\n";
for (auto &arg : constructor->args) {
sb << " TRY_STATUS(from_json" << (arg.type->type == tl::simple::Type::Bytes ? "_bytes" : "") << "(to."
sb << " TRY_STATUS(from_json" << (need_bytes(arg.type) ? "_bytes" : "") << "(to."
<< tl::simple::gen_cpp_field_name(arg.name) << ", from.extract_field(\"" << tl::simple::gen_cpp_name(arg.name)
<< "\")));\n";
}
Expand Down
24 changes: 20 additions & 4 deletions td/tl/tl_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inline void to_json(JsonValueScope &jv, const JsonInt64 json_int64) {
}

struct JsonVectorInt64 {
const std::vector<int64> &value;
const vector<int64> &value;
};

inline void to_json(JsonValueScope &jv, const JsonVectorInt64 &vec) {
Expand All @@ -51,7 +51,7 @@ void to_json(JsonValueScope &jv, const tl_object_ptr<T> &value) {
}

template <class T>
void to_json(JsonValueScope &jv, const std::vector<T> &v) {
void to_json(JsonValueScope &jv, const vector<T> &v) {
auto ja = jv.enter_array();
for (auto &value : v) {
ja.enter_value() << ToJson(value);
Expand Down Expand Up @@ -134,14 +134,14 @@ inline Status from_json_bytes(string &to, JsonValue from) {
}

template <class T>
Status from_json(std::vector<T> &to, JsonValue from) {
Status from_json(vector<T> &to, JsonValue from) {
if (from.type() != JsonValue::Type::Array) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected Array, but receive " << from.type());
}
to = std::vector<T>(from.get_array().size());
to = vector<T>(from.get_array().size());
size_t i = 0;
for (auto &value : from.get_array()) {
TRY_STATUS(from_json(to[i], std::move(value)));
Expand All @@ -150,6 +150,22 @@ Status from_json(std::vector<T> &to, JsonValue from) {
return Status::OK();
}

inline Status from_json_bytes(vector<string> &to, JsonValue from) {
if (from.type() != JsonValue::Type::Array) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected Array, but receive " << from.type());
}
to = vector<string>(from.get_array().size());
size_t i = 0;
for (auto &value : from.get_array()) {
TRY_STATUS(from_json_bytes(to[i], std::move(value)));
i++;
}
return Status::OK();
}

template <class T>
std::enable_if_t<!std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue from) {
if (from.type() != JsonValue::Type::Object) {
Expand Down

0 comments on commit d961806

Please sign in to comment.