Coder Social home page Coder Social logo

zhaoyaogit / static_json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from avplayer/static_json

0.0 1.0 0.0 1.08 MB

Much much fast, direct and static typed parsing of JSON with C++17

License: Boost Software License 1.0

CMake 1.49% C++ 95.08% Python 0.69% Makefile 0.01% CSS 0.50% HTML 0.11% Dockerfile 0.02% C 1.85% JavaScript 0.01% Shell 0.24%

static_json's Introduction

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

static_json's People

Contributors

jackarain avatar xosdy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.