All Projects → Xeverous → fuser

Xeverous / fuser

Licence: MIT License
Header-only library for automatic (de)serialization of C++ types to/from JSON.

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to fuser

Lora Serialization
LoraWAN serialization/deserialization library for The Things Network
Stars: ✭ 120 (+150%)
Mutual labels:  serialization, serialization-library
Hprose Php
Hprose is a cross-language RPC. This project is Hprose 3.0 for PHP
Stars: ✭ 1,952 (+3966.67%)
Mutual labels:  serialization, serialization-library
Yaxlib
Yet Another XML Serialization Library for the .NET Framework and .NET Core
Stars: ✭ 124 (+158.33%)
Mutual labels:  serialization, serialization-library
Hprose Golang
Hprose is a cross-language RPC. This project is Hprose for Golang.
Stars: ✭ 1,143 (+2281.25%)
Mutual labels:  serialization, serialization-library
EndianBinaryIO
A C# library that can read and write primitives, enums, arrays, and strings to streams and byte arrays with specified endianness, string encoding, and boolean sizes.
Stars: ✭ 20 (-58.33%)
Mutual labels:  serialization, serialization-library
Succ
Sexy and Utilitarian Code Configuration
Stars: ✭ 100 (+108.33%)
Mutual labels:  serialization, serialization-library
Hprose Js
Hprose is a cross-language RPC. This project is Hprose 2.0 RPC for JavaScript
Stars: ✭ 133 (+177.08%)
Mutual labels:  serialization, serialization-library
Hprose Nodejs
Hprose is a cross-language RPC. This project is Hprose 2.0 for Node.js
Stars: ✭ 297 (+518.75%)
Mutual labels:  serialization, serialization-library
Thorsserializer
C++ Serialization library for JSON
Stars: ✭ 241 (+402.08%)
Mutual labels:  serialization, serialization-library
Hprose Html5
Hprose is a cross-language RPC. This project is Hprose 2.0 Client for HTML5
Stars: ✭ 237 (+393.75%)
Mutual labels:  serialization, serialization-library
Eminim
JSON serialization framework for Nim, works from a Stream directly to any type and back. Depends only on stdlib.
Stars: ✭ 32 (-33.33%)
Mutual labels:  serialization, serialization-library
JsonFormatter
Easy, Fast and Lightweight Json Formatter. (Serializer and Deserializer)
Stars: ✭ 26 (-45.83%)
Mutual labels:  serialization, serialization-library
Hprose Java
Hprose is a cross-language RPC. This project is Hprose 2.0 for Java
Stars: ✭ 542 (+1029.17%)
Mutual labels:  serialization, serialization-library
Hprose Delphi
Hprose is a cross-language RPC. This project is Hprose 2.0 for Delphi and FreePascal
Stars: ✭ 100 (+108.33%)
Mutual labels:  serialization, serialization-library
Typedefs
Programming language agnostic type construction language based on polynomials.
Stars: ✭ 337 (+602.08%)
Mutual labels:  serialization, serialization-library
Json
Lighter and Faster Json Serialization tool.
Stars: ✭ 128 (+166.67%)
Mutual labels:  serialization, serialization-library
Savegamefree
Save Game Free is a free and simple but powerful solution for saving and loading game data in unity.
Stars: ✭ 279 (+481.25%)
Mutual labels:  serialization, serialization-library
Flatbuffers
FlatBuffers: Memory Efficient Serialization Library
Stars: ✭ 17,180 (+35691.67%)
Mutual labels:  serialization, serialization-library
Persistentstorageserializable
Swift library that makes easier to serialize the user's preferences (app's settings) with system User Defaults or Property List file on disk.
Stars: ✭ 162 (+237.5%)
Mutual labels:  serialization, serialization-library
ikeapack
Compact data serializer/packer written in Go, intended to produce a cross-language usable format.
Stars: ✭ 18 (-62.5%)
Mutual labels:  serialization, serialization-library

fuser

1-file header-only library for automatic (de)serialization of C++ types to/from JSON.

how it works

The library has a predefined set of (de)serializers for common types:

  • std::nullptr_t
  • bool
  • all standard integer (std::int*_t, std::uint*_t) and floating-point (float, double, long double) numeric types (checks if the value fits into the destination type)
  • char is not explicitly implemented, but it will very likely fall into one of fixed-width integer types
  • char8_t, char16_t and char32_t are not implemented
  • void* and void const* (converts to std::uintptr_t)
  • std::string
  • std::array<T, N> (checks if array size matches)
  • std::vector<T>
  • std::deque<T>
  • std::unique_ptr<T> (outputs null when there is no value)
  • std::optional<T> (outputs null when there is no value)
  • std::map<K, V, C>
  • std::unordered_map<K, V, H, E>

Additionally, it supports (de)serializers for structures which are valid boost fusion sequences.

Note that the above list is exactly how template specializations are implemented. The allocator template parameter is intentionally ommited because the current implementation has no guuarantees for stateful allocators. You can always define your (partial) specializations that reuse the existing serializers - just check how they are implemented.

If a type has no (de)serializer, the library will gracefully fail on a static_assert.

requirements

  • Boost >= 1.56 (only header-only libraries)
  • nlohmann/json >= 3.0
  • C++11
  • C++17 (optional, to enable std::optional support)
  • CMake >= 3.13

example

#include <fuser/fuser.hpp>

#include <vector>
#include <optional>
#include <iostream>

struct my_struct
{
	int i;
	double d;
	std::optional<bool> b;
};
// note: fusion macros must be used in global scope
BOOST_FUSION_ADAPT_STRUCT(my_struct, i, d, b)

int main()
{
	std::vector<my_struct> v = {
		{0, 3.14, {true}}, {1, 0.125, {false}}, {0, 0.0, {std::nullopt}}
	};

	std::cout << fuser::serialize(v).dump(4, ' ');
}

output:

[
    {
        "b": true,
        "d": 3.14,
        "i": 0
    },
    {
        "b": false,
        "d": 0.125,
        "i": 1
    },
    {
        "b": null,
        "d": 0.0,
        "i": 0
    }
]

library API

basic functions

namespace fuser {

template <typename T>
nlohmann::json serialize(T const& val);

template <typename T>
T deserialize(nlohmann::json const& json);

}

defining your own (de)serializer

template <>
struct fuser::serializer<my_type>
{
	static nlohmann::json serialize(my_type const& val)
	{
		/* ... */
	}
};

template <>
struct fuser::deserializer<my_type>
{
	static my_type deserialize(nlohmann::json const& json)
	{
		/* ... */
	}
};

Both class templates have a second unused template parameter to allow easy SFINAE for specializations.

trivia

The library is named "fuser" because it is a simple merge of boost fusion and serialization.

Special thanks to cycfi/json for the original idea.

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].