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"

相关推荐
肆忆_12 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星16 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc4 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼4 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛