blob: 7c30b9c90c2ad9c343e7542a0f2840ba35217ed2 [file] [log] [blame]
Deepak Kodihalli6620e982017-08-05 13:09:54 -05001#pragma once
2
3#include <cereal/archives/json.hpp>
4#include <experimental/filesystem>
5#include <fstream>
6#include "config.h"
7
8namespace cereal
9{
10
11namespace fs = std::experimental::filesystem;
12
13using Path = std::string;
14using Interface = std::string;
15
16/** @brief Serialize inventory item
17 *
18 * @param[in] path - DBus object path
19 * @param[in] iface - Inventory interface name
20 * @param[in] object - Object to be serialized
21 */
22template <typename T>
23inline void serialize(const Path& path, const Interface& iface, const T& object)
24{
25 fs::path p(PIM_PERSIST_PATH);
26 p /= path;
27 fs::create_directories(p);
28 p /= iface;
29 std::ofstream os(p, std::ios::binary);
30 cereal::JSONOutputArchive oarchive(os);
31 oarchive(object);
32}
33
Deepak Kodihallib28990f2017-08-08 07:19:34 -050034/** @brief Deserialize inventory item
35 *
36 * @param[in] path - DBus object path
37 * @param[in] iface - Inventory interface name
38 * @param[in] object - Object to be serialized
39 */
40template <typename T>
41inline void deserialize(
42 const Path& path, const Interface& iface, T& object)
43{
44 fs::path p(PIM_PERSIST_PATH);
45 p /= path;
46 p /= iface;
47 if (fs::exists(p))
48 {
49 std::ifstream is(p, std::ios::in | std::ios::binary);
50 cereal::JSONInputArchive iarchive(is);
51 iarchive(object);
52 }
53}
54
Deepak Kodihalli6620e982017-08-05 13:09:54 -050055} // namespace cereal