cronch

Quick start

Making your type usable with cronch

In order to use your type, cronch needs some metadata. The easiest way to provide this is with CRONCH_META_TYPE macro. It is also possible to add your type via templates (which is what the macro generates). See metadata for more information.

Serialization

Serialization is done via the serialize() function.

template<concepts::serialization_backend Backend, typename T>
std::string serialize(const T &obj)

Same as the overload taking document_type, but default constructs the document_type.

Additional Requirments

  • (const Backend::document_type& doc) { Backend::to_string(doc) } -> std::string

template<concepts::serialization_backend Backend, typename T>
void serialize(const T &obj, Backend::document_type &doc)

Reads a type into the document_type of the specified backend

Deserialzation

Deserialization is done via the deserialize() function.

template<typename T, concepts::deserizalation_backend Backend>
T deserialize(const Backend &from)

Parse a type from the data stored by the backend from. Default constructs T

template<typename T, concepts::deserizalation_backend Backend>
void deserialize(const Backend &from, T &to)

Same as the overload that returns T, except it parses it into to instead.

An example

#include <iostream>

#include <cronch/json/nloh.hpp>
#include <cronch/meta.hpp>
#include <cronch/serialize.hpp>

#include <string>

namespace cr = cronch;

struct my_type {
    std::string name;
    int some_info;
};

template<>
struct cr::metadata<my_type> {
    // clang-format off
    constexpr static auto name = "my_type";
    constexpr static auto members = cr::meta::mems(
        cr::meta::field("name", &my_type::name),
        cr::meta::field("some_info", &my_type::some_info)
    );
    // clang-format on
};
static_assert(!cr::json::concepts::json_serializable<std::vector<my_type>>);

int main()
{
    const auto my_obj = my_type{"Hello there", 4};
    std::cout << "my_type: " << cr::serialize<cr::json::nloh>(my_obj) << '\n';
    std::vector<my_type> types{{"Hello", 2}, {"Sup", 3}};
    std::cout << "my_types: " << cr::serialize<cr::json::nloh>(types) << '\n';

}

Registering types

The registration of types is done via metadata. See the associated page for detailed information.

Naming your type

To define a name for your type, add a field: constexpr static const char* name = "my name" to your specialisation of metadata.

Registering fields

Fields are added via meta::field. For each field you want cronch to be aware of, add a cronch::meta::field("name", &my_type::field_name) to the metadata::members field in your specialisation of metadata.

Registering properties

Properties refer to members of your type which are accessed through get/set pairs. cronch supports these via the meta::property type. Add it to the members field of your metadata specialisation.

Properties example


#include <cronch/serialize.hpp>
#include <cronch/meta.hpp>
#include <cronch/json/nloh.hpp>

#include <iostream>

class my_type {
public:

    void value(const std::string& v) {
        value_ = v;
    } 

    auto value() const -> const std::string& {
        return value_;
    }
    auto i() const -> int {
        return i_;
    }
    void set_i(int v) {
        i_ = v;
    }
private:
    std::string value_;
    int i_;
};

namespace cm = cronch::meta;


void set_v(my_type& t, const std::string& s) {
    t.value(s);
}
auto get_v(const my_type& t) -> const std::string&  {
    return t.value();
}

CRONCH_META_TYPE(my_type, (
            cm::property("value", &get_v, &set_v),
            cm::property("i", &my_type::i, &my_type::set_i)

            ))

int main() {

    my_type t;
    t.set_i(5);
    t.value("Hello");

    std::cout << cronch::serialize<cronch::json::nloh>(t) << '\n';

}