简易好用的C++结构体与JSON文本相互转换开源库

开源库地址: https ://github .com /bk192077 /struct_mapping .git

支持结构体,std::strng, std::vcetor, std::list, std::map, std::multimap等类型

注意:需要std c++17 及以上版本支持

测试程序:

cpp 复制代码
#include "struct_mapping/struct_mapping.h"

#include <list>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

struct Point
{
	Point()
	{
		struct_mapping::reg(&Point::x, "x");
		struct_mapping::reg(&Point::y, "y");	
		struct_mapping::reg(&Point::z, "z");

	}

	double x = 0.1;
	double y = 0.2;
	double z = 0.3;
};

struct LL
{
	LL()
	{
		struct_mapping::reg(&LL::str, "str");
		struct_mapping::reg(&LL::mlt, "mlt");
	}
	std::string str = "abc";
	std::map<std::string ,int> mlt = {{"zero", 0}, {"one", 1}, {"two", 2}};
};

struct President
{
	President()
	{
		struct_mapping::reg(&President::name, "name");
		struct_mapping::reg(&President::mass, "mass");
		struct_mapping::reg(&President::vec, "vec");
		struct_mapping::reg(&President::lst, "lst");
		struct_mapping::reg(&President::l, "l");
	}
	std::string name="xiaoli-feidao";
	double mass=123.4;
	std::vector<int> vec={1,2,3,4,5};
	std::list<std::string> lst={"abc","def"};
	LL l;
};

struct USA
{
	USA()
	{
		struct_mapping::reg(&USA::president, "president");
	}

	President president;
};

int main()
{
	USA u;
	std::ostringstream out;
	struct_mapping::map_struct_to_json(u, out, " ");//something out stream directly
	std::cout << std::endl;

#if 0
	std::cout << out.str() << std::endl;
#endif
	
	u.president.name = "JACK-LEE";
  u.president.mass = 76.5;
	u.president.lst.push_back("Zara");
	u.president.l.str="Scientific Persons";
	u.president.l.mlt["aaa"] = -2;
	u.president.l.mlt["bbb"] = -1;
	struct_mapping::map_struct_to_json(u, out, " ");
	std::cout << out.str() << std::endl;

#if 0		
	LL AB;
	AB.str = "Scientific Persons";
	AB.mlt["aaa"] = -2;
	AB.mlt["bbb"] = -1;
	struct_mapping::map_struct_to_json(AB, out, "	");
	std::cout << std::endl << out.str() << std::endl;
#endif
}

构建指令: g++ ./struct-lys.cpp -std=c++17 -o sl.out -I /home/lys/usr/local/include

运行结果

{

"president": {

"name": "xiaoli-feidao",

"mass": 123.4,

"vec": [

1,

2,

3,

4,

5

],

"lst": [

"abc",

"def"

],

"l": {

"str": "abc",

"mlt": {

"one": 1,

"two": 2,

"zero": 0

}

}

}

}{

"president": {

"name": "JACK-LEE",

"mass": 76.5,

"vec": [

1,

2,

3,

4,

5

],

"lst": [

"abc",

"def",

"Zara"

],

"l": {

"str": "Scientific Persons",

"mlt": {

"aaa": -2,

"bbb": -1,

"one": 1,

"two": 2,

"zero": 0

}

}

}

}

相关推荐
酷讯网络_240870160几秒前
【全开源】Java多语言tiktok跨境商城TikTok内嵌商城送搭建教程
java·开发语言·开源
午言若13 分钟前
MYSQL 架构
c++·mysql
羑悻的小杀马特2 小时前
【AIGC篇】畅谈游戏开发设计中AIGC所发挥的不可或缺的作用
c++·人工智能·aigc·游戏开发
闻缺陷则喜何志丹2 小时前
【C++动态规划】1105. 填充书架|2104
c++·算法·动态规划·力扣·高度·最小·书架
初学者丶一起加油3 小时前
C语言基础:指针(数组指针与指针数组)
linux·c语言·开发语言·数据结构·c++·算法·visual studio
是小崔啊3 小时前
开源轮子 - HTTP Client组件
网络协议·http·开源
ai产品老杨3 小时前
报警推送消息升级的名厨亮灶开源了。
vue.js·人工智能·安全·开源·音视频
智源研究院官方账号3 小时前
智源研究院与安谋科技达成战略合作,共建开源AI“芯”生态
人工智能·开源
算力魔方AIPC3 小时前
在算力魔方上运行Genesis:一款颠覆性开源生成式物理引擎!
开源
CodeClimb3 小时前
【华为OD-E卷-租车骑绿道 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od