Delete deprecated JsonObject accessors.

This commit is contained in:
levlam 2023-08-18 15:06:16 +03:00
parent 203e8cf9c2
commit 40378be487
3 changed files with 0 additions and 274 deletions

View File

@ -726,130 +726,4 @@ void JsonObject::foreach(const std::function<void(Slice name, const JsonValue &v
} }
} }
bool has_json_object_field(const JsonObject &object, Slice name) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
return true;
}
}
return false;
}
JsonValue get_json_object_field_force(JsonObject &object, Slice name) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
return std::move(field_value.second);
}
}
return JsonValue();
}
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type, bool is_optional) {
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()
<< "Field \"" << name << "\" must be of type " << JsonValue::get_type_name(type));
}
return std::move(field_value.second);
}
}
if (!is_optional) {
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
return JsonValue();
}
Result<bool> get_json_object_bool_field(const JsonObject &object, Slice name, bool is_optional, bool default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() != JsonValue::Type::Boolean) {
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Boolean");
}
return field_value.second.get_boolean();
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int32> get_json_object_int_field(const JsonObject &object, Slice name, bool is_optional, int32 default_value) {
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());
}
if (field_value.second.type() == JsonValue::Type::Number) {
return to_integer_safe<int32>(field_value.second.get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int64> get_json_object_long_field(const JsonObject &object, Slice name, bool is_optional, int64 default_value) {
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());
}
if (field_value.second.type() == JsonValue::Type::Number) {
return to_integer_safe<int64>(field_value.second.get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be a Number");
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<double> get_json_object_double_field(const JsonObject &object, Slice name, bool is_optional,
double default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() != JsonValue::Type::Number) {
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
return to_double(field_value.second.get_number());
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<string> get_json_object_string_field(const JsonObject &object, Slice name, bool is_optional,
string default_value) {
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();
}
if (field_value.second.type() == JsonValue::Type::Number) {
return field_value.second.get_number().str();
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type String");
}
}
if (is_optional) {
return std::move(default_value);
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
} // namespace td } // namespace td

View File

@ -895,26 +895,4 @@ auto json_array(const A &a, F &&f) {
}); });
} }
bool has_json_object_field(const JsonObject &object, Slice name);
JsonValue get_json_object_field_force(JsonObject &object, Slice name) TD_WARN_UNUSED_RESULT;
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type,
bool is_optional = true) TD_WARN_UNUSED_RESULT;
Result<bool> get_json_object_bool_field(const JsonObject &object, Slice name, bool is_optional = true,
bool default_value = false) TD_WARN_UNUSED_RESULT;
Result<int32> get_json_object_int_field(const JsonObject &object, Slice name, bool is_optional = true,
int32 default_value = 0) TD_WARN_UNUSED_RESULT;
Result<int64> get_json_object_long_field(const JsonObject &object, Slice name, bool is_optional = true,
int64 default_value = 0) TD_WARN_UNUSED_RESULT;
Result<double> get_json_object_double_field(const JsonObject &object, Slice name, bool is_optional = true,
double default_value = 0.0) TD_WARN_UNUSED_RESULT;
Result<string> get_json_object_string_field(const JsonObject &object, Slice name, bool is_optional = true,
string default_value = "") TD_WARN_UNUSED_RESULT;
} // namespace td } // namespace td

View File

@ -92,132 +92,6 @@ TEST(JSON, kphp) {
"qrstuvwxyz\"]],\"one_time_keyboard\":true}"); "qrstuvwxyz\"]],\"one_time_keyboard\":true}");
} }
TEST(JSON, get_json_object_field) {
const td::string encoded_object =
"{\"null\":null,\"bool\":true,\"int\":\"1\",\"int2\":2,\"long\":\"123456789012\",\"long2\":2123456789012,"
"\"double\":12345678901.1,\"string\":\"string\",\"string2\":12345e+1,\"array\":[],\"object\":{}}";
{
td::string encoded_object_copy = encoded_object;
auto value = td::json_decode(encoded_object_copy).move_as_ok();
auto &object = value.get_object();
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "null")), "null");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "bool")), "true");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "bool")), "null");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "int")), "\"1\"");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "int2")), "2");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "int3")), "null");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "long")), "\"123456789012\"");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "long2")), "2123456789012");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "double")), "12345678901.1");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "string")), "\"string\"");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "string2")), "12345e+1");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "array")), "[]");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "object")), "{}");
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "")), "null");
}
{
td::string encoded_object_copy = encoded_object;
auto value = td::json_decode(encoded_object_copy).move_as_ok();
auto &object = value.get_object();
ASSERT_TRUE(td::get_json_object_field(object, "int", td::JsonValue::Type::Number).is_error());
ASSERT_TRUE(td::get_json_object_field(object, "int", td::JsonValue::Type::Number).is_error());
ASSERT_TRUE(td::get_json_object_field(object, "int2", td::JsonValue::Type::Number).is_ok());
ASSERT_TRUE(td::get_json_object_field(object, "int2", td::JsonValue::Type::Number).is_error());
ASSERT_TRUE(td::get_json_object_field(object, "int3", td::JsonValue::Type::Number).is_ok());
ASSERT_TRUE(td::get_json_object_field(object, "int3", td::JsonValue::Type::Null).is_ok());
ASSERT_EQ(td::get_json_object_field(object, "int", td::JsonValue::Type::String).ok().get_string(), "1");
ASSERT_TRUE(td::get_json_object_field(object, "int", td::JsonValue::Type::Number).is_error());
ASSERT_EQ(td::get_json_object_field(object, "int", td::JsonValue::Type::Null).ok().type(),
td::JsonValue::Type::Null);
ASSERT_TRUE(td::get_json_object_field(object, "long", td::JsonValue::Type::Number, false).is_error());
ASSERT_TRUE(td::get_json_object_field(object, "long", td::JsonValue::Type::Number, false).is_error());
ASSERT_TRUE(td::get_json_object_field(object, "long2", td::JsonValue::Type::Number, false).is_ok());
ASSERT_TRUE(td::get_json_object_field(object, "long2", td::JsonValue::Type::Number, false).is_error());
ASSERT_TRUE(td::get_json_object_field(object, "long3", td::JsonValue::Type::Number, false).is_error());
ASSERT_TRUE(td::get_json_object_field(object, "long3", td::JsonValue::Type::Null, false).is_error());
ASSERT_EQ(td::get_json_object_field(object, "long", td::JsonValue::Type::String, false).ok().get_string(),
"123456789012");
ASSERT_TRUE(td::get_json_object_field(object, "long", td::JsonValue::Type::Number, false).is_error());
ASSERT_EQ(td::get_json_object_field(object, "long", td::JsonValue::Type::Null, false).ok().type(),
td::JsonValue::Type::Null);
}
td::string encoded_object_copy = encoded_object;
auto value = td::json_decode(encoded_object_copy).move_as_ok();
const auto &object = value.get_object();
ASSERT_TRUE(td::has_json_object_field(object, "null"));
ASSERT_TRUE(td::has_json_object_field(object, "object"));
ASSERT_TRUE(!td::has_json_object_field(object, ""));
ASSERT_TRUE(!td::has_json_object_field(object, "objec"));
ASSERT_TRUE(!td::has_json_object_field(object, "object2"));
ASSERT_TRUE(td::get_json_object_bool_field(object, "int").is_error());
ASSERT_EQ(td::get_json_object_bool_field(object, "bool").ok(), true);
ASSERT_EQ(td::get_json_object_bool_field(object, "bool").ok(), true);
ASSERT_EQ(td::get_json_object_bool_field(object, "bool", false).ok(), true);
ASSERT_EQ(td::get_json_object_bool_field(object, "bool3").ok(), false);
ASSERT_EQ(td::get_json_object_bool_field(object, "bool4", true, true).ok(), true);
ASSERT_TRUE(td::get_json_object_bool_field(object, "bool5", false, true).is_error());
ASSERT_TRUE(td::get_json_object_int_field(object, "null").is_error());
ASSERT_EQ(td::get_json_object_int_field(object, "int").ok(), 1);
ASSERT_EQ(td::get_json_object_int_field(object, "int").ok(), 1);
ASSERT_EQ(td::get_json_object_int_field(object, "int", false).ok(), 1);
ASSERT_EQ(td::get_json_object_int_field(object, "int2").ok(), 2);
ASSERT_EQ(td::get_json_object_int_field(object, "int2").ok(), 2);
ASSERT_EQ(td::get_json_object_int_field(object, "int2", false).ok(), 2);
ASSERT_EQ(td::get_json_object_int_field(object, "int3").ok(), 0);
ASSERT_EQ(td::get_json_object_int_field(object, "int4", true, 5).ok(), 5);
ASSERT_TRUE(td::get_json_object_int_field(object, "int5", false, 5).is_error());
ASSERT_EQ(td::get_json_object_int_field(object, "long").is_error(), true);
ASSERT_EQ(td::get_json_object_int_field(object, "long2").is_error(), true);
ASSERT_TRUE(td::get_json_object_long_field(object, "null").is_error());
ASSERT_EQ(td::get_json_object_long_field(object, "long").ok(), 123456789012);
ASSERT_EQ(td::get_json_object_long_field(object, "long").ok(), 123456789012);
ASSERT_EQ(td::get_json_object_long_field(object, "long", false).ok(), 123456789012);
ASSERT_EQ(td::get_json_object_long_field(object, "long2").ok(), 2123456789012);
ASSERT_EQ(td::get_json_object_long_field(object, "long2").ok(), 2123456789012);
ASSERT_EQ(td::get_json_object_long_field(object, "long2", false).ok(), 2123456789012);
ASSERT_EQ(td::get_json_object_long_field(object, "long3").ok(), 0);
ASSERT_EQ(td::get_json_object_long_field(object, "long4", true, 5).ok(), 5);
ASSERT_TRUE(td::get_json_object_long_field(object, "long5", false, 5).is_error());
ASSERT_EQ(td::get_json_object_long_field(object, "int").ok(), 1);
ASSERT_EQ(td::get_json_object_long_field(object, "int2").ok(), 2);
auto are_equal_double = [](double lhs, double rhs) {
return rhs - 1e-3 < lhs && lhs < rhs + 1e-3;
};
ASSERT_TRUE(td::get_json_object_double_field(object, "null").is_error());
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double").ok(), 12345678901.1));
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double").ok(), 12345678901.1));
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double", false).ok(), 12345678901.1));
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "long2").ok(), 2123456789012.0));
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "long2").ok(), 2123456789012.0));
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "long2", false).ok(), 2123456789012.0));
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double3").ok(), 0.0));
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double4", true, -5.23).ok(), -5.23));
ASSERT_TRUE(td::get_json_object_double_field(object, "double5", false, 5).is_error());
ASSERT_TRUE(td::get_json_object_double_field(object, "int").is_error());
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "int2").ok(), 2));
ASSERT_TRUE(td::get_json_object_string_field(object, "null").is_error());
ASSERT_EQ(td::get_json_object_string_field(object, "string").ok(), "string");
ASSERT_EQ(td::get_json_object_string_field(object, "string").ok(), "string");
ASSERT_EQ(td::get_json_object_string_field(object, "string", false).ok(), "string");
ASSERT_EQ(td::get_json_object_string_field(object, "string2").ok(), "12345e+1");
ASSERT_EQ(td::get_json_object_string_field(object, "string2").ok(), "12345e+1");
ASSERT_EQ(td::get_json_object_string_field(object, "string2", false).ok(), "12345e+1");
ASSERT_EQ(td::get_json_object_string_field(object, "string3").ok(), td::string());
ASSERT_EQ(td::get_json_object_string_field(object, "string4", true, "abacaba").ok(), "abacaba");
ASSERT_TRUE(td::get_json_object_string_field(object, "string5", false, "test").is_error());
ASSERT_EQ(td::get_json_object_string_field(object, "int").ok(), "1");
ASSERT_EQ(td::get_json_object_string_field(object, "int2").ok(), "2");
}
TEST(JSON, json_object_get_field) { TEST(JSON, json_object_get_field) {
const td::string encoded_object = const td::string encoded_object =
"{\"null\":null,\"bool\":true,\"int\":\"1\",\"int2\":2,\"long\":\"123456789012\",\"long2\":2123456789012," "{\"null\":null,\"bool\":true,\"int\":\"1\",\"int2\":2,\"long\":\"123456789012\",\"long2\":2123456789012,"