json_object draft

GitOrigin-RevId: 571b710cc58c0271c0f78a9e1a1263b6858730f3
This commit is contained in:
Arseny Smirnov 2018-04-11 13:49:04 +03:00
parent 4910a36a13
commit 293b706c74
2 changed files with 32 additions and 5 deletions

View File

@ -203,8 +203,13 @@ void DeviceTokenManager::register_device(tl_object_ptr<td_api::DeviceToken> devi
Slice auth_; Slice auth_;
}; };
token = json_encode<string>( token = json_encode<string>(json_object([&device_token](auto &o) {
JsonWebPushToken(device_token->endpoint_, device_token->p256dh_base64url_, device_token->auth_base64url_)); o("endpoint", device_token->endpoint_);
o("keys", json_object([&device_token](auto &o) {
o("pb256df", device_token->p256dh_base64url_);
o("auth", device_token->auth_base64url_);
}));
}));
} }
token_type = TokenType::WEB_PUSH; token_type = TokenType::WEB_PUSH;
break; break;

View File

@ -375,19 +375,23 @@ class JsonObjectScope : public JsonScope {
} }
template <class S, class T> template <class S, class T>
JsonObjectScope &operator<<(std::tuple<S, T> key_value) { JsonObjectScope &operator<<(std::tuple<S, T> key_value) {
return *this << std::pair<S, T>(std::get<0>(key_value), std::get<1>(key_value)); return (*this)(std::get<0>(key_value), std::get<1>(key_value));
} }
template <class S, class T> template <class S, class T>
JsonObjectScope &operator<<(std::pair<S, T> key_value) { JsonObjectScope &operator<<(std::pair<S, T> key_value) {
return (*this)(key_value.first, key_value.second);
}
template <class S, class T>
JsonObjectScope &operator()(S &&key, T &&value) {
CHECK(is_active()); CHECK(is_active());
if (is_first_) { if (is_first_) {
*sb_ << ","; *sb_ << ",";
} else { } else {
is_first_ = true; is_first_ = true;
} }
jb_->enter_value() << key_value.first; jb_->enter_value() << key;
*sb_ << ":"; *sb_ << ":";
jb_->enter_value() << key_value.second; jb_->enter_value() << key;
return *this; return *this;
} }
JsonObjectScope &operator<<(const JsonRaw &key_value) { JsonObjectScope &operator<<(const JsonRaw &key_value) {
@ -763,6 +767,24 @@ void to_json(JsonValueScope &jv, const T &value) {
jv << value; jv << value;
} }
template <class F>
class JsonObjectImpl : Jsonable {
public:
JsonObjectImpl(F &&f) : f_(std::forward<F>(f)) {
}
void store(JsonValueScope *scope) const {
auto object = scope->enter_object();
f_(object);
}
private:
F f_;
};
template <class F>
auto json_object(F &&f) {
return JsonObjectImpl<F>(std::forward<F>(f));
}
bool has_json_object_field(JsonObject &object, Slice name); bool has_json_object_field(JsonObject &object, Slice name);
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type, Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type,