Remove nlohmann::json::items()

nlohmann::json::items() throws an exception if the object in question is
not a json object.  This has the potential to cause problems, and isn't
in line with the standard that we code against.

Replace all uses of items with iterating the nlohmann::json::object_t.

This adds a new error check for pulling the object_t out of the
nlohmann::json object before each iteration, to ensure that we're
handling errors.

Tested: Redfish service validator passes.

Change-Id: I2934c9450ec296c76544c2a7c5855c9b519eae7f
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp
index 048d987..3bd256d 100644
--- a/include/persistent_data.hpp
+++ b/include/persistent_data.hpp
@@ -61,14 +61,20 @@
             }
             else
             {
-                for (const auto& item : data.items())
+                const nlohmann::json::object_t* obj =
+                    data.get_ptr<nlohmann::json::object_t*>();
+                if (obj == nullptr)
                 {
-                    if (item.key() == "revision")
+                    return;
+                }
+                for (const auto& item : *obj)
+                {
+                    if (item.first == "revision")
                     {
                         fileRevision = 0;
 
                         const uint64_t* uintPtr =
-                            item.value().get_ptr<const uint64_t*>();
+                            item.second.get_ptr<const uint64_t*>();
                         if (uintPtr == nullptr)
                         {
                             BMCWEB_LOG_ERROR("Failed to read revision flag");
@@ -78,24 +84,24 @@
                             fileRevision = *uintPtr;
                         }
                     }
-                    else if (item.key() == "system_uuid")
+                    else if (item.first == "system_uuid")
                     {
                         const std::string* jSystemUuid =
-                            item.value().get_ptr<const std::string*>();
+                            item.second.get_ptr<const std::string*>();
                         if (jSystemUuid != nullptr)
                         {
                             systemUuid = *jSystemUuid;
                         }
                     }
-                    else if (item.key() == "auth_config")
+                    else if (item.first == "auth_config")
                     {
                         SessionStore::getInstance()
                             .getAuthMethodsConfig()
-                            .fromJson(item.value());
+                            .fromJson(item.second);
                     }
-                    else if (item.key() == "sessions")
+                    else if (item.first == "sessions")
                     {
-                        for (const auto& elem : item.value())
+                        for (const auto& elem : item.second)
                         {
                             std::shared_ptr<UserSession> newSession =
                                 UserSession::fromJson(elem);
@@ -115,10 +121,10 @@
                                 newSession->sessionToken, newSession);
                         }
                     }
-                    else if (item.key() == "timeout")
+                    else if (item.first == "timeout")
                     {
                         const int64_t* jTimeout =
-                            item.value().get_ptr<int64_t*>();
+                            item.second.get_ptr<const int64_t*>();
                         if (jTimeout == nullptr)
                         {
                             BMCWEB_LOG_DEBUG(
@@ -131,15 +137,25 @@
                         SessionStore::getInstance().updateSessionTimeout(
                             sessionTimeoutInseconds);
                     }
-                    else if (item.key() == "eventservice_config")
+                    else if (item.first == "eventservice_config")
                     {
+                        const nlohmann::json::object_t* esobj =
+                            item.second
+                                .get_ptr<const nlohmann::json::object_t*>();
+                        if (esobj == nullptr)
+                        {
+                            BMCWEB_LOG_DEBUG(
+                                "Problem reading EventService value");
+                            continue;
+                        }
+
                         EventServiceStore::getInstance()
                             .getEventServiceConfig()
-                            .fromJson(item.value());
+                            .fromJson(*esobj);
                     }
-                    else if (item.key() == "subscriptions")
+                    else if (item.first == "subscriptions")
                     {
-                        for (const auto& elem : item.value())
+                        for (const auto& elem : item.second)
                         {
                             std::shared_ptr<UserSubscription> newSubscription =
                                 UserSubscription::fromJson(elem);