aboutsummaryrefslogtreecommitdiff
path: root/src/jsonable.h
blob: d6866b804c6de897834477f5910bb99d9b719c00 (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
27
28
29
30
31
32
33
#pragma once
#include "json.hpp"
#include "utils.h"
#include <memory>
#include <vector>

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

template<typename T> std::vector<T> json2vec(const nlohmann::json& data) {
    using std::begin; using std::end;
    return std::vector<T>(begin(data), end(data));
}

template<typename T> std::vector<T> jsonList2vec(const std::string& type, const std::vector<std::string>& names) {
    std::vector<T> ret;
    for(auto name : names) {
        ret.push_back(utils::loadJson(type, name));
    }
    return ret;
}

template<typename T> std::vector<std::shared_ptr<T>> json2ptrvec(const nlohmann::json& data) {
    std::vector<std::shared_ptr<T>> ret;
    for(nlohmann::json d : data) {
        ret.push_back(T::create(d));
    }
    return ret;
}