Skip to content

Commit d961806

Browse files
committed
Fix vector<bytes> support in JSON interface.
1 parent 6050606 commit d961806

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

td/generate/tl_json_converter.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace td {
2121

2222
using Mode = tl::TL_writer::Mode;
2323

24+
static bool need_bytes(const tl::simple::Type *type) {
25+
return type->type == tl::simple::Type::Bytes ||
26+
(type->type == tl::simple::Type::Vector && need_bytes(type->vector_value_type));
27+
}
28+
2429
template <class T>
2530
void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_header) {
2631
sb << "void to_json(JsonValueScope &jv, "
@@ -42,6 +47,8 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he
4247
}
4348
if (arg.type->type == tl::simple::Type::Bytes) {
4449
object = PSTRING() << "base64_encode(" << object << ")";
50+
} else if (need_bytes(arg.type)) {
51+
object = "UNSUPPORTED STORED VECTOR OF BYTES";
4552
} else if (arg.type->type == tl::simple::Type::Bool) {
4653
object = PSTRING() << "JsonBool{" << object << "}";
4754
} else if (arg.type->type == tl::simple::Type::Int64) {
@@ -102,7 +109,7 @@ void gen_from_json_constructor(StringBuilder &sb, const T *constructor, bool is_
102109
} else {
103110
sb << " {\n";
104111
for (auto &arg : constructor->args) {
105-
sb << " TRY_STATUS(from_json" << (arg.type->type == tl::simple::Type::Bytes ? "_bytes" : "") << "(to."
112+
sb << " TRY_STATUS(from_json" << (need_bytes(arg.type) ? "_bytes" : "") << "(to."
106113
<< tl::simple::gen_cpp_field_name(arg.name) << ", from.extract_field(\"" << tl::simple::gen_cpp_name(arg.name)
107114
<< "\")));\n";
108115
}

td/tl/tl_json.h

+20-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ inline void to_json(JsonValueScope &jv, const JsonInt64 json_int64) {
3131
}
3232

3333
struct JsonVectorInt64 {
34-
const std::vector<int64> &value;
34+
const vector<int64> &value;
3535
};
3636

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

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

136136
template <class T>
137-
Status from_json(std::vector<T> &to, JsonValue from) {
137+
Status from_json(vector<T> &to, JsonValue from) {
138138
if (from.type() != JsonValue::Type::Array) {
139139
if (from.type() == JsonValue::Type::Null) {
140140
return Status::OK();
141141
}
142142
return Status::Error(PSLICE() << "Expected Array, but receive " << from.type());
143143
}
144-
to = std::vector<T>(from.get_array().size());
144+
to = vector<T>(from.get_array().size());
145145
size_t i = 0;
146146
for (auto &value : from.get_array()) {
147147
TRY_STATUS(from_json(to[i], std::move(value)));
@@ -150,6 +150,22 @@ Status from_json(std::vector<T> &to, JsonValue from) {
150150
return Status::OK();
151151
}
152152

153+
inline Status from_json_bytes(vector<string> &to, JsonValue from) {
154+
if (from.type() != JsonValue::Type::Array) {
155+
if (from.type() == JsonValue::Type::Null) {
156+
return Status::OK();
157+
}
158+
return Status::Error(PSLICE() << "Expected Array, but receive " << from.type());
159+
}
160+
to = vector<string>(from.get_array().size());
161+
size_t i = 0;
162+
for (auto &value : from.get_array()) {
163+
TRY_STATUS(from_json_bytes(to[i], std::move(value)));
164+
i++;
165+
}
166+
return Status::OK();
167+
}
168+
153169
template <class T>
154170
std::enable_if_t<!std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue from) {
155171
if (from.type() != JsonValue::Type::Object) {

0 commit comments

Comments
 (0)