2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-12-24 01:55:36 +01:00
|
|
|
#include "td/tl/TlObject.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/base64.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/JsonBuilder.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
2021-09-12 18:04:22 +02:00
|
|
|
#include "td/utils/TlDowncastHelper.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
struct JsonInt64 {
|
|
|
|
int64 value;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void to_json(JsonValueScope &jv, const JsonInt64 json_int64) {
|
|
|
|
jv << JsonString(PSLICE() << json_int64.value);
|
|
|
|
}
|
2019-12-23 01:21:51 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct JsonVectorInt64 {
|
|
|
|
const std::vector<int64> &value;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void to_json(JsonValueScope &jv, const JsonVectorInt64 &vec) {
|
|
|
|
auto ja = jv.enter_array();
|
|
|
|
for (auto &value : vec.value) {
|
|
|
|
ja.enter_value() << ToJson(JsonInt64{value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void to_json(JsonValueScope &jv, const tl_object_ptr<T> &value) {
|
|
|
|
if (value) {
|
|
|
|
to_json(jv, *value);
|
|
|
|
} else {
|
|
|
|
jv << JsonNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void to_json(JsonValueScope &jv, const std::vector<T> &v) {
|
|
|
|
auto ja = jv.enter_array();
|
|
|
|
for (auto &value : v) {
|
|
|
|
ja.enter_value() << ToJson(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:44:41 +01:00
|
|
|
inline Status from_json(int32 &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) {
|
2019-12-08 08:44:41 +01:00
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected Number, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number();
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(to, to_integer_safe<int32>(number));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:44:41 +01:00
|
|
|
inline Status from_json(bool &to, JsonValue from) {
|
2021-10-17 12:07:20 +02:00
|
|
|
auto from_type = from.type();
|
|
|
|
if (from_type != JsonValue::Type::Boolean) {
|
|
|
|
if (from_type == JsonValue::Type::Null) {
|
2019-12-08 08:44:41 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-09-05 17:32:45 +02:00
|
|
|
int32 x = 0;
|
2019-12-08 08:44:41 +01:00
|
|
|
auto status = from_json(x, std::move(from));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status.is_ok()) {
|
|
|
|
to = x != 0;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-10-17 12:07:20 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected Boolean, got " << from_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
to = from.get_boolean();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:44:41 +01:00
|
|
|
inline Status from_json(int64 &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) {
|
2019-12-08 08:44:41 +01:00
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected String or Number, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number();
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(to, to_integer_safe<int64>(number));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
|
2019-12-08 08:44:41 +01:00
|
|
|
inline Status from_json(double &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::Number) {
|
2019-12-08 08:44:41 +01:00
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected Number, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-03 21:01:33 +01:00
|
|
|
to = to_double(from.get_number());
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:44:41 +01:00
|
|
|
inline Status from_json(string &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::String) {
|
2019-12-08 08:44:41 +01:00
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected String, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
to = from.get_string().str();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:44:41 +01:00
|
|
|
inline Status from_json_bytes(string &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::String) {
|
2019-12-08 08:44:41 +01:00
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected String, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(to, base64_decode(from.get_string()));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2019-12-08 08:44:41 +01:00
|
|
|
Status from_json(std::vector<T> &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::Array) {
|
2019-12-08 08:44:41 +01:00
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected Array, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
to = std::vector<T>(from.get_array().size());
|
|
|
|
size_t i = 0;
|
|
|
|
for (auto &value : from.get_array()) {
|
2019-12-08 08:44:41 +01:00
|
|
|
TRY_STATUS(from_json(to[i], std::move(value)));
|
2018-12-31 20:04:05 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2019-12-08 08:44:41 +01:00
|
|
|
std::enable_if_t<!std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::Object) {
|
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
to = nullptr;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected Object, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &object = from.get_object();
|
|
|
|
TRY_RESULT(constructor_value, get_json_object_field(object, "@type", JsonValue::Type::Null, false));
|
|
|
|
int32 constructor = 0;
|
|
|
|
if (constructor_value.type() == JsonValue::Type::Number) {
|
|
|
|
constructor = to_integer<int32>(constructor_value.get_number());
|
|
|
|
} else if (constructor_value.type() == JsonValue::Type::String) {
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(constructor, tl_constructor_from_string(to.get(), constructor_value.get_string().str()));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected String or Integer, got " << constructor_value.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-12 18:04:22 +02:00
|
|
|
TlDowncastHelper<T> helper(constructor);
|
2018-12-31 20:04:05 +01:00
|
|
|
Status status;
|
|
|
|
bool ok = downcast_call(static_cast<T &>(helper), [&](auto &dummy) {
|
|
|
|
auto result = make_tl_object<std::decay_t<decltype(dummy)>>();
|
|
|
|
status = from_json(*result, object);
|
|
|
|
to = std::move(result);
|
|
|
|
});
|
|
|
|
TRY_STATUS(std::move(status));
|
|
|
|
if (!ok) {
|
|
|
|
return Status::Error(PSLICE() << "Unknown constructor " << format::as_hex(constructor));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2019-12-08 08:44:41 +01:00
|
|
|
std::enable_if_t<std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue from) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (from.type() != JsonValue::Type::Object) {
|
|
|
|
if (from.type() == JsonValue::Type::Null) {
|
|
|
|
to = nullptr;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-05-20 14:30:36 +02:00
|
|
|
return Status::Error(PSLICE() << "Expected Object, got " << from.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
to = make_tl_object<T>();
|
|
|
|
return from_json(*to, from.get_object());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|