GithubHelp home page GithubHelp logo

aint-no-programmer / twin_pack Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 26 KB

Simple cross-platform (Windows/Linux) header-only library for binary and json serialization based on popular libraries msgpack and nlohmann-json

License: MIT License

CMake 10.97% C++ 88.65% C 0.38%
json msgpack nlohmann-json serialization binary

twin_pack's Introduction

twin_pack

Simple cross-platform (Windows/Linux) header-only library for binary and json serialization based on popular libraries msgpack and nlohmann-json

example

#include "twin_pack.h"

using namespace std;

using twin_pack::ISerializableObject;
using twin_pack::property_value;

/*
 * Inherit class from ISerializableObject
 */
class TestMessage : public ISerializableObject
{
public:
	int a;
	double b;
	std::string c;

	/*
	 * property_value class based on boost::variant
	 */
	property_value d;
	std::vector<property_value> m_vector;
	std::map<std::string, property_value> m_map;

	/*
	 * Inherit class from ISerializableObject
	 * In public part of class add macro PACK_IT :
	 * first arg - class name,
	 * other args - fields of class need to be serialized
	*/
	PACK_IT(TestMessage, a, b, c, d, m_vector, m_map)
};

int main()
{
	using nlohmann::json;

	TestMessage test_message;
	test_message.a = 12;
	test_message.b = 34.56;
	test_message.c = "hello world";
	test_message.d = 123;
	test_message.m_vector = {12323.f, 123123213, 4, 56.67, "eve", true};
	test_message.m_map = {
		{"1", 123123123},
		{"2", 3434.3434},
		{"3", 12},
		{"4", 0},
		{"5", true},
		{"6", "QWERTY23123"},
		{"7", false}
	};

	//error message 
	std::string msg_error;

	//serializing to json
	json first_json;
	bool ok = test_message.serialize(first_json, msg_error);
	std::cout << first_json.dump(2) << std::endl;

	//restoring from json
	TestMessage restored_from_json;
	ok = restored_from_json.unserialize(first_json, msg_error);

	//serializing to binary
	std::stringstream stream;
	ok = restored_from_json.serialize(stream, msg_error);
	
	//restoring from binary
	TestMessage restored_from_binary;
	ok = restored_from_binary.unserialize(stream, msg_error);

	//serialize to json for comparing
	json output_json;
	ok = restored_from_binary.serialize(output_json, msg_error);
	std::cout << output_json.dump(2) << std::endl;

	if (first_json == output_json)
	{
		std::cout << "\nequal\n";
	}

	return 0;
}

dependency

twin_pack requires boost, msgpack, nlohmann-json libraries. Tests depend on googletest framework and are linked with it, so if you want to build them (BUILD_TESTS=ON), you need to have this dependency installed.

build

via cmake

mkdir build
cd build
cmake .. -DBoost_DIR=boos_dir -Dnlohmann_json_DIR=nlohmann_json_dir -Dmsgpack-cxx_DIR=msgpack_dir -DGTest_DIR=gtest_dir -DBUILD_TESTS=ON

via vcpkg

mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=<path to vcpkg>\scripts\buildsystems\vcpkg.cmake -DBUILD_TESTS=ON 

license

twin_pack is licensed under MIT Software License, see the LICENSE file for details.

twin_pack's People

Contributors

aint-no-programmer avatar

Watchers

 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.