An open API service indexing awesome lists of open source software.

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

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