1 Star 0 Fork 0

超哥 / static_json

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSL-1.0

c++/json serialization

Build Status

Introduction

Allows conversion from a C++ structure to json, or from a json to a C++ structure.

Motivation

In some of the past projects, json is often used as a protocol, and when dealing with the corresponding protocol, it is often necessary to reference the value object parsed by json in each required place, and then go through the interface provided by the value json library. Access to the fields needed in the business, which leads to the json library-related code everywhere in the code, adding coupling to the json parsing library.

After understanding the basic principles of boost.serialization, I designed this library. static_json in the process of serialization and deserialization, the overhead is almost negligible, depending on the efficiency of the rapididjson library.

With this library, we only need to define the C++ data structure, and then serialize the json data into the structure, so that when accessing, it is no longer through the interface of the json library, but the structure of c++, thus avoiding json library and The degree of coupling of the project.

How to use

You can simply include static_json.hpp and rapidjson libraries (rapidjson is also header only), or you can copy the code in the include directory to your own project, and then include it, you can start using it.

Get started quickly

#include "static_json.hpp"

int main() {
	using namespace static_json;

	std::vector<int> ai = {1, 3, 4, 7, 9};
	std::string a = to_json_string(ai);

	std::cout << a << std::endl;

	std::vector<int> af;
	from_json_string(af, a);
}

Invasive c++ structure serialization

struct proto {
	int type;
	std::string name;
	double height;
	
	template <typename Archive>
	void serialize(Archive &ar)
	{
		ar	& JSON_SERIALIZATION_NVP(type)
			& JSON_SERIALIZATION_NVP(name)
			& JSON_SERIALIZATION_NVP(height);
	}
};


proto test1 {1, "abcd", 1.83};
std::string a = to_json_string(test1);

proto test2;
from_json_string(test2, a); // test2 same as test1.

Non-intrusive c++ structure serialization

struct proto {
	int type;
	std::string name;
	double height;
};

template<class Archive>
void serialize(Archive& ar, proto& a)
{
	ar	& JSON_NI_SERIALIZATION_NVP(a, type)
		& JSON_NI_SERIALIZATION_NVP(a, name)
		& JSON_NI_SERIALIZATION_NVP(a, height);
}

proto test1 {1, "abcd", 1.83};
std::string a = to_json_string(test1);

proto test2;
from_json_string(test2, a); // test2 same as test1.

For more usage, see src/main.cpp

Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

c++ 转json 展开 收起
BSL-1.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/yhc2020/static_json.git
git@gitee.com:yhc2020/static_json.git
yhc2020
static_json
static_json
master

搜索帮助