aboutsummaryrefslogtreecommitdiff
path: root/src/jsonable.h
blob: 01c720b605bd19d7fcdc167737a88f9606d13b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#pragma once
#include "json.hpp"
#include <memory>
#include <vector>

class Jsonable {
    public:
        virtual nlohmann::json toJson(void) const = 0;
        operator nlohmann::json() const {return toJson();}
        virtual ~Jsonable() {}
};

namespace nlohmann {
    template <typename T> struct adl_serializer<std::shared_ptr<T>> {
        static void to_json(json& j, const std::shared_ptr<T>& opt) {
            if(opt) {
                j = *opt;
            } else {
                j = nullptr;
            }
        }
        static void from_json(const json& j, std::shared_ptr<T>& opt) {
            opt = std::shared_ptr<T>(T::create(j));
        }
    };
}