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"

相关推荐
martian6655 分钟前
深入解析C++驱动开发实战:优化高效稳定的驱动应用
开发语言·c++·驱动开发
FMRbpm31 分钟前
用队列实现栈
数据结构·c++·新手入门
wangjialelele1 小时前
git工作原理、个人使用到多人协作开发与git FLOW模型
c语言·c++·git·团队开发·个人开发
iCxhust1 小时前
__acrtused 是什么
c语言·c++·单片机·嵌入式硬件·微机原理
程序员zgh1 小时前
CMake 项目构建工具介绍
c语言·开发语言·c++·编辑器
量子炒饭大师1 小时前
一天一个计算机知识——【编程百度】向上取整
c语言·数据结构·c++·git·github
Dream it possible!2 小时前
LeetCode 面试经典 150_字典树_添加与搜索单词 - 数据结构设计(96_211_C++_中等)
c++·leetcode·面试·字典树
dragoooon342 小时前
[C++——lesson26.「多态」]
java·c++·学习方法·多态
爱吃KFC的大肥羊2 小时前
Redis 基础完全指南:从全局命令到五大数据结构
java·开发语言·数据库·c++·redis·后端
charlie1145141912 小时前
现代C++工程实践:简单的IniParser4——实现ini_parser
开发语言·c++·笔记·学习·工程