summaryrefslogtreecommitdiff
path: root/src/formatter
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-05-16 16:36:48 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-05-16 16:36:48 +0300
commite8ceb3b5d429c0b58d60c68cc0d00582eb0fd25a (patch)
treecf55a84d2978c35fb0c28334114c898c04265346 /src/formatter
parenta9a55d0d2da510f6094e321af5e374895d2c0250 (diff)
add 'simple-json' format
Diffstat (limited to 'src/formatter')
-rw-r--r--src/formatter/formatter.h35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/formatter/formatter.h b/src/formatter/formatter.h
index 5dfa184..2ffa9b2 100644
--- a/src/formatter/formatter.h
+++ b/src/formatter/formatter.h
@@ -38,6 +38,7 @@ enum class Format {
Table,
SimpleTable,
JSON,
+ SimpleJSON,
};
std::ostream& operator<<(std::ostream& os, Unit val);
@@ -92,6 +93,7 @@ public:
virtual ~Formattable() = default;
virtual std::ostream& writeJSON(std::ostream& os) const = 0;
+ virtual std::ostream& writeSimpleJSON(std::ostream& os) const = 0;
virtual std::ostream& writeTable(std::ostream& os) const = 0;
virtual std::ostream& writeSimpleTable(std::ostream& os) const = 0;
@@ -105,6 +107,9 @@ public:
case Format::JSON:
return ref.writeJSON(os);
+
+ case Format::SimpleJSON:
+ return ref.writeSimpleJSON(os);
}
return os;
@@ -112,7 +117,7 @@ public:
};
-// T must have `operator<<` and `basic_json toJSON()` methods
+// T must have `operator<<`, `json toJSON()` and `json toSimpleJSON()` methods
template <typename T>
class Table : public Formattable {
protected:
@@ -184,6 +189,17 @@ public:
}
return os << j.dump();
}
+
+ std::ostream& writeSimpleJSON(std::ostream& os) const override {
+ ordered_json j = {
+ {"result", "ok"},
+ {"data", {}}
+ };
+ for (const auto &item: v_) {
+ j["data"][item.key] = item.value.toSimpleJSON();
+ }
+ return os << j.dump();
+ }
};
template <typename T>
@@ -220,6 +236,19 @@ public:
return os << j.dump();
}
+
+ std::ostream& writeSimpleJSON(std::ostream& os) const override {
+ json data = {};
+ ordered_json j;
+
+ j["result"] = "ok";
+
+ for (const auto &item: v_)
+ data.push_back(item.value.toSimpleJSON());
+ j["data"] = data;
+
+ return os << j.dump();
+ }
};
class Status : public Formattable {
@@ -250,6 +279,10 @@ public:
j["message"] = message_;
return os << j.dump();
}
+
+ std::ostream& writeSimpleJSON(std::ostream& os) const override {
+ return writeJSON(os);
+ }
};
}