nlohmann/json——NLOHMANN_JSON_SERIALIZE_ENUM

目录

源码如下:

源码分析:

使用示例:


源码如下:

cpp 复制代码
/*!
@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<typename BasicJsonType>                                                    \
    inline void to_json(BasicJsonType& j, const ENUM_TYPE& e)                           \
    {                                                                                   \
        static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");  \
        static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;             \
        auto it = std::find_if(std::begin(m), std::end(m),                              \
            [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool             \
        {                                                                               \
            return ej_pair.first == e;                                                  \
        });                                                                             \
        j = ((it != std::end(m)) ? it : std::begin(m))->second;                         \
    }                                                                                   \
    template<typename BasicJsonType>                                                    \
    inline void from_json(const BasicJsonType& j, ENUM_TYPE& e)                         \
    {                                                                                   \
        static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");  \
        static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;             \
        auto it = std::find_if(std::begin(m), std::end(m),                              \
            [&j](const std::pair<ENUM_TYPE, BasicJsonType>& 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<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");

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

static const std::pair<ENUM_TYPE, BasicJsonType> 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; ## 使用示例: ```cpp 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"

相关推荐
感哥4 小时前
C++ 多态
c++
沐怡旸11 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River41614 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥17 小时前
C++ std::set
c++
侃侃_天下17 小时前
最终的信号类
开发语言·c++·算法
博笙困了18 小时前
AcWing学习——差分
c++·算法
青草地溪水旁18 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁18 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥18 小时前
C++ std::vector
c++
zl_dfq18 小时前
C++ 之【C++11的简介】(可变参数模板、lambda表达式、function\bind包装器)
c++