summaryrefslogtreecommitdiff
path: root/redfish-core/src/utils
diff options
context:
space:
mode:
authorNan Zhou <nanzhoumails@gmail.com>2022-08-16 03:44:20 +0300
committerEd Tanous <edtanous@google.com>2023-06-21 20:08:20 +0300
commit8a7c4b475469c8811dffe265992b903060aad65f (patch)
treed8b8bd3ba1c3129cc8232abd92c139a7d2586c9d /redfish-core/src/utils
parent6fde5971ff53f43ece1aa3977b0689390934b06e (diff)
downloadbmcweb-8a7c4b475469c8811dffe265992b903060aad65f.tar.xz
json utils: add getEstimatedJsonSize
Add a utility function which estimates the size of the JSON tree. It is used in the children change to limit the reponse size of expand query. Tested: 1. unit test passed; 2. tested on hardware, the following are real sizes and estimation ``` Real payload size, Estimation, query 15.69 KB, 10.21 KB, redfish/v1?$expand=.($levels=1) 95.76 KB, 62.11 KB, redfish/v1?$expand=.($levels=2) 117.14 KB, 72.71 KB, redfish/v1?$expand=.($levels=3) 127.65 KB, 77.64 KB, redfish/v1?$expand=.($levels=4) ``` Signed-off-by: Nan Zhou <nanzhoumails@gmail.com> Change-Id: Iae26d6732a6ec63ecc59eacf657b4bf33c07c046
Diffstat (limited to 'redfish-core/src/utils')
-rw-r--r--redfish-core/src/utils/json_utils.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/redfish-core/src/utils/json_utils.cpp b/redfish-core/src/utils/json_utils.cpp
index 5daf95b058..5e44199d44 100644
--- a/redfish-core/src/utils/json_utils.cpp
+++ b/redfish-core/src/utils/json_utils.cpp
@@ -48,5 +48,54 @@ bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
return true;
}
+uint64_t getEstimatedJsonSize(const nlohmann::json& root)
+{
+ if (root.is_null())
+ {
+ return 4;
+ }
+ if (root.is_number())
+ {
+ return 8;
+ }
+ if (root.is_boolean())
+ {
+ return 5;
+ }
+ if (root.is_string())
+ {
+ constexpr uint64_t quotesSize = 2;
+ return root.get<std::string>().size() + quotesSize;
+ }
+ if (root.is_binary())
+ {
+ return root.get_binary().size();
+ }
+ const nlohmann::json::array_t* arr =
+ root.get_ptr<const nlohmann::json::array_t*>();
+ if (arr != nullptr)
+ {
+ uint64_t sum = 0;
+ for (const auto& element : *arr)
+ {
+ sum += getEstimatedJsonSize(element);
+ }
+ return sum;
+ }
+ const nlohmann::json::object_t* object =
+ root.get_ptr<const nlohmann::json::object_t*>();
+ if (object != nullptr)
+ {
+ uint64_t sum = 0;
+ for (const auto& [k, v] : root.items())
+ {
+ constexpr uint64_t colonQuoteSpaceSize = 4;
+ sum += k.size() + getEstimatedJsonSize(v) + colonQuoteSpaceSize;
+ }
+ return sum;
+ }
+ return 0;
+}
+
} // namespace json_util
} // namespace redfish