All Projects → Donerkebap13 → Donerserializer

Donerkebap13 / Donerserializer

Licence: other
A C++14 JSON Serialization Library

Programming Languages

cpp
1120 projects
cpp14
131 projects

Projects that are alternatives of or similar to Donerserializer

Uecs
Ubpa Entity-Component-System (U ECS) in Unity3D-style
Stars: ✭ 174 (+461.29%)
Mutual labels:  game-development, game-engine, game-dev, architectural-patterns
O2
2D Game Engine with visual WYSIWYG editor
Stars: ✭ 121 (+290.32%)
Mutual labels:  framework, game-development, game-engine, game-dev
Entt
Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more
Stars: ✭ 6,017 (+19309.68%)
Mutual labels:  game-development, game-engine, game-dev, architectural-patterns
Gamedev Resources
🎮 🎲 A wonderful list of Game Development resources.
Stars: ✭ 2,054 (+6525.81%)
Mutual labels:  game-development, game-engine, game-dev
Is Engine
SFML C++ game engine that allows to create games on Web (HTML 5 - CSS 3), Android and PC
Stars: ✭ 94 (+203.23%)
Mutual labels:  game-development, game-engine, game-dev
Goluwa
a game framework written in luajit
Stars: ✭ 173 (+458.06%)
Mutual labels:  game-development, game-engine, game-dev
Duality
a 2D Game Development Framework
Stars: ✭ 1,231 (+3870.97%)
Mutual labels:  framework, game-development, game-engine
Lumberyard
Amazon Lumberyard is a free AAA game engine deeply integrated with AWS and Twitch – with full source.
Stars: ✭ 1,785 (+5658.06%)
Mutual labels:  game-development, game-engine, game-dev
Pydark
PyDark is a 2D and Online Multiplayer video game framework written on-top of Python and PyGame.
Stars: ✭ 201 (+548.39%)
Mutual labels:  framework, game-development, game-engine
Awesome Haxe Gamedev
Resources for game development on haxe
Stars: ✭ 213 (+587.1%)
Mutual labels:  framework, game-development, game-engine
Glas
WebGL in WebAssembly with AssemblyScript
Stars: ✭ 278 (+796.77%)
Mutual labels:  game-development, game-engine, game-dev
Fxgl
Stars: ✭ 2,378 (+7570.97%)
Mutual labels:  framework, game-development, game-engine
Iogrid
Multiplayer game engine/framework built using SocketCluster and Phaser
Stars: ✭ 455 (+1367.74%)
Mutual labels:  framework, game-development, game-engine
Luxe Alpha
luxe alpha - deprecated, unrelated to the new engine! see the readme or website for details - https://luxeengine.com/
Stars: ✭ 559 (+1703.23%)
Mutual labels:  game-development, game-engine, game-dev
Ktx
LibKTX: Kotlin extensions for LibGDX games and applications
Stars: ✭ 913 (+2845.16%)
Mutual labels:  game-development, game-engine
Radialprogressbar
Customizable radial progress bar shader for Unity3D. Allows you to set arc range, minimum and maximum colors, textures, radius, and a few more things. Create HP Bars, Speedometers, rank progress, etc!
Stars: ✭ 714 (+2203.23%)
Mutual labels:  game-development, game-dev
Acl
Animation Compression Library
Stars: ✭ 716 (+2209.68%)
Mutual labels:  game-development, game-engine
Avg Core
A Future-oriented Adventure Game Framework based on React & Pixi.js. Docs: https://avgjs.github.io/docs/
Stars: ✭ 740 (+2287.1%)
Mutual labels:  game-development, game-engine
Gameproject3
游戏服务器框架,网络层分别用SocketAPI、Boost Asio、Libuv三种方式实现, 框架内使用共享内存,无锁队列,对象池,内存池来提高服务器性能。还包含一个不断完善的Unity 3D客户端,客户端含大量完整资源,坐骑,宠物,伙伴,装备, 这些均己实现上阵和穿戴, 并可进入副本战斗,多人玩法也己实现, 持续开发中。
Stars: ✭ 655 (+2012.9%)
Mutual labels:  game-development, game-engine
Ursina
A game engine powered by python and panda3d.
Stars: ✭ 730 (+2254.84%)
Mutual labels:  game-development, game-engine

Doner Serializer

Release version Build Status Build status

A C++14 header-only library to serialize your class data to JSON

DonerSerializer is a C++14 header-only library that provides you a simple interface to serialize/deserialize your class data in a few lines of code.

Internally it uses:

Supported types

Built-in types

  • std::int32_t
  • std::uint32_t
  • std::int64_t
  • std::uint64_t
  • float
  • double
  • bool

Std containers

  • std::string
  • std::vector
  • std::list
  • std::map
  • std::unordered_map

User-defined Types

Thirdparty Types

Downloading

You can acquire stable releases here.

Alternatively, you can check out the current development version with:

git clone https://github.com/Donerkebap13/DonerSerializer.git

Contact

You can contact me directly via email. Also, if you have any suggestion or you find any bug, please don't hesitate to create a new Issue.

If you decide to start using DonerSerializer in your project, I'll be glad to hear about it and post it here in the main page as an example!

How to use it

DonerSerializer uses DonerReflection macros to expose your class data.

namespace Foo
{
	struct Bar
	{
		int m_int;
		float m_float;
		std::string m_char;
	}
}
// IMPORTANT!!
// It is mandatory that this macro calls happen always outside of any namespace
DONER_DEFINE_REFLECTION_DATA(Foo::Bar,
	DONER_ADD_NAMED_VAR_INFO(m_int, "intFoo"),
	DONER_ADD_NAMED_VAR_INFO(m_float, "floatBar"),
	DONER_ADD_NAMED_VAR_INFO(m_char, "charMander")
)

As simple as that. Those macros will define a struct containing all the registered members info for that class. As the comment say, this calls should be done outside of any namespace. Otherwise it won't work. Also, if you don't care about how each member is called in the json, you can use the following macro instead:

DONER_DEFINE_REFLECTION_DATA(Foo::Bar,
	DONER_ADD_VAR_INFO(m_int),
	DONER_ADD_VAR_INFO(m_float),
	DONER_ADD_VAR_INFO(m_char)
)

The name in this case will be the same as the variable name.

In order to access protected/private members, you need to use the following macro:

namespace Foo
{
	class Bar
	{
	DONER_DECLARE_OBJECT_AS_REFLECTABLE(Bar)
	private:
		int m_int;
		float m_float;
		std::string m_char;
	}
}
// IMPORTANT!!
// It is mandatory that this macro calls happen always outside of any namespace
DONER_DEFINE_REFLECTION_DATA(Foo::Bar,
	DONER_ADD_NAMED_VAR_INFO(m_int, "intFoo"),
	DONER_ADD_NAMED_VAR_INFO(m_float, "floatBar"),
	DONER_ADD_NAMED_VAR_INFO(m_char, "charMander")
)

By doing this you allow DonerSerializer to access private members information.

Inheritance

DonerSerializer doesn't support inheritance per se. If you want to serialize class members inherited from its upper class, you need to do as follows:

class Foo
{
protected:
	int m_int;
}

class Bar : public Foo
{
DONER_DECLARE_OBJECT_AS_REFLECTABLE(Bar)
private:
	float m_float;
}

DONER_DEFINE_REFLECTION_DATA(Bar,
	DONER_ADD_VAR_INFO(m_int),
	DONER_ADD_VAR_INFO(m_float)
)

Even if the upper class have some reflection data defined, this information is not transitive, so you need to re-declare it for any children class.

How to Serialize

You just need to use DonerSerializer::CJsonSerializer

CFoo foo;
foo.m_int = 1337;

DonerSerializer::CJsonSerializer serializer;
serializer.Serialize(foo);
std::string result = serializer.GetJsonString(); // value is {"m_int": 1337}

You can also get the rapidjson::Document with all the contents:

rapidjson::Document& document = serializer.GetJsonDocument();

If you rather prefer to use your own rapidjson::Document, you can use the static method Serialize:

CFoo foo;
foo.m_int = 1337;
rapidjson::Document document;
// ...
// Any changes to document
// ...
DonerSerializer::CJsonSerializer::Serialize(foo, document);

How to Deserialize

You just need to load the json and use the static method CJsonDeserializer::Deserialize

CFoo foo;
DonerSerializer::CJsonDeserializer::Deserialize(foo, "{\"m_int\": 1337}");
// foo.m_int == 1337 

You can also specify a specific rapidjson::Value to deserialize data from:

rapidjson::Value value;
// ...
// Fill value with some data
// ...
CFoo foo;
DonerSerializer::CJsonDeserializer::Deserialize(foo, value);

How to Serialize your custom classes

In order to serialize you own classes, you just need to inherit from DonerSerialization::ISerializable and to define the desired reflection data as mentioned above

class Foo : DonerSerialization::ISerializable
{
DONER_DECLARE_OBJECT_AS_REFLECTABLE(Foo)
protected:
	int m_int;
}
DONER_DEFINE_REFLECTION_DATA(Foo,
	DONER_ADD_VAR_INFO(m_int)
)
// ...
class Bar : public Foo
{
DONER_DECLARE_OBJECT_AS_REFLECTABLE(Bar)
private:
	Foo m_foo;
}
DONER_DEFINE_REFLECTION_DATA(Bar,
	DONER_ADD_VAR_INFO(m_foo)
)

How to Serialize Thirdparty types

A thirdparty type is a type defined in any external library, where you can't change the implementation of the types to make them usable by DonerSerializer.

To achieve this, you can specialize CDeserializationResolver::CDeserializationResolverType<> and CSerializationResolver::CSerializationResolverType<>. Here's an example on how to do it for SFML sf::Vector2f:

namespace DonerSerializer
{
	template <>
	class CDeserializationResolver::CDeserializationResolverType<sf::Vector2f>
	{
	public:
		static void Apply(sf::Vector2f& value, const rapidjson::Value& att)
		{
			if (att.IsArray())
			{
				value = sf::Vector2f(att[0].GetFloat(), att[1].GetFloat());
			}
		}
	};
	
	template <>
	class CSerializationResolver::CSerializationResolverType<sf::Vector2f>
	{
	public:
		static void Apply(const char* name, const sf::Vector2f& value, rapidjson::Document& root)
		{
			rapidjson::Value array(rapidjson::kArrayType);
			CSerializationResolver::CSerializationResolverType<float>::SerializeToJsonArray(array, value.x, root.GetAllocator());
			CSerializationResolver::CSerializationResolverType<float>::SerializeToJsonArray(array, value.y, root.GetAllocator());
			root.AddMember(rapidjson::GenericStringRef<char>(name), array, root.GetAllocator());
		}

		static void SerializeToJsonArray(rapidjson::Value& root, const sf::Vector2f& value, rapidjson::Document::AllocatorType& allocator)
		{
			rapidjson::Value array(rapidjson::kArrayType);
			CSerializationResolver::CSerializationResolverType<float>::SerializeToJsonArray(array, value.x, allocator);
			CSerializationResolver::CSerializationResolverType<float>::SerializeToJsonArray(array, value.y, allocator);
			root.PushBack(array, allocator);
		}
	};
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].