简易好用的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

}

}

}

}

相关推荐
时序数据说1 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
No0d1es4 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
DjangoJason6 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq
weixin_307779138 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法
曼妥思8 小时前
PosterKit:跨框架海报生成工具
前端·开源
爱喝奶茶的企鹅9 小时前
Ethan独立开发新品速递 | 2025-08-18
人工智能·程序员·开源
程序媛Dev9 小时前
还在 SSH 连服务器敲 SQL?我用 Sealos 把数据库管理后台搬进了浏览器!
开源·github
蒋星熠10 小时前
C++零拷贝网络编程实战:从理论到生产环境的性能优化之路
网络·c++·人工智能·深度学习·性能优化·系统架构
CHANG_THE_WORLD10 小时前
# C++ 中的 `string_view` 和 `span`:现代安全视图指南
开发语言·c++
雨落倾城夏未凉10 小时前
9.c++new申请二维数组
c++·后端