aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThéophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>2022-04-20 16:37:44 +0200
committerGitHub <noreply@github.com>2022-04-20 16:37:44 +0200
commit2ffc5a45422522122c65cf71a2958e4b891e8134 (patch)
tree9fa4f96b7ed64c750388571a6803d51c11a7a9eb
parentcd0549a9cd91369f2ad858f0887c5e19077664a1 (diff)
parentebf2fd76b106d5eb8f45ccce0615653108bb99bc (diff)
Merge pull request #6425 from yorickvP/fix-6424
Add custom to_json and from_json functions for ExperimentalFeature
-rw-r--r--src/libutil/experimental-features.cc14
-rw-r--r--src/libutil/experimental-features.hh7
2 files changed, 21 insertions, 0 deletions
diff --git a/src/libutil/experimental-features.cc b/src/libutil/experimental-features.cc
index e033a4116..df37edf57 100644
--- a/src/libutil/experimental-features.cc
+++ b/src/libutil/experimental-features.cc
@@ -58,4 +58,18 @@ std::ostream & operator <<(std::ostream & str, const ExperimentalFeature & featu
return str << showExperimentalFeature(feature);
}
+void to_json(nlohmann::json& j, const ExperimentalFeature& feature) {
+ j = showExperimentalFeature(feature);
+}
+
+void from_json(const nlohmann::json& j, ExperimentalFeature& feature) {
+ const std::string input = j;
+ const auto parsed = parseExperimentalFeature(input);
+
+ if (parsed.has_value())
+ feature = *parsed;
+ else
+ throw Error("Unknown experimental feature '%s' in JSON input", input);
+}
+
}
diff --git a/src/libutil/experimental-features.hh b/src/libutil/experimental-features.hh
index 266e41a22..a6d080094 100644
--- a/src/libutil/experimental-features.hh
+++ b/src/libutil/experimental-features.hh
@@ -51,4 +51,11 @@ public:
MissingExperimentalFeature(ExperimentalFeature);
};
+/**
+ * Semi-magic conversion to and from json.
+ * See the nlohmann/json readme for more details.
+ */
+void to_json(nlohmann::json&, const ExperimentalFeature&);
+void from_json(const nlohmann::json&, ExperimentalFeature&);
+
}