nlohmann/json——NLOHMANN_JSON_SERIALIZE_ENUM

目录

源码如下:

源码分析:

使用示例:


源码如下:

/*!
@brief macro to briefly define a mapping between an enum and JSON
@def NLOHMANN_JSON_SERIALIZE_ENUM
@since version 3.4.0
*/
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...)                                    \
    template                                                    \
    inline void to_json(BasicJsonType& j, const ENUM_TYPE& e)                           \
    {                                                                                   \
        static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!");  \
        static const std::pair m[] = __VA_ARGS__;             \
        auto it = std::find_if(std::begin(m), std::end(m),                              \
            [e](const std::pair& ej_pair) -> bool             \
        {                                                                               \
            return ej_pair.first == e;                                                  \
        });                                                                             \
        j = ((it != std::end(m)) ? it : std::begin(m))->second;                         \
    }                                                                                   \
    template                                                    \
    inline void from_json(const BasicJsonType& j, ENUM_TYPE& e)                         \
    {                                                                                   \
        static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!");  \
        static const std::pair m[] = __VA_ARGS__;             \
        auto it = std::find_if(std::begin(m), std::end(m),                              \
            [&j](const std::pair& ej_pair) -> bool            \
        {                                                                               \
            return ej_pair.second == j;                                                 \
        });                                                                             \
        e = ((it != std::end(m)) ? it : std::begin(m))->first;                          \
    }

源码分析:

从注释看,NLOHMANN_JSON_SERIALIZE_ENUM宏是将用户定义的枚举类型与JSON进行映射。其实际上是定义了两个模板函数,to_json和from_json,宏的第一个参数ENUM_TYPE是用户定义的枚举类型,后面携带了可变参数,即...符号。

// asset检查,看ENUM_TYPE是否是枚举,其中#符号为宏中的特殊运算符,在预编译时期,用于将宏参数转换为字符串

static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!");

// __VA_ARGS__为系统自定义变量,指向宏中的可变参数

static const std::pair m[] = __VA_ARGS__;

// 在可变参数指定的map中查找

auto it = std::find_if(std::begin(m), std::end(m),

    [e](const std::pair& ej_pair) -> bool

{

    return ej_pair.first == e;

});

j = ((it != std::end(m)) ? it : std::begin(m))->second;

使用示例:

enum class Type
{
    kType1 = 0,
    kType2 = 1,
    kType3 = 2,
    kType4 = 3,
    kType5 = 4
};

NLOHMANN_JSON_SERIALIZE_ENUM(Type, {
    {Type::kType1, nullptr},
    {Type::kType2, "stopped"},
    {Type::kType3, "running"},
    {Type::kType4, "completed"},
    {Type::kType5, "completed"},
});

int main(int argc, char *argv[])
{
    Type t = Type::kType4;
    nlohmann::json j = t;
    std::cout << "dump:" << j.dump() << std::endl;
    return 0;
}

运行结果如下:

dump:"completed"

你可能感兴趣的:(C++,json,c++,nlohmann)