https://github.com/thomastrapp/recursive-variant
A serializable recursive variant for JSON-like data
https://github.com/thomastrapp/recursive-variant
cereal cpp json recursive serialization variant
Last synced: 6 months ago
JSON representation
A serializable recursive variant for JSON-like data
- Host: GitHub
- URL: https://github.com/thomastrapp/recursive-variant
- Owner: thomastrapp
- License: mit
- Created: 2017-08-24T21:35:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-25T11:13:17.000Z (over 8 years ago)
- Last Synced: 2025-03-30T10:45:41.540Z (12 months ago)
- Topics: cereal, cpp, json, recursive, serialization, variant
- Language: C++
- Homepage:
- Size: 6.84 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Serializable Boost Recursive Variant
A Boost.Variant for JSON-like data with serialization support
for [cereal](http://uscilab.github.io/cereal/).
This project is released under the terms of the MIT license.
## Dependencies
* Boost.Variant, Boost.MPL
* [cereal](http://uscilab.github.io/cereal/)
* Testing: cmake, [catch](https://github.com/philsquared/Catch)
## Types
| Alias | Type |
|-----------------|-------------------------------------------------|
| `Value::Value` | `Int\|String\|Vector\|Map` |
| `Value::Int` | `int32_t` |
| `Value::String` | `std::string` |
| `Value::Vector` | `std::vector` |
| `Value::Map` | `std::unordered_map` |
## The gist
```cpp
using Int = std::int32_t;
using String = std::string;
using Value = boost::make_recursive_variant<
Int
, String
, std::vector
, std::unordered_map
>::type;
using Vector = boost::mpl::at>::type;
using Map = boost::mpl::at>::type;
```
Defined in [Value.h](include/Value/Value.h).
## Helpers
```cpp
template
inline bool Is(const Value& value) { return value.which() == TypeIndex; }
template
inline Type& Get(Value& value) { return boost::get(value); }
template
inline const Type& Get(const Value& value) { return boost::get(value); }
```
Defined in [Helper.h](include/Value/Helper.h).
## Serialization
Powered by [cereal](http://uscilab.github.io/cereal/).
```cpp
Value::Map val({{"beep","boop"}, {"whoop", Value::Vector({1,2,3,"whoop"})}});
std::string payload = Value::Pack(val);
Value::Value unpacked = Value::Unpack(payload);
assert(Value::Is(unpacked));
```
Defined in [Serialization.h](include/Value/Serialization.h).
## Switching by type
```cpp
namespace V = Value;
switch( value.which() )
{
case V::TypeIndex: /* ... */ break;
case V::TypeIndex: /* ... */ break;
case V::TypeIndex: /* ... */ break;
case V::TypeIndex: /* ... */ break;
default: break;
}
```
Defined in [TypeIndex.h](include/Value/TypeIndex.h).
## Printing
```cpp
Value::Print(Value::Map({{"beep","boop"}, {"whoop", Value::Vector({1,2,3,"whoop"})}}), std::cout);
// {"whoop":[1, 2, 3, "whoop"], "beep":"boop"}
```
Defined in [Print.h](include/Value/Print.h).