Allow String as a Number in get_json_object_int_field.

GitOrigin-RevId: f7c17eb1f6f8caa3d078d4eafe2de3b58e34002f
This commit is contained in:
levlam 2019-03-25 18:09:46 +03:00
parent 6235ec62b0
commit afa4871028

View File

@ -619,11 +619,22 @@ 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) {
TRY_RESULT(value, get_json_object_field(object, name, JsonValue::Type::Number, is_optional));
if (value.type() == JsonValue::Type::Null) {
for (auto &field_value : object) {
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 to_integer_safe<int32>(value.get_number());
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
}
Result<double> get_json_object_double_field(JsonObject &object, Slice name, bool is_optional, double default_value) {