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;
}
相关推荐
XZ-07000119 分钟前
week2-1-可视化
开发语言·python
Java后端的Ai之路27 分钟前
14、Python - 责任链模式
服务器·开发语言·人工智能·python·责任链模式
吴声子夜歌40 分钟前
Java——基本类型
java·开发语言
江安下小雨43 分钟前
muduo网络库(十六):新增连接池模块
网络·c++
「QT(C++)开发工程师」1 小时前
C++11 std::unique_ptr — 独占所有权的智能指针
c语言·开发语言·c++·qt
不正经学生1 小时前
C语言动态内存管理(上):堆上的自由与责任
c语言·开发语言·c++·算法·面试
AI情绪识别开源2 小时前
检信AI高一分班分科智选评估系统 v2.0测试报告
开发语言·数据结构·人工智能·科技
纯.Pure_Jin(g)2 小时前
靶场PHP函数整理+防御方式
开发语言·php·ctf
zl.rs2 小时前
配置更适合生产环境的coredump
c++
海兰2 小时前
【 Python 量化交易】第3章:金融基础概念
开发语言·python·金融