1 Star 0 Fork 0

yunf-tech/utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
http_rep.cpp 2.70 KB
一键复制 编辑 原始数据 按行查看 历史
Yunfeng.Bo 提交于 2023-05-26 13:16 . 增加 module
#include "http_rep.h"
json_rep str_to_json_object(std::string_view req_body, json::object& obj) {
json::error_code ec;
json::value v = json::parse(req_body, ec);
if (ec)
return HTTP_NOT_JSON(req_body);
if (!v.is_object())
return HTTP_NOT_JSON_OBJ(req_body);
obj = v.as_object();
return HTTP_SUCCESS;
}
// 在JSON对象中,查找指定的参数(std::string)
json_rep get_string(json::object& obj, std::string_view param_name, std::string& param) {
const json::value* v = obj.if_contains(param_name);
if (nullptr == v)
return HTTP_FIELD_NOT_FOUND(param_name);
if (!v->is_string())
return HTTP_FIELD_TYPE_ERR(param_name, "string");
param = v->as_string();
return HTTP_SUCCESS;
}
// 在JSON对象中,查找指定的参数(int int64)
json_rep get_int(json::object& obj, std::string_view param_name, int& param) {
const json::value* v = obj.if_contains(param_name);
if (nullptr == v)
return HTTP_FIELD_NOT_FOUND(param_name);
if (!v->is_int64())
return HTTP_FIELD_TYPE_ERR(param_name, "int");
param = (int)v->as_int64();
return HTTP_SUCCESS;
}
// 在JSON对象中,查找指定的参数(uint64_t)
json_rep get_uint(json::object& obj, std::string_view param_name, uint64_t& param) {
const json::value* v = obj.if_contains(param_name);
if (nullptr == v)
return HTTP_FIELD_NOT_FOUND(param_name);
if (!v->is_uint64())
return HTTP_FIELD_TYPE_ERR(param_name, "uint");
param = v->as_uint64();
return HTTP_SUCCESS;
}
// 在JSON对象中,查找指定的参数(double)
json_rep get_double(json::object& obj, std::string_view param_name, double& param) {
const json::value* v = obj.if_contains(param_name);
if (nullptr == v)
return HTTP_FIELD_NOT_FOUND(param_name);
if (!v->is_double())
return HTTP_FIELD_TYPE_ERR(param_name, "double");
param = v->as_double();
return HTTP_SUCCESS;
}
// 在JSON对象中,查找指定的参数(bool)
json_rep get_bool(json::object& obj, std::string_view param_name, bool& param) {
const json::value* v = obj.if_contains(param_name);
if (nullptr == v)
return HTTP_FIELD_NOT_FOUND(param_name);
if (!v->is_bool())
return HTTP_FIELD_TYPE_ERR(param_name, "bool");
param = v->as_bool();
return HTTP_SUCCESS;
}
json_rep get_object(json::object& obj, std::string_view param_name, json::object& param) {
const json::value* v = obj.if_contains(param_name);
if (nullptr == v)
return HTTP_FIELD_NOT_FOUND(param_name);
if (!v->is_object())
return HTTP_FIELD_TYPE_ERR(param_name, "object");
param = v->as_object();
return HTTP_SUCCESS;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yunf-tech/utils.git
git@gitee.com:yunf-tech/utils.git
yunf-tech
utils
utils
master

搜索帮助