inventory: implement serialization
Use Cereal to serialize and persist inventory items to the filesystem.
Serialize inventory objects as and when they're created/updated via the
notify() API.
Create a template API to perform serialization on the sdbusplus
inventory interface type.
An inventory item path /foo/bar/baz implementing interfaces iface1 and
iface2 would be stored in paths /foo/bar/baz/iface1 and
/foo/bar/baz/iface2.
Change-Id: I9a175185eac1740d6f2ca86a3ee13457edfc8ea9
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/serialize.hpp b/serialize.hpp
new file mode 100644
index 0000000..281f61c
--- /dev/null
+++ b/serialize.hpp
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <cereal/archives/json.hpp>
+#include <experimental/filesystem>
+#include <fstream>
+#include "config.h"
+
+namespace cereal
+{
+
+namespace fs = std::experimental::filesystem;
+
+using Path = std::string;
+using Interface = std::string;
+
+/** @brief Serialize inventory item
+ *
+ * @param[in] path - DBus object path
+ * @param[in] iface - Inventory interface name
+ * @param[in] object - Object to be serialized
+ */
+template <typename T>
+inline void serialize(const Path& path, const Interface& iface, const T& object)
+{
+ fs::path p(PIM_PERSIST_PATH);
+ p /= path;
+ fs::create_directories(p);
+ p /= iface;
+ std::ofstream os(p, std::ios::binary);
+ cereal::JSONOutputArchive oarchive(os);
+ oarchive(object);
+}
+
+} // namespace cereal