xref: /openbmc/bmcweb/features/redfish/src/utils/json_utils.cpp (revision 0bdda665a3589924e1f5a51d7ff8633c6544ffa1)
177dd8813SKowalski, Kamil /*
277dd8813SKowalski, Kamil // Copyright (c) 2018 Intel Corporation
377dd8813SKowalski, Kamil //
477dd8813SKowalski, Kamil // Licensed under the Apache License, Version 2.0 (the "License");
577dd8813SKowalski, Kamil // you may not use this file except in compliance with the License.
677dd8813SKowalski, Kamil // You may obtain a copy of the License at
777dd8813SKowalski, Kamil //
877dd8813SKowalski, Kamil //      http://www.apache.org/licenses/LICENSE-2.0
977dd8813SKowalski, Kamil //
1077dd8813SKowalski, Kamil // Unless required by applicable law or agreed to in writing, software
1177dd8813SKowalski, Kamil // distributed under the License is distributed on an "AS IS" BASIS,
1277dd8813SKowalski, Kamil // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1377dd8813SKowalski, Kamil // See the License for the specific language governing permissions and
1477dd8813SKowalski, Kamil // limitations under the License.
1577dd8813SKowalski, Kamil */
1677dd8813SKowalski, Kamil #include "utils/json_utils.hpp"
171abe55efSEd Tanous 
181aa0c2b8SEd Tanous #include "error_messages.hpp"
191aa0c2b8SEd Tanous #include "http/http_request.hpp"
201aa0c2b8SEd Tanous #include "http/http_response.hpp"
211aa0c2b8SEd Tanous #include "http/parsing.hpp"
221aa0c2b8SEd Tanous 
231aa0c2b8SEd Tanous #include <nlohmann/json.hpp>
241aa0c2b8SEd Tanous 
25f0b59af4SEd Tanous #include <cstdint>
26f0b59af4SEd Tanous #include <string>
27f0b59af4SEd Tanous 
281abe55efSEd Tanous namespace redfish
291abe55efSEd Tanous {
3077dd8813SKowalski, Kamil 
311abe55efSEd Tanous namespace json_util
321abe55efSEd Tanous {
3377dd8813SKowalski, Kamil 
3455c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
351abe55efSEd Tanous                             nlohmann::json& reqJson)
361abe55efSEd Tanous {
371aa0c2b8SEd Tanous     JsonParseResult ret = parseRequestAsJson(req, reqJson);
381aa0c2b8SEd Tanous     if (ret == JsonParseResult::BadContentType)
391aa0c2b8SEd Tanous     {
401aa0c2b8SEd Tanous         messages::unrecognizedRequestBody(res);
411aa0c2b8SEd Tanous         return false;
421aa0c2b8SEd Tanous     }
4333c6b580SEd Tanous     reqJson = nlohmann::json::parse(req.body(), nullptr, false);
4477dd8813SKowalski, Kamil 
451abe55efSEd Tanous     if (reqJson.is_discarded())
461abe55efSEd Tanous     {
47f12894f8SJason M. Bills         messages::malformedJSON(res);
4877dd8813SKowalski, Kamil         return false;
4977dd8813SKowalski, Kamil     }
5077dd8813SKowalski, Kamil 
5177dd8813SKowalski, Kamil     return true;
5277dd8813SKowalski, Kamil }
5377dd8813SKowalski, Kamil 
548a7c4b47SNan Zhou uint64_t getEstimatedJsonSize(const nlohmann::json& root)
558a7c4b47SNan Zhou {
568a7c4b47SNan Zhou     if (root.is_null())
578a7c4b47SNan Zhou     {
588a7c4b47SNan Zhou         return 4;
598a7c4b47SNan Zhou     }
608a7c4b47SNan Zhou     if (root.is_number())
618a7c4b47SNan Zhou     {
628a7c4b47SNan Zhou         return 8;
638a7c4b47SNan Zhou     }
648a7c4b47SNan Zhou     if (root.is_boolean())
658a7c4b47SNan Zhou     {
668a7c4b47SNan Zhou         return 5;
678a7c4b47SNan Zhou     }
688a7c4b47SNan Zhou     if (root.is_string())
698a7c4b47SNan Zhou     {
708a7c4b47SNan Zhou         constexpr uint64_t quotesSize = 2;
718a7c4b47SNan Zhou         return root.get<std::string>().size() + quotesSize;
728a7c4b47SNan Zhou     }
738a7c4b47SNan Zhou     if (root.is_binary())
748a7c4b47SNan Zhou     {
758a7c4b47SNan Zhou         return root.get_binary().size();
768a7c4b47SNan Zhou     }
778a7c4b47SNan Zhou     const nlohmann::json::array_t* arr =
788a7c4b47SNan Zhou         root.get_ptr<const nlohmann::json::array_t*>();
798a7c4b47SNan Zhou     if (arr != nullptr)
808a7c4b47SNan Zhou     {
818a7c4b47SNan Zhou         uint64_t sum = 0;
828a7c4b47SNan Zhou         for (const auto& element : *arr)
838a7c4b47SNan Zhou         {
848a7c4b47SNan Zhou             sum += getEstimatedJsonSize(element);
858a7c4b47SNan Zhou         }
868a7c4b47SNan Zhou         return sum;
878a7c4b47SNan Zhou     }
888a7c4b47SNan Zhou     const nlohmann::json::object_t* object =
898a7c4b47SNan Zhou         root.get_ptr<const nlohmann::json::object_t*>();
908a7c4b47SNan Zhou     if (object != nullptr)
918a7c4b47SNan Zhou     {
928a7c4b47SNan Zhou         uint64_t sum = 0;
93*0bdda665SEd Tanous         for (const auto& [k, v] : *object)
948a7c4b47SNan Zhou         {
958a7c4b47SNan Zhou             constexpr uint64_t colonQuoteSpaceSize = 4;
968a7c4b47SNan Zhou             sum += k.size() + getEstimatedJsonSize(v) + colonQuoteSpaceSize;
978a7c4b47SNan Zhou         }
988a7c4b47SNan Zhou         return sum;
998a7c4b47SNan Zhou     }
1008a7c4b47SNan Zhou     return 0;
1018a7c4b47SNan Zhou }
1028a7c4b47SNan Zhou 
10377dd8813SKowalski, Kamil } // namespace json_util
10477dd8813SKowalski, Kamil } // namespace redfish
105