nlohmann::json 超简单序列化反序列化

nlohmann::json 超简单序列化反序列化

上实例

cpp 复制代码
#include <vector>
#include <string>
#include "json.hpp"
using namespace std;
using json = nlohmann::json;


namespace niumabufen
{
	// 公共请求类
	class JsonLocalMessage
	{
	public:
		int m_nUserID{ 0 };              // 用户id
		int m_nVesion{ 0 };              // 版本
		std::string m_strCmd{ "" };      // 信令类型

										 // 类名,成员1,成员2
		NLOHMANN_DEFINE_TYPE_INTRUSIVE(JsonLocalMessage, m_nUserID, m_nVesion, m_strCmd);
	};
}

void test()
{
	// 序列化
	niumabufen::JsonLocalMessage stJsonLocalMessage ;
	stJsonLocalMessage .m_nUserID = 0;
	stJsonLocalMessage .m_nVesion= 1;
	stJsonLocalMessage .m_strCmd = "Cmd";

	json json_JsonLocalMessage  = stJsonLocalMessage;
	std::string strRes = json_JsonLocalMessage.dump();
	std::cout << "Res:" << strRes << std::endl;
	
	// 反序列化
	auto config_json = nlohmann::json::parse(strRes );
	JsonLocalMessage stLocalMessage = config_json;
	std::cout << "UserID:" << stLocalMessage.m_nUserID << std::endl;
	std::cout << "Vesion:" << stLocalMessage.m_nVesion<< std::endl;
	std::cout << "Cmd:" << stLocalMessage.m_strCmd<< std::endl;
}

int main()
{
	test();
	return 0;
}
相关推荐
MediaTea28 分钟前
Python 第三方库:matplotlib(科学绘图与数据可视化)
开发语言·python·信息可视化·matplotlib
草莓熊Lotso36 分钟前
C++ 方向 Web 自动化测试入门指南:从概念到 Selenium 实战
前端·c++·python·selenium
JS.Huang42 分钟前
【JavaScript】原生函数
开发语言·javascript·ecmascript
CoderCodingNo2 小时前
【GESP】C++五级考试大纲知识点梳理, (5) 算法复杂度估算(多项式、对数)
开发语言·c++·算法
星河队长2 小时前
VS创建C++动态库和C#访问过程
java·c++·c#
ftpeak2 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化
一个很帅的帅哥3 小时前
JavaScript事件循环
开发语言·前端·javascript
驰羽3 小时前
[GO]gin框架:ShouldBindJSON与其他常见绑定方法
开发语言·golang·gin
程序员大雄学编程3 小时前
「用Python来学微积分」5. 曲线的极坐标方程
开发语言·python·微积分
沐怡旸3 小时前
【穿越Effective C++】条款02:尽量以const, enum, inline替换#define
c++·面试