-
Notifications
You must be signed in to change notification settings - Fork 67
Quick start (zh)
haibo2.liu edited this page Sep 29, 2022
·
5 revisions
下载并引入 configor 头文件
#include "configor/json.hpp"
using namespace configor;以 JSON 为例,创建 document 对象。
使用 operator[] 为 JSON 对象赋值:
json::value j;
j["number"] = 1;
j["float"] = 1.5;
j["string"] = "this is a string";
j["boolean"] = true;
j["user"]["id"] = 10;
j["user"]["name"] = "Nomango";使用 std::initializer_list 为 JSON 对象赋值:
// 构造数组
json::value arr = json::array{ 1, 2, 3 };
// 构造对象
json::value obj = json::object{
{
"user", json::object{
{ "id", 10 },
{ "name", "Nomango" }
}
}
};
// 构造更复杂的对象
json::value obj2 = json::object{
{ "nul", nullptr },
{ "number", 1 },
{ "float", 1.3 },
{ "boolean", false },
{ "string", "中文测试" },
{ "array", json::array{ 1, 2, true, 1.4 } },
{ "object", json::object{
{ "key", "value" },
{ "key2", "value2" },
}},
};快速完成自定义类型与 JSON 转换
struct User {
std::string name;
int age;
// 一行代码完成字段绑定
CONFIGOR_BIND(json::value, User, REQUIRED(name), OPTIONAL(age))
};
// User 转换到 json
json::value j = User{"John", 18};
// json 转换到 User
User u = json::object{{"name", "John"}, {"age", 18}};