xref: /openbmc/bmcweb/features/redfish/src/utils/json_utils.cpp (revision 1aa0c2b84be62a20d8c37a11ad877e0a8a48c69d)
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 
18*1aa0c2b8SEd Tanous #include "error_messages.hpp"
19*1aa0c2b8SEd Tanous #include "http/http_request.hpp"
20*1aa0c2b8SEd Tanous #include "http/http_response.hpp"
21*1aa0c2b8SEd Tanous #include "http/parsing.hpp"
22*1aa0c2b8SEd Tanous 
23*1aa0c2b8SEd Tanous #include <nlohmann/json.hpp>
24*1aa0c2b8SEd Tanous 
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
2777dd8813SKowalski, Kamil 
281abe55efSEd Tanous namespace json_util
291abe55efSEd Tanous {
3077dd8813SKowalski, Kamil 
3155c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
321abe55efSEd Tanous                             nlohmann::json& reqJson)
331abe55efSEd Tanous {
34*1aa0c2b8SEd Tanous     JsonParseResult ret = parseRequestAsJson(req, reqJson);
35*1aa0c2b8SEd Tanous     if (ret == JsonParseResult::BadContentType)
36*1aa0c2b8SEd Tanous     {
37*1aa0c2b8SEd Tanous         messages::unrecognizedRequestBody(res);
38*1aa0c2b8SEd Tanous         return false;
39*1aa0c2b8SEd Tanous     }
4077dd8813SKowalski, Kamil     reqJson = nlohmann::json::parse(req.body, nullptr, false);
4177dd8813SKowalski, Kamil 
421abe55efSEd Tanous     if (reqJson.is_discarded())
431abe55efSEd Tanous     {
44f12894f8SJason M. Bills         messages::malformedJSON(res);
4577dd8813SKowalski, Kamil         return false;
4677dd8813SKowalski, Kamil     }
4777dd8813SKowalski, Kamil 
4877dd8813SKowalski, Kamil     return true;
4977dd8813SKowalski, Kamil }
5077dd8813SKowalski, Kamil 
5177dd8813SKowalski, Kamil } // namespace json_util
5277dd8813SKowalski, Kamil } // namespace redfish
53