2 Star 4 Fork 0

Jack.arain / static_json

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

c++/json serialization

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

Comments ( 0 )

Sign in for post a comment

About

Much much fast, direct and static typed parsing of JSON with C++17 spread retract
C++ and 6 more languages
BSL-1.0
Cancel

Releases

No release

static_json

Contributors

All

Activities

load more
can not load any more
C++
1
https://gitee.com/jackarain/static_json.git
git@gitee.com:jackarain/static_json.git
jackarain
static_json
static_json
master

Search

102255 3a0e046c 1850385 102255 7aaa926c 1850385