Remove adl_serializer uses for json

Several pieces of code seems to be using the adl_serializer from
nlohmann.  This unfortunately has very undesirable behavior in some
cases, and makes a lot of things really difficult to track back to the
function that did the serialization, which has caused several bugs in
the past with incorrect types.

This patchset removes them, and opts for the inline version of the
nlohmann json serialization.

Tested:
Booted bmcweb, and logged in.
cat bmcweb_persistent_data.json showed persistent data written properly.

Logged into bmc through webui-vue
systemctl restart bmcweb
Then refreshed webui-vue, and didn't get logged out.

Change-Id: I92868629c54d08b37dd1d956f7c2e2a954f9b670
diff --git a/include/dbus_monitor.hpp b/include/dbus_monitor.hpp
index e177388..f770ec3 100644
--- a/include/dbus_monitor.hpp
+++ b/include/dbus_monitor.hpp
@@ -12,18 +12,6 @@
 
 #include <variant>
 
-namespace nlohmann
-{
-template <typename... Args>
-struct adl_serializer<std::variant<Args...>>
-{
-    static void to_json(json& j, const std::variant<Args...>& v)
-    {
-        std::visit([&](auto&& val) { j = val; }, v);
-    }
-};
-} // namespace nlohmann
-
 namespace crow
 {
 namespace dbus_monitor
diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp
index 03457c7..9c86f65 100644
--- a/include/persistent_data.hpp
+++ b/include/persistent_data.hpp
@@ -168,13 +168,38 @@
             std::filesystem::perms::owner_write |
             std::filesystem::perms::group_read;
         std::filesystem::permissions(filename, permission);
+        const auto& c = SessionStore::getInstance().getAuthMethodsConfig();
+        nlohmann::json data{{"auth_config",
+                             {{"XToken", c.xtoken},
+                              {"Cookie", c.cookie},
+                              {"SessionToken", c.sessionToken},
+                              {"BasicAuth", c.basic},
+                              {"TLS", c.tls}}
 
-        nlohmann::json data{
-            {"sessions", SessionStore::getInstance().authTokens},
-            {"auth_config", SessionStore::getInstance().getAuthMethodsConfig()},
-            {"system_uuid", systemUuid},
-            {"revision", jsonRevision},
-            {"timeout", SessionStore::getInstance().getTimeoutInSeconds()}};
+                            },
+                            {"system_uuid", systemUuid},
+                            {"revision", jsonRevision}};
+
+        nlohmann::json& sessions = data["sessions"];
+        sessions = nlohmann::json::array();
+        for (const auto& p : SessionStore::getInstance().authTokens)
+        {
+            if (p.second->persistence !=
+                persistent_data::PersistenceType::SINGLE_REQUEST)
+            {
+                sessions.push_back({
+                    {"unique_id", p.second->uniqueId},
+                    {"session_token", p.second->sessionToken},
+                    {"username", p.second->username},
+                    {"csrf_token", p.second->csrfToken},
+                    {"timeout",
+                     SessionStore::getInstance().getTimeoutInSeconds()},
+#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
+                    {"client_id", p.second->clientId},
+#endif
+                });
+            }
+        }
         persistentFile << data;
     }
 
diff --git a/include/sessions.hpp b/include/sessions.hpp
index 23de570..418f6f8 100644
--- a/include/sessions.hpp
+++ b/include/sessions.hpp
@@ -417,45 +417,3 @@
 };
 
 } // namespace persistent_data
-
-// to_json(...) definition for objects of UserSession type
-namespace nlohmann
-{
-template <>
-struct adl_serializer<std::shared_ptr<persistent_data::UserSession>>
-{
-    static void to_json(nlohmann::json& j,
-                        const std::shared_ptr<persistent_data::UserSession>& p)
-    {
-        if (p->persistence != persistent_data::PersistenceType::SINGLE_REQUEST)
-        {
-#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
-            j = nlohmann::json{
-                {"unique_id", p->uniqueId}, {"session_token", p->sessionToken},
-                {"username", p->username},  {"csrf_token", p->csrfToken},
-                {"client_id", p->clientId}, { "client_ip", p->clientIp }};
-#else
-            j = nlohmann::json{{"unique_id", p->uniqueId},
-                               {"session_token", p->sessionToken},
-                               {"username", p->username},
-                               {"csrf_token", p->csrfToken},
-                               {"client_ip", p->clientIp}};
-#endif
-        }
-    }
-};
-
-template <>
-struct adl_serializer<persistent_data::AuthConfigMethods>
-{
-    static void to_json(nlohmann::json& j,
-                        const persistent_data::AuthConfigMethods& c)
-    {
-        j = nlohmann::json{{"XToken", c.xtoken},
-                           {"Cookie", c.cookie},
-                           {"SessionToken", c.sessionToken},
-                           {"BasicAuth", c.basic},
-                           {"TLS", c.tls}};
-    }
-};
-} // namespace nlohmann
diff --git a/redfish-core/lib/cpudimm.hpp b/redfish-core/lib/cpudimm.hpp
index 6e658df..31db746 100644
--- a/redfish-core/lib/cpudimm.hpp
+++ b/redfish-core/lib/cpudimm.hpp
@@ -110,10 +110,6 @@
                 }
                 aResp->res.jsonValue["TotalCores"] = *coresCount;
             }
-            else if (property.first == "MaxSpeedInMhz")
-            {
-                aResp->res.jsonValue["MaxSpeedMHz"] = property.second;
-            }
             else if (property.first == "Socket")
             {
                 const std::string* value =
@@ -125,7 +121,11 @@
             }
             else if (property.first == "ThreadCount")
             {
-                aResp->res.jsonValue["TotalThreads"] = property.second;
+                const int64_t* value = std::get_if<int64_t>(&property.second);
+                if (value != nullptr)
+                {
+                    aResp->res.jsonValue["TotalThreads"] = *value;
+                }
             }
             else if (property.first == "Family")
             {
@@ -642,24 +642,53 @@
         }
         else if (property.first == "VolatileRegionNumberLimit")
         {
-            aResp->res.jsonValue["VolatileRegionNumberLimit"] = property.second;
+            const uint64_t* value = std::get_if<uint64_t>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
+            aResp->res.jsonValue["VolatileRegionNumberLimit"] = *value;
         }
         else if (property.first == "PmRegionNumberLimit")
         {
-            aResp->res.jsonValue["PersistentRegionNumberLimit"] =
-                property.second;
+            const uint64_t* value = std::get_if<uint64_t>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
+            aResp->res.jsonValue["PersistentRegionNumberLimit"] = *value;
         }
         else if (property.first == "SpareDeviceCount")
         {
-            aResp->res.jsonValue["SpareDeviceCount"] = property.second;
+            const uint64_t* value = std::get_if<uint64_t>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
+            aResp->res.jsonValue["SpareDeviceCount"] = *value;
         }
         else if (property.first == "IsSpareDeviceInUse")
         {
-            aResp->res.jsonValue["IsSpareDeviceEnabled"] = property.second;
+            const bool* value = std::get_if<bool>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
+            aResp->res.jsonValue["IsSpareDeviceEnabled"] = *value;
         }
         else if (property.first == "IsRankSpareEnabled")
         {
-            aResp->res.jsonValue["IsRankSpareEnabled"] = property.second;
+            const bool* value = std::get_if<bool>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
+            aResp->res.jsonValue["IsRankSpareEnabled"] = *value;
         }
         else if (property.first == "MaxAveragePowerLimitmW")
         {
@@ -674,13 +703,15 @@
             }
             aResp->res.jsonValue["MaxTDPMilliWatts"] = *value;
         }
-        else if (property.first == "CurrentSecurityState")
-        {
-            aResp->res.jsonValue["SecurityState"] = property.second;
-        }
         else if (property.first == "ConfigurationLocked")
         {
-            aResp->res.jsonValue["ConfigurationLocked"] = property.second;
+            const bool* value = std::get_if<bool>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
+            aResp->res.jsonValue["ConfigurationLocked"] = *value;
         }
         else if (property.first == "AllowedMemoryModes")
         {
@@ -726,26 +757,31 @@
                 }
             }
         }
-        // PersistantMemory.PowerManagmentPolicy interface
-        else if (property.first == "AveragePowerBudgetmW" ||
-                 property.first == "MaxTDPmW" ||
-                 property.first == "PeakPowerBudgetmW" ||
-                 property.first == "PolicyEnabled")
-        {
-            std::string name =
-                boost::replace_all_copy(property.first, "mW", "MilliWatts");
-            aResp->res.jsonValue["PowerManagementPolicy"][name] =
-                property.second;
-        }
         // PersistantMemory.SecurityCapabilites interface
         else if (property.first == "ConfigurationLockCapable" ||
                  property.first == "DataLockCapable" ||
-                 property.first == "MaxPassphraseCount" ||
-                 property.first == "PassphraseCapable" ||
+                 property.first == "PassphraseCapable")
+        {
+            const bool* value = std::get_if<bool>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
+            aResp->res.jsonValue["SecurityCapabilities"][property.first] =
+                *value;
+        }
+        else if (property.first == "MaxPassphraseCount" ||
                  property.first == "PassphraseLockLimit")
         {
+            const uint64_t* value = std::get_if<uint64_t>(&property.second);
+            if (value == nullptr)
+            {
+                messages::internalError(aResp->res);
+                continue;
+            }
             aResp->res.jsonValue["SecurityCapabilities"][property.first] =
-                property.second;
+                *value;
         }
     }
 }
@@ -802,19 +838,43 @@
             {
                 if (property.first == "MemoryDataWidth")
                 {
-                    aResp->res.jsonValue["DataWidthBits"] = property.second;
+                    const uint16_t* value =
+                        std::get_if<uint16_t>(&property.second);
+                    if (value == nullptr)
+                    {
+                        continue;
+                    }
+                    aResp->res.jsonValue["DataWidthBits"] = *value;
                 }
                 else if (property.first == "PartNumber")
                 {
-                    aResp->res.jsonValue["PartNumber"] = property.second;
+                    const std::string* value =
+                        std::get_if<std::string>(&property.second);
+                    if (value == nullptr)
+                    {
+                        continue;
+                    }
+                    aResp->res.jsonValue["PartNumber"] = *value;
                 }
                 else if (property.first == "SerialNumber")
                 {
-                    aResp->res.jsonValue["SerialNumber"] = property.second;
+                    const std::string* value =
+                        std::get_if<std::string>(&property.second);
+                    if (value == nullptr)
+                    {
+                        continue;
+                    }
+                    aResp->res.jsonValue["SerialNumber"] = *value;
                 }
                 else if (property.first == "Manufacturer")
                 {
-                    aResp->res.jsonValue["Manufacturer"] = property.second;
+                    const std::string* value =
+                        std::get_if<std::string>(&property.second);
+                    if (value == nullptr)
+                    {
+                        continue;
+                    }
+                    aResp->res.jsonValue["Manufacturer"] = *value;
                 }
                 else if (property.first == "RevisionCode")
                 {
@@ -833,7 +893,13 @@
                 }
                 else if (property.first == "MemoryTotalWidth")
                 {
-                    aResp->res.jsonValue["BusWidthBits"] = property.second;
+                    const uint16_t* value =
+                        std::get_if<uint16_t>(&property.second);
+                    if (value == nullptr)
+                    {
+                        continue;
+                    }
+                    aResp->res.jsonValue["BusWidthBits"] = *value;
                 }
                 else if (property.first == "ECC")
                 {
@@ -886,7 +952,19 @@
                 }
                 else if (property.first == "AllowedSpeedsMT")
                 {
-                    aResp->res.jsonValue["AllowedSpeedsMHz"] = property.second;
+                    const std::vector<uint16_t>* value =
+                        std::get_if<std::vector<uint16_t>>(&property.second);
+                    if (value == nullptr)
+                    {
+                        continue;
+                    }
+                    nlohmann::json& jValue =
+                        aResp->res.jsonValue["AllowedSpeedsMHz"];
+                    jValue = nlohmann::json::array();
+                    for (uint16_t subVal : *value)
+                    {
+                        jValue.push_back(subVal);
+                    }
                 }
                 else if (property.first == "MemoryAttributes")
                 {
@@ -905,7 +983,13 @@
                 }
                 else if (property.first == "MemoryConfiguredSpeedInMhz")
                 {
-                    aResp->res.jsonValue["OperatingSpeedMhz"] = property.second;
+                    const uint16_t* value =
+                        std::get_if<uint16_t>(&property.second);
+                    if (value == nullptr)
+                    {
+                        continue;
+                    }
+                    aResp->res.jsonValue["OperatingSpeedMhz"] = *value;
                 }
                 else if (property.first == "MemoryType")
                 {
@@ -938,8 +1022,15 @@
                          property.first == "MemoryController" ||
                          property.first == "Slot" || property.first == "Socket")
                 {
+                    const std::string* value =
+                        std::get_if<std::string>(&property.second);
+                    if (value == nullptr)
+                    {
+                        messages::internalError(aResp->res);
+                        continue;
+                    }
                     aResp->res.jsonValue["MemoryLocation"][property.first] =
-                        property.second;
+                        *value;
                 }
                 else
                 {
@@ -975,7 +1066,13 @@
             {
                 if (key == "MemoryClassification")
                 {
-                    partition[key] = val;
+                    const std::string* value = std::get_if<std::string>(&val);
+                    if (value == nullptr)
+                    {
+                        messages::internalError(aResp->res);
+                        continue;
+                    }
+                    partition[key] = *value;
                 }
                 else if (key == "OffsetInKiB")
                 {
@@ -983,8 +1080,6 @@
                     if (value == nullptr)
                     {
                         messages::internalError(aResp->res);
-                        BMCWEB_LOG_DEBUG
-                            << "Invalid property type for OffsetInKiB";
                         continue;
                     }
 
@@ -992,12 +1087,24 @@
                 }
                 else if (key == "PartitionId")
                 {
-                    partition["RegionId"] = val;
+                    const std::string* value = std::get_if<std::string>(&val);
+                    if (value == nullptr)
+                    {
+                        messages::internalError(aResp->res);
+                        continue;
+                    }
+                    partition["RegionId"] = *value;
                 }
 
                 else if (key == "PassphraseState")
                 {
-                    partition["PassphraseEnabled"] = val;
+                    const bool* value = std::get_if<bool>(&val);
+                    if (value == nullptr)
+                    {
+                        messages::internalError(aResp->res);
+                        continue;
+                    }
+                    partition["PassphraseEnabled"] = *value;
                 }
                 else if (key == "SizeInKiB")
                 {
diff --git a/redfish-core/lib/task.hpp b/redfish-core/lib/task.hpp
index 8083566..19973d9 100644
--- a/redfish-core/lib/task.hpp
+++ b/redfish-core/lib/task.hpp
@@ -80,14 +80,6 @@
     nlohmann::json jsonBody;
 };
 
-inline void to_json(nlohmann::json& j, const Payload& p)
-{
-    j = {{"TargetUri", p.targetUri},
-         {"HttpOperation", p.httpOperation},
-         {"HttpHeaders", p.httpHeaders},
-         {"JsonBody", p.jsonBody.dump()}};
-}
-
 struct TaskData : std::enable_shared_from_this<TaskData>
 {
   private:
@@ -448,7 +440,12 @@
         }
         if (ptr->payload)
         {
-            asyncResp->res.jsonValue["Payload"] = *(ptr->payload);
+            const task::Payload& p = *(ptr->payload);
+            asyncResp->res.jsonValue["Payload"] = {
+                {"TargetUri", p.targetUri},
+                {"HttpOperation", p.httpOperation},
+                {"HttpHeaders", p.httpHeaders},
+                {"JsonBody", p.jsonBody.dump()}};
         }
     }
 };