Skip to content

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["integer"] = 1;
j["float"] = 1.5;
j["string"] = "something";
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{
    { "id", 10 },
    { "name", "Nomango" }
};

// 构造更复杂的对象
json::value j2 = json::object{
    { "null", nullptr },
    { "integer", 1 },
    { "float", 1.3 },
    { "boolean", true },
    { "string", "something" },
    { "array", json::array{ 1, 2 } },
    { "object", json::object{
        { "key", "value" },
        { "key2", "value2" },
    }},
};

类型转换与序列化

struct User
{
    std::string name;
    int age;

    // 将自定义类型绑定到 configor
    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}};

// User -> string
std::string str = json::dump(User{"John", 18});
// string -> User
User u = json::parse("{\"name\": \"John\", \"age\": 18}");

// User -> stream
std::cout << json::wrap(User{"John", 18});
// stream -> User
User u;
std::cin >> json::wrap(u);

Overview

  • Quick start ( en | zh )
  • Examples ( en | zh )

Features

  • Value operation ( en | zh )
  • Serialization ( en | zh )
  • Custom conversion ( en | zh )
  • Encoding support ( en | zh )

Other

  • Q&A ( en | zh )
  • Advanced ( en | zh )
Clone this wiki locally