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;
}
相关推荐
2301_7890156224 分钟前
DS进阶:AVL树
开发语言·数据结构·c++·算法
Filotimo_2 小时前
5.3 Internet基础知识
开发语言·php
识君啊2 小时前
Java异常处理:中小厂面试通关指南
java·开发语言·面试·异常处理·exception·中小厂
qyzm4 小时前
天梯赛练习(3月13日)
开发语言·数据结构·python·算法·贪心算法
leluckys4 小时前
swift- Swift中常见的面试题
开发语言·汇编·swift
BUG_MeDe4 小时前
json格式字符串解析的简单使用 libjson-c
c语言·开发语言·json
CoderCodingNo4 小时前
【GESP】C++五级练习题 luogu-P1182 数列分段 Section II
开发语言·c++·算法
Qt学视觉5 小时前
AI2-Paddle环境搭建
c++·人工智能·python·opencv·paddle