Clean up Manager to use readJson

Change-Id: If4237a21aa0c5f414e20cd9e7eee2f1188097e14
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/redfish-core/lib/managers.hpp b/redfish-core/lib/managers.hpp
index 26abec0..2f50ca0 100644
--- a/redfish-core/lib/managers.hpp
+++ b/redfish-core/lib/managers.hpp
@@ -709,7 +709,7 @@
 
 inline bool
     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
-                        std::vector<nlohmann::json>& config,
+                        std::vector<nlohmann::json::object_t>& config,
                         std::vector<std::string>& zones)
 {
     if (config.empty())
@@ -721,8 +721,8 @@
     for (auto& odata : config)
     {
         std::string path;
-        if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
-                                          path))
+        if (!redfish::json_util::readJsonObject(odata, response->res,
+                                                "@odata.id", path))
         {
             return false;
         }
@@ -746,11 +746,11 @@
 
 inline const dbus::utility::ManagedObjectType::value_type*
     findChassis(const dbus::utility::ManagedObjectType& managedObj,
-                const std::string& value, std::string& chassis)
+                std::string_view value, std::string& chassis)
 {
     BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
 
-    std::string escaped = value;
+    std::string escaped(value);
     std::replace(escaped.begin(), escaped.end(), ' ', '_');
     escaped = "/" + escaped;
     auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
@@ -778,13 +778,13 @@
 
 inline CreatePIDRet createPidInterface(
     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
-    const nlohmann::json::iterator& it, const std::string& path,
+    std::string_view name, nlohmann::json& jsonValue, const std::string& path,
     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
     const std::string& profile)
 {
     // common deleter
-    if (it.value() == nullptr)
+    if (jsonValue == nullptr)
     {
         std::string iface;
         if (type == "PidControllers" || type == "FanControllers")
@@ -827,7 +827,7 @@
     {
         // if we aren't creating a new object, we should be able to find it on
         // d-bus
-        managedItem = findChassis(managedObj, it.key(), chassis);
+        managedItem = findChassis(managedObj, name, chassis);
         if (managedItem == nullptr)
         {
             BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
@@ -909,13 +909,13 @@
             output.emplace_back("Type", "Pid");
         }
 
-        std::optional<std::vector<nlohmann::json>> zones;
+        std::optional<std::vector<nlohmann::json::object_t>> zones;
         std::optional<std::vector<std::string>> inputs;
         std::optional<std::vector<std::string>> outputs;
         std::map<std::string, std::optional<double>> doubles;
         std::optional<std::string> setpointOffset;
         if (!redfish::json_util::readJson(
-                it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
+                jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
                 "Zones", zones, "FFGainCoefficient",
                 doubles["FFGainCoefficient"], "FFOffCoefficient",
                 doubles["FFOffCoefficient"], "ICoefficient",
@@ -990,7 +990,7 @@
             else
             {
                 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
-                messages::propertyValueNotInList(response->res, it.key(),
+                messages::propertyValueNotInList(response->res, name,
                                                  "SetPointOffset");
                 return CreatePIDRet::fail;
             }
@@ -1012,33 +1012,26 @@
     {
         output.emplace_back("Type", "Pid.Zone");
 
-        std::optional<nlohmann::json> chassisContainer;
+        std::optional<std::string> chassisId;
         std::optional<double> failSafePercent;
         std::optional<double> minThermalOutput;
-        if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
-                                          chassisContainer, "FailSafePercent",
-                                          failSafePercent, "MinThermalOutput",
-                                          minThermalOutput))
+        if (!redfish::json_util::readJson(jsonValue, response->res,
+                                          "Chassis/@odata.id", chassisId,
+                                          "FailSafePercent", failSafePercent,
+                                          "MinThermalOutput", minThermalOutput))
         {
             return CreatePIDRet::fail;
         }
 
-        if (chassisContainer)
+        if (chassisId)
         {
-            std::string chassisId;
-            if (!redfish::json_util::readJson(*chassisContainer, response->res,
-                                              "@odata.id", chassisId))
-            {
-                return CreatePIDRet::fail;
-            }
-
             // /redfish/v1/chassis/chassis_name/
-            if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
+            if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
             {
-                BMCWEB_LOG_ERROR("Got invalid path {}", chassisId);
+                BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
                 messages::invalidObject(
                     response->res,
-                    boost::urls::format("/redfish/v1/Chassis/{}", chassisId));
+                    boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
                 return CreatePIDRet::fail;
             }
         }
@@ -1055,14 +1048,14 @@
     {
         output.emplace_back("Type", "Stepwise");
 
-        std::optional<std::vector<nlohmann::json>> zones;
-        std::optional<std::vector<nlohmann::json>> steps;
+        std::optional<std::vector<nlohmann::json::object_t>> zones;
+        std::optional<std::vector<nlohmann::json::object_t>> steps;
         std::optional<std::vector<std::string>> inputs;
         std::optional<double> positiveHysteresis;
         std::optional<double> negativeHysteresis;
         std::optional<std::string> direction; // upper clipping curve vs lower
         if (!redfish::json_util::readJson(
-                it.value(), response->res, "Zones", zones, "Steps", steps,
+                jsonValue, response->res, "Zones", zones, "Steps", steps,
                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
                 "NegativeHysteresis", negativeHysteresis, "Direction",
                 direction))
@@ -1098,8 +1091,8 @@
                 double target = 0.0;
                 double out = 0.0;
 
-                if (!redfish::json_util::readJson(step, response->res, "Target",
-                                                  target, "Output", out))
+                if (!redfish::json_util::readJsonObject(
+                        step, response->res, "Target", target, "Output", out))
                 {
                     return CreatePIDRet::fail;
                 }
@@ -1324,28 +1317,16 @@
 
 struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
 {
-    SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
-                 nlohmann::json& data) :
-        asyncResp(asyncRespIn)
-    {
-        std::optional<nlohmann::json> pidControllers;
-        std::optional<nlohmann::json> fanControllers;
-        std::optional<nlohmann::json> fanZones;
-        std::optional<nlohmann::json> stepwiseControllers;
-
-        if (!redfish::json_util::readJson(
-                data, asyncResp->res, "PidControllers", pidControllers,
-                "FanControllers", fanControllers, "FanZones", fanZones,
-                "StepwiseControllers", stepwiseControllers, "Profile", profile))
-        {
-            return;
-        }
-        configuration.emplace_back("PidControllers", std::move(pidControllers));
-        configuration.emplace_back("FanControllers", std::move(fanControllers));
-        configuration.emplace_back("FanZones", std::move(fanZones));
-        configuration.emplace_back("StepwiseControllers",
-                                   std::move(stepwiseControllers));
-    }
+    SetPIDValues(
+        const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
+        std::vector<
+            std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
+            configurationsIn,
+        std::optional<std::string>& profileIn) :
+        asyncResp(asyncRespIn),
+        configuration(std::move(configurationsIn)),
+        profile(std::move(profileIn))
+    {}
 
     SetPIDValues(const SetPIDValues&) = delete;
     SetPIDValues(SetPIDValues&&) = delete;
@@ -1489,14 +1470,11 @@
             {
                 continue;
             }
-            BMCWEB_LOG_DEBUG("{}", *container);
 
             const std::string& type = containerPair.first;
 
-            for (nlohmann::json::iterator it = container->begin();
-                 it != container->end(); ++it)
+            for (auto& [name, value] : *container)
             {
-                const auto& name = it.key();
                 std::string dbusObjName = name;
                 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
                 BMCWEB_LOG_DEBUG("looking for {}", name);
@@ -1557,10 +1535,10 @@
                     }
                 }
 
-                if (createNewObject && it.value() == nullptr)
+                if (createNewObject && value == nullptr)
                 {
                     // can't delete a non-existent object
-                    messages::propertyValueNotInList(response->res, it.value(),
+                    messages::propertyValueNotInList(response->res, value,
                                                      name);
                     continue;
                 }
@@ -1586,8 +1564,8 @@
 
                 std::string chassis;
                 CreatePIDRet ret = createPidInterface(
-                    response, type, it, path, managedObj, createNewObject,
-                    output, chassis, currentProfile);
+                    response, type, name, value, path, managedObj,
+                    createNewObject, output, chassis, currentProfile);
                 if (ret == CreatePIDRet::fail)
                 {
                     return;
@@ -1678,7 +1656,7 @@
     }
 
     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
-    std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
+    std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
         configuration;
     std::optional<std::string> profile;
     dbus::utility::ManagedObjectType managedObj;
@@ -2219,68 +2197,69 @@
         {
             return;
         }
-        std::optional<nlohmann::json> oem;
-        std::optional<nlohmann::json> links;
+        std::optional<std::string> activeSoftwareImageOdataId;
         std::optional<std::string> datetime;
+        std::optional<nlohmann::json::object_t> pidControllers;
+        std::optional<nlohmann::json::object_t> fanControllers;
+        std::optional<nlohmann::json::object_t> fanZones;
+        std::optional<nlohmann::json::object_t> stepwiseControllers;
+        std::optional<std::string> profile;
 
-        if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
-                                      "DateTime", datetime, "Links", links))
+        // clang-format off
+        if (!json_util::readJsonPatch(req, asyncResp->res,
+              "DateTime", datetime,
+              "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
+              "Oem/OpenBmc/Fan/FanControllers", fanControllers,
+              "Oem/OpenBmc/Fan/FanZones", fanZones,
+              "Oem/OpenBmc/Fan/PidControllers", pidControllers,
+              "Oem/OpenBmc/Fan/Profile", profile,
+              "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
+        ))
         {
             return;
         }
+        // clang-format on
 
-        if (oem)
+        if (pidControllers || fanControllers || fanZones ||
+            stepwiseControllers || profile)
         {
 #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
-            std::optional<nlohmann::json> openbmc;
-            if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
-                                              openbmc))
+            std::vector<
+                std::pair<std::string, std::optional<nlohmann::json::object_t>>>
+                configuration;
+            if (pidControllers)
             {
-                return;
+                configuration.emplace_back("PidControllers",
+                                           std::move(pidControllers));
             }
-            if (openbmc)
+            if (fanControllers)
             {
-                std::optional<nlohmann::json> fan;
-                if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
-                                                  "Fan", fan))
-                {
-                    return;
-                }
-                if (fan)
-                {
-                    auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
-                    pid->run();
-                }
+                configuration.emplace_back("FanControllers",
+                                           std::move(fanControllers));
             }
+            if (fanZones)
+            {
+                configuration.emplace_back("FanZones", std::move(fanZones));
+            }
+            if (stepwiseControllers)
+            {
+                configuration.emplace_back("StepwiseControllers",
+                                           std::move(stepwiseControllers));
+            }
+            auto pid = std::make_shared<SetPIDValues>(
+                asyncResp, std::move(configuration), profile);
+            pid->run();
 #else
             messages::propertyUnknown(asyncResp->res, "Oem");
             return;
 #endif
         }
-        if (links)
-        {
-            std::optional<nlohmann::json> activeSoftwareImage;
-            if (!redfish::json_util::readJson(*links, asyncResp->res,
-                                              "ActiveSoftwareImage",
-                                              activeSoftwareImage))
-            {
-                return;
-            }
-            if (activeSoftwareImage)
-            {
-                std::optional<std::string> odataId;
-                if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
-                                         "@odata.id", odataId))
-                {
-                    return;
-                }
 
-                if (odataId)
-                {
-                    setActiveFirmwareImage(asyncResp, *odataId);
-                }
-            }
+        if (activeSoftwareImageOdataId)
+        {
+            setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
         }
+
         if (datetime)
         {
             setDateTime(asyncResp, *datetime);