xref: /openbmc/bmcweb/features/redfish/src/utils/json_utils.cpp (revision 33c6b58026e5dd3bc600d152a07b95f863bfa1a7)
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 
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 {
341aa0c2b8SEd Tanous     JsonParseResult ret = parseRequestAsJson(req, reqJson);
351aa0c2b8SEd Tanous     if (ret == JsonParseResult::BadContentType)
361aa0c2b8SEd Tanous     {
371aa0c2b8SEd Tanous         messages::unrecognizedRequestBody(res);
381aa0c2b8SEd Tanous         return false;
391aa0c2b8SEd Tanous     }
40*33c6b580SEd Tanous     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