aboutsummaryrefslogtreecommitdiff
path: root/src/jsonable.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/jsonable.h')
-rw-r--r--src/jsonable.h25
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;
+}