Make JsonObject class.
This commit is contained in:
parent
6a6cd8af76
commit
59044294a8
@ -41,7 +41,7 @@ static td_api::object_ptr<td_api::JsonValue> get_json_value_object(const JsonVal
|
||||
return td_api::make_object<td_api::jsonValueArray>(transform(json_value.get_array(), get_json_value_object));
|
||||
case JsonValue::Type::Object:
|
||||
return td_api::make_object<td_api::jsonValueObject>(
|
||||
transform(json_value.get_object(), get_json_value_member_object));
|
||||
transform(json_value.get_object().field_values_, get_json_value_member_object));
|
||||
default:
|
||||
UNREACHABLE();
|
||||
return nullptr;
|
||||
|
@ -3028,7 +3028,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
|
||||
string announcement_message_text;
|
||||
vector<string> loc_args;
|
||||
string sender_name;
|
||||
for (auto &field_value : data) {
|
||||
for (auto &field_value : data.field_values_) {
|
||||
if (field_value.first == "loc_key") {
|
||||
if (field_value.second.type() != JsonValue::Type::String) {
|
||||
return Status::Error("Expected loc_key as a String");
|
||||
@ -3971,7 +3971,7 @@ Result<int64> NotificationManager::get_push_receiver_id(string payload) {
|
||||
data = std::move(data_data.get_object());
|
||||
}
|
||||
|
||||
for (auto &field_value : data) {
|
||||
for (auto &field_value : data.field_values_) {
|
||||
if (field_value.first == "p") {
|
||||
auto encrypted_payload = std::move(field_value.second);
|
||||
if (encrypted_payload.type() != JsonValue::Type::String) {
|
||||
@ -4019,7 +4019,7 @@ Result<string> NotificationManager::decrypt_push(int64 encryption_key_id, string
|
||||
return Status::Error(400, "Expected JSON object");
|
||||
}
|
||||
|
||||
for (auto &field_value : json_value.get_object()) {
|
||||
for (auto &field_value : json_value.get_object().field_values_) {
|
||||
if (field_value.first == "p") {
|
||||
auto encrypted_payload = std::move(field_value.second);
|
||||
if (encrypted_payload.type() != JsonValue::Type::String) {
|
||||
|
@ -226,7 +226,7 @@ static tl_object_ptr<td_api::PaymentProvider> convert_payment_provider(
|
||||
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
|
||||
return nullptr;
|
||||
}
|
||||
if (value.get_object().size() != 1) {
|
||||
if (value.get_object().field_values_.size() != 1) {
|
||||
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
|
||||
}
|
||||
|
||||
@ -257,7 +257,7 @@ static tl_object_ptr<td_api::PaymentProvider> convert_payment_provider(
|
||||
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
|
||||
return nullptr;
|
||||
}
|
||||
if (value.get_object().size() != 5) {
|
||||
if (value.get_object().field_values_.size() != 5) {
|
||||
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
|
||||
}
|
||||
|
||||
|
@ -394,7 +394,7 @@ Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
|
||||
case '{': {
|
||||
parser.skip('{');
|
||||
parser.skip_whitespaces();
|
||||
std::vector<std::pair<MutableSlice, JsonValue>> res;
|
||||
JsonObject res;
|
||||
if (parser.try_skip('}')) {
|
||||
return JsonValue::make_object(std::move(res));
|
||||
}
|
||||
@ -402,13 +402,13 @@ Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
|
||||
if (parser.empty()) {
|
||||
return Status::Error("Unexpected string end");
|
||||
}
|
||||
TRY_RESULT(key, json_string_decode(parser));
|
||||
TRY_RESULT(field, json_string_decode(parser));
|
||||
parser.skip_whitespaces();
|
||||
if (!parser.try_skip(':')) {
|
||||
return Status::Error("':' expected");
|
||||
}
|
||||
TRY_RESULT(value, do_json_decode(parser, max_depth - 1));
|
||||
res.emplace_back(key, std::move(value));
|
||||
res.field_values_.emplace_back(field, std::move(value));
|
||||
|
||||
parser.skip_whitespaces();
|
||||
if (parser.try_skip('}')) {
|
||||
@ -586,7 +586,7 @@ Slice JsonValue::get_type_name(Type type) {
|
||||
}
|
||||
|
||||
bool has_json_object_field(const JsonObject &object, Slice name) {
|
||||
for (auto &field_value : object) {
|
||||
for (auto &field_value : object.field_values_) {
|
||||
if (field_value.first == name) {
|
||||
return true;
|
||||
}
|
||||
@ -595,7 +595,7 @@ bool has_json_object_field(const JsonObject &object, Slice name) {
|
||||
}
|
||||
|
||||
JsonValue get_json_object_field_force(JsonObject &object, Slice name) {
|
||||
for (auto &field_value : object) {
|
||||
for (auto &field_value : object.field_values_) {
|
||||
if (field_value.first == name) {
|
||||
return std::move(field_value.second);
|
||||
}
|
||||
@ -604,7 +604,7 @@ JsonValue get_json_object_field_force(JsonObject &object, Slice name) {
|
||||
}
|
||||
|
||||
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type, bool is_optional) {
|
||||
for (auto &field_value : object) {
|
||||
for (auto &field_value : object.field_values_) {
|
||||
if (field_value.first == name) {
|
||||
if (type != JsonValue::Type::Null && field_value.second.type() != type) {
|
||||
return Status::Error(400, PSLICE()
|
||||
@ -629,7 +629,7 @@ Result<bool> get_json_object_bool_field(JsonObject &object, Slice name, bool is_
|
||||
}
|
||||
|
||||
Result<int32> get_json_object_int_field(JsonObject &object, Slice name, bool is_optional, int32 default_value) {
|
||||
for (auto &field_value : object) {
|
||||
for (auto &field_value : object.field_values_) {
|
||||
if (field_value.first == name) {
|
||||
if (field_value.second.type() == JsonValue::Type::String) {
|
||||
return to_integer_safe<int32>(field_value.second.get_string());
|
||||
@ -648,7 +648,7 @@ Result<int32> get_json_object_int_field(JsonObject &object, Slice name, bool is_
|
||||
}
|
||||
|
||||
Result<int64> get_json_object_long_field(JsonObject &object, Slice name, bool is_optional, int64 default_value) {
|
||||
for (auto &field_value : object) {
|
||||
for (auto &field_value : object.field_values_) {
|
||||
if (field_value.first == name) {
|
||||
if (field_value.second.type() == JsonValue::Type::String) {
|
||||
return to_integer_safe<int64>(field_value.second.get_string());
|
||||
@ -675,7 +675,7 @@ Result<double> get_json_object_double_field(JsonObject &object, Slice name, bool
|
||||
}
|
||||
|
||||
Result<string> get_json_object_string_field(JsonObject &object, Slice name, bool is_optional, string default_value) {
|
||||
for (auto &field_value : object) {
|
||||
for (auto &field_value : object.field_values_) {
|
||||
if (field_value.first == name) {
|
||||
if (field_value.second.type() == JsonValue::Type::String) {
|
||||
return field_value.second.get_string().str();
|
||||
|
@ -401,7 +401,7 @@ class JsonObjectScope final : public JsonScope {
|
||||
*sb_ << "}";
|
||||
}
|
||||
template <class T>
|
||||
JsonObjectScope &operator()(Slice key, T &&value) {
|
||||
JsonObjectScope &operator()(Slice field, T &&value) {
|
||||
CHECK(is_active());
|
||||
if (is_first_) {
|
||||
*sb_ << ",";
|
||||
@ -409,7 +409,7 @@ class JsonObjectScope final : public JsonScope {
|
||||
is_first_ = true;
|
||||
}
|
||||
jb_->print_offset();
|
||||
jb_->enter_value() << key;
|
||||
jb_->enter_value() << field;
|
||||
if (jb_->is_pretty()) {
|
||||
*sb_ << " : ";
|
||||
} else {
|
||||
@ -418,10 +418,10 @@ class JsonObjectScope final : public JsonScope {
|
||||
jb_->enter_value() << value;
|
||||
return *this;
|
||||
}
|
||||
JsonObjectScope &operator<<(const JsonRaw &key_value) {
|
||||
JsonObjectScope &operator<<(const JsonRaw &field_value) {
|
||||
CHECK(is_active());
|
||||
is_first_ = true;
|
||||
jb_->enter_value() << key_value;
|
||||
jb_->enter_value() << field_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -451,9 +451,13 @@ inline JsonArrayScope JsonBuilder::enter_array() {
|
||||
|
||||
class JsonValue;
|
||||
|
||||
using JsonObject = vector<std::pair<MutableSlice, JsonValue>>;
|
||||
using JsonArray = vector<JsonValue>;
|
||||
|
||||
class JsonObject {
|
||||
public:
|
||||
vector<std::pair<MutableSlice, JsonValue>> field_values_;
|
||||
};
|
||||
|
||||
class JsonValue final : private Jsonable {
|
||||
public:
|
||||
enum class Type { Null, Number, Boolean, String, Array, Object };
|
||||
@ -584,8 +588,8 @@ class JsonValue final : private Jsonable {
|
||||
}
|
||||
case Type::Object: {
|
||||
auto object = scope->enter_object();
|
||||
for (auto &key_value : get_object()) {
|
||||
object(key_value.first, key_value.second);
|
||||
for (auto &field_value : get_object().field_values_) {
|
||||
object(field_value.first, field_value.second);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -664,7 +668,7 @@ class JsonValue final : private Jsonable {
|
||||
array_.~vector<JsonValue>();
|
||||
break;
|
||||
case Type::Object:
|
||||
object_.~vector<std::pair<MutableSlice, JsonValue>>();
|
||||
object_.~JsonObject();
|
||||
break;
|
||||
}
|
||||
type_ = Type::Null;
|
||||
|
Loading…
Reference in New Issue
Block a user