| #include "managed_store_serialization.h" |
| |
| #include <cerrno> |
| #include <cstddef> |
| #include <cstdint> |
| #include <cstring> |
| #include <filesystem> |
| #include <fstream> |
| #include <ios> |
| #include <iterator> |
| #include <optional> |
| #include <string> |
| #include <tuple> |
| #include <utility> |
| #include <variant> |
| #include <vector> |
| |
| #include "nlohmann/json.hpp" |
| #include "dbus_utility.hpp" // NOLINT |
| #include "async_resp.hpp" // NOLINT |
| #include "http_request.hpp" // NOLINT |
| #include "sdbusplus/message/native_types.hpp" |
| |
| namespace serialize { |
| |
| using dbus::utility::DbusVariantType; |
| using dbus::utility::MapperGetObject; |
| using dbus::utility::MapperGetSubTreePathsResponse; |
| using dbus::utility::MapperGetSubTreeResponse; |
| using dbus::utility::MapperServiceMap; |
| using dbus::utility::SystemdListUnits; |
| using dbus::utility::SystemdUnitStruct; |
| |
| enum class Serialize : uint8_t { |
| string_type, |
| int64_t_type, |
| uint64_t_type, |
| double_type, |
| int32_t_type, |
| uint32_t_type, |
| uint16_t_type, |
| int16_t_type, |
| uint8_t_type, |
| bool_type, |
| size_t_type, |
| iter_type, |
| tup_type |
| }; |
| |
| static std::string SerializeToString(enum Serialize in) { |
| switch (in) { |
| case Serialize::string_type: |
| return "string"; |
| case Serialize::int64_t_type: |
| return "int64_t"; |
| case Serialize::uint64_t_type: |
| return "uint64_t"; |
| case Serialize::double_type: |
| return "double"; |
| case Serialize::int32_t_type: |
| return "int32_t"; |
| case Serialize::uint32_t_type: |
| return "uint32_t"; |
| case Serialize::uint16_t_type: |
| return "uint16_t"; |
| case Serialize::int16_t_type: |
| return "int16_t"; |
| case Serialize::uint8_t_type: |
| return "uint8_t"; |
| case Serialize::bool_type: |
| return "bool"; |
| case Serialize::size_t_type: |
| return "size_t"; |
| case Serialize::iter_type: |
| return "iter_type"; |
| case Serialize::tup_type: |
| return "tup"; |
| default: |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to serialize enum value "; |
| return "unknown"; |
| } |
| } |
| |
| template <typename T> |
| nlohmann::json iterate_serialize(const std::vector<T>& in) { |
| nlohmann::json arr = nlohmann::json::array(); |
| for (auto& itor : in) { |
| DbusVariantType temp = itor; |
| arr.push_back(Serialize(temp)); |
| } |
| return arr; |
| } |
| |
| nlohmann::json Serialize(const DbusVariantType& v) { |
| nlohmann::json j; |
| |
| if (const std::string* pval = std::get_if<std::string>(&v)) { |
| j["type"] = "string"; |
| j["val"] = *pval; |
| return j; |
| } |
| if (const int64_t* pval = std::get_if<int64_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::int64_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const uint64_t* pval = std::get_if<uint64_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::uint64_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const double* pval = std::get_if<double>(&v)) { |
| j["type"] = SerializeToString(Serialize::double_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const int32_t* pval = std::get_if<int32_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::int32_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const uint32_t* pval = std::get_if<uint32_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::uint32_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const uint16_t* pval = std::get_if<uint16_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::uint16_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const int16_t* pval = std::get_if<int16_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::int16_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const uint8_t* pval = std::get_if<uint8_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::uint8_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const bool* pval = std::get_if<bool>(&v)) { |
| j["type"] = SerializeToString(Serialize::bool_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const size_t* pval = std::get_if<size_t>(&v)) { |
| j["type"] = SerializeToString(Serialize::size_t_type); |
| j["val"] = *pval; |
| return j; |
| } |
| if (const std::vector<std::tuple<std::string, std::string, std::string>>* t = |
| std::get_if< |
| std::vector<std::tuple<std::string, std::string, std::string>>>( |
| &v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| j["iterType"] = "v_t_s_s_s"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| if (const std::vector<std::string>* t = |
| std::get_if<std::vector<std::string>>(&v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| j["iterType"] = "v_s"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| if (const std::vector<double>* t = std::get_if<std::vector<double>>(&v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| j["iterType"] = "v_d"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| if (const std::vector<uint32_t>* t = std::get_if<std::vector<uint32_t>>(&v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| j["iterType"] = "v_u32"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| if (const std::vector<uint16_t>* t = std::get_if<std::vector<uint16_t>>(&v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| j["iterType"] = "v_u16"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| if (const std::vector<std::tuple<std::string, std::string>>* t = |
| std::get_if<std::vector<std::tuple<std::string, std::string>>>(&v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| j["iterType"] = "v_t_s_s"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| if (const std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>* t = |
| std::get_if<std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>>( |
| &v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| ; |
| j["iterType"] = "v_t_u32_v_u32"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| if (const std::vector<std::tuple<uint32_t, size_t>>* t = |
| std::get_if<std::vector<std::tuple<uint32_t, size_t>>>(&v)) { |
| j["type"] = SerializeToString(Serialize::iter_type); |
| ; |
| j["iterType"] = "v_t_u32_size"; |
| j["val"] = iterate_serialize(*t); |
| return j; |
| } |
| |
| if (const std::tuple<std::string, std::string, std::string>* t = |
| std::get_if<std::tuple<std::string, std::string, std::string>>(&v)) { |
| j["type"] = SerializeToString(Serialize::tup_type); |
| j["iterType"] = "t_s_s_s"; |
| nlohmann::json arr = nlohmann::json::array(); |
| DbusVariantType tmp_var = std::get<0>(*t); |
| arr.push_back(Serialize(tmp_var)); |
| tmp_var = std::get<1>(*t); |
| arr.push_back(Serialize(tmp_var)); |
| tmp_var = std::get<2>(*t); |
| arr.push_back(Serialize(tmp_var)); |
| j["val"] = arr; |
| return j; |
| } |
| |
| if (const std::tuple<std::string, std::string>* t = |
| std::get_if<std::tuple<std::string, std::string>>(&v)) { |
| j["type"] = SerializeToString(Serialize::tup_type); |
| j["iterType"] = "t_s_s"; |
| nlohmann::json arr = nlohmann::json::array(); |
| DbusVariantType tmp_var = std::get<0>(*t); |
| arr.push_back(Serialize(tmp_var)); |
| tmp_var = std::get<1>(*t); |
| arr.push_back(Serialize(tmp_var)); |
| j["val"] = arr; |
| return j; |
| } |
| if (const std::tuple<uint32_t, std::vector<uint32_t>>* t = |
| std::get_if<std::tuple<uint32_t, std::vector<uint32_t>>>(&v)) { |
| j["type"] = SerializeToString(Serialize::tup_type); |
| ; |
| j["iterType"] = "t_u32_v_u32"; |
| nlohmann::json arr = nlohmann::json::array(); |
| DbusVariantType tmp_var = std::get<0>(*t); |
| arr.push_back(Serialize(tmp_var)); |
| tmp_var = std::get<1>(*t); |
| arr.push_back(Serialize(tmp_var)); |
| j["val"] = arr; |
| return j; |
| } |
| /* |
| * TODO deal with sdbusplus::message::object_path,and size_t |
| */ |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to serialize DbusVariantType, index " |
| << v.index(); |
| return j; |
| } |
| |
| inline DbusVariantType DeserializeIter(nlohmann::json::reference j, |
| const std::string& type) { |
| if (type == "v_t_s_s_s") { |
| std::vector<std::tuple<std::string, std::string, std::string>> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const std::tuple<std::string, std::string, std::string>* pval = |
| std::get_if<std::tuple<std::string, std::string, std::string>>( |
| &tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG |
| << "unable to deserialize vector tuple string string string"; |
| } |
| } |
| return vec; |
| } |
| |
| if (type == "v_s") { |
| std::vector<std::string> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const std::string* pval = std::get_if<std::string>(&tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to deserialize vector of string"; |
| } |
| } |
| return vec; |
| } |
| if (type == "v_d") { |
| std::vector<double> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const double* pval = std::get_if<double>(&tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to deserialize vector double"; |
| } |
| } |
| return vec; |
| } |
| if (type == "v_u32") { |
| std::vector<uint32_t> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const std::uint32_t* pval = std::get_if<uint32_t>(&tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to deserialize vector u32"; |
| } |
| } |
| return vec; |
| } |
| if (type == "v_u16") { |
| std::vector<uint16_t> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const std::uint16_t* pval = std::get_if<uint16_t>(&tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to deserialize vector u16"; |
| } |
| } |
| return vec; |
| } |
| if (type == "v_t_s_s") { |
| std::vector<std::tuple<std::string, std::string>> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const std::tuple<std::string, std::string>* pval = |
| std::get_if<std::tuple<std::string, std::string>>(&tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG |
| << "unable to deserialize vector tuple string string"; |
| } |
| } |
| return vec; |
| } |
| |
| if (type == "v_t_u32_v_u32") { |
| std::vector<std::tuple<uint32_t, std::vector<uint32_t>>> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const std::tuple<uint32_t, std::vector<uint32_t>>* pval = |
| std::get_if<std::tuple<uint32_t, std::vector<uint32_t>>>(&tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG |
| << "unable to deserialize vector tuple uint32 vector uint32"; |
| } |
| } |
| return vec; |
| } |
| |
| if (type == "v_t_u32_size") { |
| std::vector<std::tuple<uint32_t, size_t>> vec; |
| for (nlohmann::json::iterator itor = j.begin(); itor != j.end(); itor++) { |
| DbusVariantType tmp = Deserialize(*itor); |
| if (const std::tuple<uint32_t, size_t>* pval = |
| std::get_if<std::tuple<uint32_t, size_t>>(&tmp)) { |
| vec.push_back(*pval); |
| } else { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to deserialize vector uint32 size"; |
| } |
| } |
| return vec; |
| } |
| |
| BMCWEB_LOG_STATEFUL_DEBUG << "deserialize: unknown iter type, iterType: " |
| << type; |
| return {}; |
| } |
| |
| inline DbusVariantType DeserializeTup(nlohmann::json::reference j, |
| const std::string& type) { |
| if (type == "t_s_s") { |
| std::tuple<std::string, std::string> tup; |
| DbusVariantType tmp = Deserialize(j[0]); |
| if (const std::string* pval = std::get_if<std::string>(&tmp)) { |
| std::get<0>(tup) = *pval; |
| } else { |
| BMCWEB_LOG_ERROR << "Unable to derizlize tup t_s_s 0"; |
| } |
| tmp = Deserialize(j[1]); |
| if (const std::string* pval = std::get_if<std::string>(&tmp)) { |
| std::get<1>(tup) = *pval; |
| } else { |
| BMCWEB_LOG_ERROR << "Unable to derizlize tup t_s_s 1"; |
| } |
| return tup; |
| } |
| if (type == "t_s_s_s") { |
| std::tuple<std::string, std::string, std::string> tup; |
| DbusVariantType tmp = Deserialize(j[0]); |
| if (const std::string* pval = std::get_if<std::string>(&tmp)) { |
| std::get<0>(tup) = *pval; |
| } else { |
| BMCWEB_LOG_ERROR << "Unable to derizlize up t_s_s_s 0"; |
| } |
| tmp = Deserialize(j[1]); |
| if (const std::string* pval = std::get_if<std::string>(&tmp)) { |
| std::get<1>(tup) = *pval; |
| } else { |
| BMCWEB_LOG_ERROR << "Unable to derizlize up t_s_s_s 1"; |
| } |
| tmp = Deserialize(j[2]); |
| if (const std::string* pval = std::get_if<std::string>(&tmp)) { |
| std::get<2>(tup) = *pval; |
| } else { |
| BMCWEB_LOG_ERROR << "Unable to derizlize up t_s_s_s 2"; |
| } |
| return tup; |
| } |
| |
| if (type == "t_u32_v_u32") { |
| std::tuple<uint32_t, std::vector<uint32_t>> tup; |
| DbusVariantType tmp = Deserialize(j[0]); |
| if (const uint32_t* pval = std::get_if<uint32_t>(&tmp)) { |
| std::get<0>(tup) = *pval; |
| } else { |
| BMCWEB_LOG_ERROR << "Unable to derizlize tup t_u32_v_u32 0"; |
| } |
| tmp = Deserialize(j[1]); |
| if (const std::vector<uint32_t>* pval = |
| std::get_if<std::vector<uint32_t>>(&tmp)) { |
| std::get<1>(tup) = *pval; |
| } else { |
| BMCWEB_LOG_ERROR << "Unable to derizlize tup t_u32_v_u32 2"; |
| } |
| return tup; |
| } |
| |
| BMCWEB_LOG_STATEFUL_DEBUG << "deserialize: unknown tuple type, iterType: " |
| << type; |
| return {}; |
| } |
| |
| DbusVariantType Deserialize(nlohmann::json::reference j) { |
| try { |
| std::string vtype = j["type"]; |
| BMCWEB_LOG_STATEFUL_DEBUG << "deserialize: vtype: "; |
| BMCWEB_LOG_STATEFUL_DEBUG << vtype; |
| if (vtype == SerializeToString(Serialize::string_type)) { |
| std::string r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::int64_t_type)) { |
| int64_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::uint64_t_type)) { |
| uint64_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::double_type)) { |
| if (j["val"].is_null()) { |
| return {0.0}; |
| } |
| double r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::int32_t_type)) { |
| int32_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::uint32_t_type)) { |
| uint32_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::int16_t_type)) { |
| int16_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::uint16_t_type)) { |
| uint16_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::uint8_t_type)) { |
| uint8_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::bool_type)) { |
| bool r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::size_t_type)) { |
| size_t r = j["val"]; |
| return {r}; |
| } |
| if (vtype == SerializeToString(Serialize::iter_type)) { |
| nlohmann::json::reference ref = j["val"]; |
| std::string iter_type = j["iterType"]; |
| DbusVariantType r = DeserializeIter(ref, iter_type); |
| return r; |
| } |
| if (vtype == SerializeToString(Serialize::tup_type)) { |
| nlohmann::json::reference ref = j["val"]; |
| std::string iter_type = j["iterType"]; |
| DbusVariantType r = DeserializeTup(ref, iter_type); |
| return r; |
| } |
| BMCWEB_LOG_STATEFUL_DEBUG << "deserialize: unknown Type: " << vtype; |
| return {}; |
| } catch (...) { |
| BMCWEB_LOG_STATEFUL_DEBUG << "deserialize: catch "; |
| return {}; |
| } |
| } |
| |
| using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>; |
| using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>; |
| using ManagedObjectType = |
| std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>; |
| |
| nlohmann::json SerializePropMap(DBusPropertiesMap& in) { |
| nlohmann::json j; |
| j["type"] = "PropMap"; |
| nlohmann::json arr = nlohmann::json::array(); |
| for (DBusPropertiesMap::iterator itor = in.begin(); itor != in.end(); |
| itor++) { |
| nlohmann::json tmp; |
| tmp["string"] = itor->first; |
| DbusVariantType var = itor->second; |
| tmp["val"] = Serialize(var); |
| arr.push_back(tmp); |
| } |
| j["val"] = arr; |
| return j; |
| } |
| |
| DBusPropertiesMap DeserializePropMap(nlohmann::json::reference j) { |
| BMCWEB_LOG_STATEFUL_DEBUG << "deserialize propMap" << j.dump(); |
| DBusPropertiesMap ret; |
| nlohmann::json::reference val = j["val"]; |
| for (nlohmann::json::iterator itor = val.begin(); itor != val.end(); itor++) { |
| nlohmann::json::reference innerval = (*itor)["val"]; |
| DbusVariantType tmp_variant = Deserialize(innerval); |
| std::pair<std::string, DbusVariantType> tmp = {(*itor)["string"], |
| tmp_variant}; |
| ret.push_back(tmp); |
| } |
| return ret; |
| } |
| |
| nlohmann::json SerializeInterfaceMap(DBusInteracesMap& in) { |
| nlohmann::json j; |
| j["type"] = "interaceMap"; |
| nlohmann::json arr = nlohmann::json::array(); |
| for (DBusInteracesMap::iterator itor = in.begin(); itor != in.end(); itor++) { |
| nlohmann::json tmp; |
| tmp["string"] = itor->first; |
| tmp["val"] = SerializePropMap(itor->second); |
| arr.push_back(tmp); |
| } |
| j["val"] = arr; |
| return j; |
| } |
| |
| DBusInteracesMap DeserializeInterfaceMap(nlohmann::json::reference& j) { |
| DBusInteracesMap ret; |
| nlohmann::json::reference val = j["val"]; |
| for (nlohmann::json::iterator itor = val.begin(); itor != val.end(); itor++) { |
| nlohmann::json::reference innerval = (*itor)["val"]; |
| std::pair<std::string, DBusPropertiesMap> tmp = { |
| (*itor)["string"], DeserializePropMap(innerval)}; |
| ret.push_back(tmp); |
| } |
| return ret; |
| } |
| |
| nlohmann::json SerializeObject(ManagedObjectType& in) { |
| nlohmann::json j; |
| j["type"] = "object"; |
| nlohmann::json arr = nlohmann::json::array(); |
| for (ManagedObjectType::iterator itor = in.begin(); itor != in.end(); |
| itor++) { |
| nlohmann::json tmp; |
| tmp["string"] = itor->first; |
| tmp["val"] = SerializeInterfaceMap(itor->second); |
| arr.push_back(tmp); |
| } |
| j["val"] = arr; |
| return j; |
| } |
| |
| ManagedObjectType DeserializeObject(nlohmann::json::reference j) { |
| ManagedObjectType ret; |
| nlohmann::json::reference val = j["val"]; |
| for (nlohmann::json::iterator itor = val.begin(); itor != val.end(); itor++) { |
| nlohmann::json::reference innerval = (*itor)["val"]; |
| std::pair<std::string, DBusInteracesMap> tmp = { |
| (*itor)["string"], DeserializeInterfaceMap(innerval)}; |
| ret.push_back(tmp); |
| } |
| return ret; |
| } |
| |
| nlohmann::json SerializeMapperServiceMap( |
| const dbus::utility::MapperServiceMap& in) { |
| nlohmann::json j; |
| j["type"] = "mapperServiceMap"; |
| nlohmann::json arr = nlohmann::json::array(); |
| for (MapperServiceMap::const_iterator itor = in.begin(); itor != in.end(); |
| itor++) { |
| nlohmann::json elem; |
| elem["string"] = itor->first; |
| nlohmann::json inner_arr = nlohmann::json::array(); |
| for (std::vector<std::string>::const_iterator inner_itor = |
| itor->second.begin(); |
| inner_itor != itor->second.end(); inner_itor++) { |
| nlohmann::json inner_elem; |
| inner_elem["type"] = "string"; |
| inner_elem["val"] = *inner_itor; |
| inner_arr.push_back(inner_elem); |
| } |
| elem["val"] = inner_arr; |
| arr.push_back(elem); |
| } |
| j["val"] = arr; |
| return j; |
| } |
| |
| MapperServiceMap DeserializeMapperServiceMap(nlohmann::json::reference j) { |
| MapperServiceMap ret; |
| nlohmann::json::reference val = j["val"]; |
| for (nlohmann::json::iterator itor = val.begin(); itor != val.end(); itor++) { |
| std::vector<std::string> inner_tmp = {}; |
| |
| for (nlohmann::json::iterator inner_itor = (*itor)["val"].begin(); |
| inner_itor != (*itor)["val"].end(); inner_itor++) { |
| inner_tmp.push_back((*inner_itor)["val"]); |
| } |
| const std::pair<std::string, std::vector<std::string>> tmp = { |
| (*itor)["string"], inner_tmp}; |
| ret.push_back(tmp); |
| } |
| return ret; |
| } |
| |
| nlohmann::json SerializeSubtree(dbus::utility::MapperGetSubTreeResponse& in) { |
| nlohmann::json j; |
| j["type"] = "subTreeResponse"; |
| nlohmann::json arr = nlohmann::json::array(); |
| for (MapperGetSubTreeResponse::iterator itor = in.begin(); itor != in.end(); |
| itor++) { |
| nlohmann::json tmp; |
| tmp["string"] = itor->first; |
| tmp["val"] = SerializeMapperServiceMap(itor->second); |
| arr.push_back(tmp); |
| } |
| j["val"] = arr; |
| return j; |
| } |
| |
| MapperGetSubTreeResponse DeserializeSubtree( |
| nlohmann::json::reference j) { |
| MapperGetSubTreeResponse ret; |
| nlohmann::json::reference val = j["val"]; |
| for (nlohmann::json::iterator itor = val.begin(); itor != val.end(); itor++) { |
| nlohmann::json::reference innerval = (*itor)["val"]; |
| std::pair<std::string, MapperServiceMap> tmp = { |
| (*itor)["string"], DeserializeMapperServiceMap(innerval)}; |
| ret.push_back(tmp); |
| } |
| return ret; |
| } |
| |
| nlohmann::json SerializeSubtreePaths(MapperGetSubTreePathsResponse& in) { |
| nlohmann::json j; |
| j["type"] = "subTreePathsResponse"; |
| nlohmann::json arr = nlohmann::json::array(); |
| for (MapperGetSubTreePathsResponse::iterator itor = in.begin(); |
| itor != in.end(); itor++) { |
| arr.push_back(*itor); |
| } |
| j["val"] = arr; |
| return j; |
| } |
| |
| MapperGetSubTreePathsResponse DeserializeSubtreePaths( |
| nlohmann::json::reference j) { |
| MapperGetSubTreePathsResponse ret; |
| nlohmann::json::reference val = j["val"]; |
| for (nlohmann::json::iterator itor = val.begin(); itor != val.end(); itor++) { |
| ret.push_back(*itor); |
| } |
| return ret; |
| } |
| |
| // MapperObject is equivalent to MapperServiceMap |
| nlohmann::json SerializeMapperObject(MapperGetObject& in) { |
| return SerializeMapperServiceMap(in); |
| } |
| |
| MapperGetObject DeserializeMapperObject(nlohmann::json::reference j) { |
| return DeserializeMapperServiceMap(j); |
| } |
| |
| nlohmann::json SerializeSystemdUnitStruct(SystemdUnitStruct& in) { |
| nlohmann::json j; |
| j["type"] = "systemdUnitStruct"; |
| nlohmann::json::reference val = j["val"]; |
| |
| // Serialize all parts of the systemd unit struct a(ssssssouso) |
| val["name"] = std::get<0>(in); |
| val["description"] = std::get<1>(in); |
| val["load_state"] = std::get<2>(in); |
| val["active_state"] = std::get<3>(in); |
| val["substate"] = std::get<4>(in); |
| val["followed_unit"] = std::get<5>(in); |
| val["unit_path"] = std::get<6>(in).str; |
| val["job_id"] = std::get<7>(in); |
| val["job_type"] = std::get<8>(in); |
| val["job_path"] = std::get<9>(in).str; |
| |
| return j; |
| } |
| |
| nlohmann::json SerializeSystemdListUnits(SystemdListUnits& in) { |
| nlohmann::json j; |
| j["type"] = "systemdListUnits"; |
| nlohmann::json arr = nlohmann::json::array(); |
| for (SystemdListUnits::iterator itor = in.begin(); itor != in.end(); itor++) { |
| arr.push_back(SerializeSystemdUnitStruct(*itor)); |
| } |
| j["val"] = arr; |
| return j; |
| } |
| |
| SystemdUnitStruct DeserializeSystemdUnitStruct( |
| nlohmann::json::reference j) { |
| SystemdUnitStruct ret; |
| nlohmann::json::reference val = j["val"]; |
| |
| return std::make_tuple( |
| val["name"], val["description"], val["load_state"], val["active_state"], |
| val["substate"], val["followed_unit"], |
| sdbusplus::message::object_path(val["unit_path"]), val["job_id"], |
| val["job_type"], sdbusplus::message::object_path(val["job_path"])); |
| } |
| |
| SystemdListUnits DeserializeSystemdListUnits( |
| nlohmann::json::reference j) { |
| SystemdListUnits ret; |
| nlohmann::json::reference val = j["val"]; |
| for (nlohmann::json::iterator itor = val.begin(); itor != val.end(); itor++) { |
| ret.push_back(DeserializeSystemdUnitStruct(*itor)); |
| } |
| return ret; |
| } |
| |
| std::optional<std::string> ReadBinaryFileToString( |
| const std::filesystem::path& file) { |
| std::ifstream f(file, std::ios_base::in | std::ios::binary); |
| if (!f.is_open()) { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to open file " << file; |
| return std::nullopt; |
| } |
| std::string content{std::istreambuf_iterator<char>(f), |
| std::istreambuf_iterator<char>()}; |
| if (!f) { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to read file " << file |
| << strerror(errno); |
| return std::nullopt; |
| } |
| f.close(); |
| if (!f) { |
| BMCWEB_LOG_STATEFUL_DEBUG << "unable to close file " << file |
| << strerror(errno); |
| return std::nullopt; |
| } |
| return content; |
| } |
| |
| } // namespace serialize |