diff options
author | Your Name <you@example.com> | 2021-04-15 15:23:23 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-04-15 15:23:23 -0400 |
commit | dfce4d0398a8bafbb7ad7a31345af181c0269c09 (patch) | |
tree | 695162ff8cc25e146f52d9e26fe19ffa9934b3d6 /src/jsonable.h | |
parent | 9034c3d2533177f7cb7a7ce939ec53f7fa63f60e (diff) | |
download | dmtool-dfce4d0398a8bafbb7ad7a31345af181c0269c09.tar.gz dmtool-dfce4d0398a8bafbb7ad7a31345af181c0269c09.tar.bz2 dmtool-dfce4d0398a8bafbb7ad7a31345af181c0269c09.zip |
Added spells
Diffstat (limited to 'src/jsonable.h')
-rw-r--r-- | src/jsonable.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/jsonable.h b/src/jsonable.h index 87f3f66..4be2bec 100644 --- a/src/jsonable.h +++ b/src/jsonable.h @@ -1,7 +1,32 @@ #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();} }; + +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; +} |