Added JSON utilities to allow easy exception-less usage of nlohmann JSON.
These functions are not yet used, but will be required by at least two upcoming
patchsets. This functionality has been cut out from Configuration patchset for
easier merge without having to wait for that commit.
Change-Id: Ibe5d5cefd874d4a2d896b42a2b7cfc17480f3c5a
Signed-off-by: Kowalski, Kamil <kamil.kowalski@intel.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3ff1182..72002f2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -139,6 +139,7 @@
set(SRC_FILES
redfish-core/src/error_messages.cpp
+ redfish-core/src/utils/json_utils.cpp
${GENERATED_SRC_FILES}
)
diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp
new file mode 100644
index 0000000..5651513
--- /dev/null
+++ b/redfish-core/include/utils/json_utils.hpp
@@ -0,0 +1,292 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+#pragma once
+#include <nlohmann/json.hpp>
+#include <crow/http_request.h>
+#include <crow/http_response.h>
+
+namespace redfish {
+
+namespace json_util {
+
+/**
+ * @brief Defines JSON utils operation status
+ */
+enum class Result { SUCCESS, NOT_EXIST, WRONG_TYPE, NULL_POINTER };
+
+/**
+ * @brief Describes JSON utils messages requirement
+ */
+enum class MessageSetting {
+ NONE = 0x0, ///< No messages will be added
+ MISSING = 0x1, ///< PropertyMissing message will be added
+ TYPE_ERROR = 0x2 ///< PropertyValueTypeError message will be added
+};
+
+/**
+ * @brief Wrapper function for extracting string from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getString(const char* fieldName, const nlohmann::json& json,
+ const std::string*& output);
+
+/**
+ * @brief Wrapper function for extracting object from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getObject(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output);
+
+/**
+ * @brief Wrapper function for extracting array from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getArray(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output);
+
+/**
+ * @brief Wrapper function for extracting int from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getInt(const char* fieldName, const nlohmann::json& json,
+ int64_t& output);
+
+/**
+ * @brief Wrapper function for extracting uint from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getUnsigned(const char* fieldName, const nlohmann::json& json,
+ uint64_t& output);
+
+/**
+ * @brief Wrapper function for extracting bool from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getBool(const char* fieldName, const nlohmann::json& json, bool& output);
+
+/**
+ * @brief Wrapper function for extracting float from JSON object without
+ * throwing exceptions (nlohmann stores JSON floats as C++ doubles)
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getDouble(const char* fieldName, const nlohmann::json& json,
+ double& output);
+
+/**
+ * @brief Wrapper function for extracting string from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ * @param[in] msgCfgMap Map for message addition settings
+ * @param[out] msgJson JSON to which error messages will be added
+ * @param[in] fieldPath Field path in JSON
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getString(const char* fieldName, const nlohmann::json& json,
+ const std::string*& output, uint8_t msgCfgMap,
+ nlohmann::json& msgJson, const std::string&& fieldPath);
+
+/**
+ * @brief Wrapper function for extracting object from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ * @param[in] msgCfgMap Map for message addition settings
+ * @param[out] msgJson JSON to which error messages will be added
+ * @param[in] fieldPath Field path in JSON
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getObject(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output, uint8_t msgCfgMap,
+ nlohmann::json& msgJson, const std::string&& fieldPath);
+
+/**
+ * @brief Wrapper function for extracting array from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ * @param[in] msgCfgMap Map for message addition settings
+ * @param[out] msgJson JSON to which error messages will be added
+ * @param[in] fieldPath Field path in JSON
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getArray(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output, uint8_t msgCfgMap,
+ nlohmann::json& msgJson, const std::string&& fieldPath);
+
+/**
+ * @brief Wrapper function for extracting int from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ * @param[in] msgCfgMap Map for message addition settings
+ * @param[out] msgJson JSON to which error messages will be added
+ * @param[in] fieldPath Field path in JSON
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getInt(const char* fieldName, const nlohmann::json& json,
+ int64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath);
+
+/**
+ * @brief Wrapper function for extracting uint from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ * @param[in] msgCfgMap Map for message addition settings
+ * @param[out] msgJson JSON to which error messages will be added
+ * @param[in] fieldPath Field path in JSON
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getUnsigned(const char* fieldName, const nlohmann::json& json,
+ uint64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath);
+
+/**
+ * @brief Wrapper function for extracting bool from JSON object without
+ * throwing exceptions
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ * @param[in] msgCfgMap Map for message addition settings
+ * @param[out] msgJson JSON to which error messages will be added
+ * @param[in] fieldPath Field path in JSON
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getBool(const char* fieldName, const nlohmann::json& json, bool& output,
+ uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath);
+
+/**
+ * @brief Wrapper function for extracting float from JSON object without
+ * throwing exceptions (nlohmann stores JSON floats as C++ doubles)
+ *
+ * @param[in] fieldName Name of requested field
+ * @param[in] json JSON object from which field should be extracted
+ * @param[out] output Variable to which extracted will be written in case
+ * of success
+ * @param[in] msgCfgMap Map for message addition settings
+ * @param[out] msgJson JSON to which error messages will be added
+ * @param[in] fieldPath Field path in JSON
+ *
+ * @return Result informing about operation status, output will be
+ * written only in case of Result::SUCCESS
+ */
+Result getDouble(const char* fieldName, const nlohmann::json& json,
+ double& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath);
+
+/**
+ * @brief Processes request to extract JSON from its body. If it fails, adds
+ * MalformedJSON message to response and ends it.
+ *
+ * @param[io] res Response object
+ * @param[in] req Request object
+ * @param[out] reqJson JSON object extracted from request's body
+ *
+ * @return true if JSON is valid, false when JSON is invalid and response has
+ * been filled with message and ended.
+ */
+bool processJsonFromRequest(crow::response& res, const crow::request& req,
+ nlohmann::json& reqJson);
+
+} // namespace json_util
+
+} // namespace redfish
diff --git a/redfish-core/src/utils/json_utils.cpp b/redfish-core/src/utils/json_utils.cpp
new file mode 100644
index 0000000..b4bdba6
--- /dev/null
+++ b/redfish-core/src/utils/json_utils.cpp
@@ -0,0 +1,454 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+#include "utils/json_utils.hpp"
+#include <error_messages.hpp>
+#include <crow/http_codes.h>
+
+namespace redfish {
+
+namespace json_util {
+
+Result getString(const char* fieldName, const nlohmann::json& json,
+ const std::string*& output) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ return Result::NOT_EXIST;
+ }
+
+ output = fieldIt->get_ptr<const std::string*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (output == nullptr) {
+ return Result::WRONG_TYPE;
+ }
+
+ return Result::SUCCESS;
+}
+
+Result getObject(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output) {
+ // Verify input pointer
+ if (output == nullptr) {
+ return Result::NULL_POINTER;
+ }
+
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ return Result::NOT_EXIST;
+ }
+
+ // Verify type
+ if (!fieldIt->is_object()) {
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ *output = *fieldIt;
+
+ return Result::SUCCESS;
+}
+
+Result getArray(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output) {
+ // Verify input pointer
+ if (output == nullptr) {
+ return Result::NULL_POINTER;
+ }
+
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ return Result::NOT_EXIST;
+ }
+
+ // Verify type
+ if (!fieldIt->is_array()) {
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ *output = *fieldIt;
+
+ return Result::SUCCESS;
+}
+
+Result getInt(const char* fieldName, const nlohmann::json& json,
+ int64_t& output) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ return Result::NOT_EXIST;
+ }
+
+ const int64_t* retVal = fieldIt->get_ptr<const int64_t*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+Result getUnsigned(const char* fieldName, const nlohmann::json& json,
+ uint64_t& output) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ return Result::NOT_EXIST;
+ }
+
+ const uint64_t* retVal = fieldIt->get_ptr<const uint64_t*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+Result getBool(const char* fieldName, const nlohmann::json& json,
+ bool& output) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ return Result::NOT_EXIST;
+ }
+
+ const bool* retVal = fieldIt->get_ptr<const bool*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+Result getDouble(const char* fieldName, const nlohmann::json& json,
+ double& output) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ return Result::NOT_EXIST;
+ }
+
+ const double* retVal = fieldIt->get_ptr<const double*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+Result getString(const char* fieldName, const nlohmann::json& json,
+ const std::string*& output, uint8_t msgCfgMap,
+ nlohmann::json& msgJson, const std::string&& fieldPath) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::MISSING)) {
+ messages::addMessageToJson(msgJson, messages::propertyMissing(fieldName),
+ fieldPath);
+ }
+
+ return Result::NOT_EXIST;
+ }
+
+ output = fieldIt->get_ptr<const std::string*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (output == nullptr) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR)) {
+ messages::addMessageToJson(
+ msgJson, messages::propertyValueTypeError(fieldIt->dump(), fieldName),
+ fieldPath);
+ }
+
+ return Result::WRONG_TYPE;
+ }
+
+ return Result::SUCCESS;
+}
+
+Result getObject(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output, uint8_t msgCfgMap,
+ nlohmann::json& msgJson, const std::string&& fieldPath) {
+ // Verify input pointer
+ if (output == nullptr) {
+ return Result::NULL_POINTER;
+ }
+
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::MISSING)) {
+ messages::addMessageToJson(msgJson, messages::propertyMissing(fieldName),
+ fieldPath);
+ }
+
+ return Result::NOT_EXIST;
+ }
+
+ // Verify type
+ if (!fieldIt->is_object()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR)) {
+ messages::addMessageToJson(
+ msgJson, messages::propertyValueTypeError(fieldIt->dump(), fieldName),
+ fieldPath);
+ }
+
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ *output = *fieldIt;
+
+ return Result::SUCCESS;
+}
+
+Result getArray(const char* fieldName, const nlohmann::json& json,
+ nlohmann::json* output, uint8_t msgCfgMap,
+ nlohmann::json& msgJson, const std::string&& fieldPath) {
+ // Verify input pointer
+ if (output == nullptr) {
+ return Result::NULL_POINTER;
+ }
+
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::MISSING)) {
+ messages::addMessageToJson(msgJson, messages::propertyMissing(fieldName),
+ fieldPath);
+ }
+
+ return Result::NOT_EXIST;
+ }
+
+ // Verify type
+ if (!fieldIt->is_array()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR)) {
+ messages::addMessageToJson(
+ msgJson, messages::propertyValueTypeError(fieldIt->dump(), fieldName),
+ fieldPath);
+ }
+
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ *output = *fieldIt;
+
+ return Result::SUCCESS;
+}
+
+Result getInt(const char* fieldName, const nlohmann::json& json,
+ int64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::MISSING)) {
+ messages::addMessageToJson(msgJson, messages::propertyMissing(fieldName),
+ fieldPath);
+ }
+
+ return Result::NOT_EXIST;
+ }
+
+ const int64_t* retVal = fieldIt->get_ptr<const int64_t*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR)) {
+ messages::addMessageToJson(
+ msgJson, messages::propertyValueTypeError(fieldIt->dump(), fieldName),
+ fieldPath);
+ }
+
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+Result getUnsigned(const char* fieldName, const nlohmann::json& json,
+ uint64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::MISSING)) {
+ messages::addMessageToJson(msgJson, messages::propertyMissing(fieldName),
+ fieldPath);
+ }
+
+ return Result::NOT_EXIST;
+ }
+
+ const uint64_t* retVal = fieldIt->get_ptr<const uint64_t*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR)) {
+ messages::addMessageToJson(
+ msgJson, messages::propertyValueTypeError(fieldIt->dump(), fieldName),
+ fieldPath);
+ }
+
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+Result getBool(const char* fieldName, const nlohmann::json& json, bool& output,
+ uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::MISSING)) {
+ messages::addMessageToJson(msgJson, messages::propertyMissing(fieldName),
+ fieldPath);
+ }
+
+ return Result::NOT_EXIST;
+ }
+
+ const bool* retVal = fieldIt->get_ptr<const bool*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR)) {
+ messages::addMessageToJson(
+ msgJson, messages::propertyValueTypeError(fieldIt->dump(), fieldName),
+ fieldPath);
+ }
+
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+Result getDouble(const char* fieldName, const nlohmann::json& json,
+ double& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
+ const std::string&& fieldPath) {
+ // Find field
+ auto fieldIt = json.find(fieldName);
+
+ // Verify existence
+ if (fieldIt == json.end()) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::MISSING)) {
+ messages::addMessageToJson(msgJson, messages::propertyMissing(fieldName),
+ fieldPath);
+ }
+
+ return Result::NOT_EXIST;
+ }
+
+ const double* retVal = fieldIt->get_ptr<const double*>();
+
+ // Verify type - we know that it exists, so nullptr means wrong type
+ if (retVal == nullptr) {
+ if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR)) {
+ messages::addMessageToJson(
+ msgJson, messages::propertyValueTypeError(fieldIt->dump(), fieldName),
+ fieldPath);
+ }
+
+ return Result::WRONG_TYPE;
+ }
+
+ // Extract value
+ output = *retVal;
+
+ return Result::SUCCESS;
+}
+
+bool processJsonFromRequest(crow::response& res, const crow::request& req,
+ nlohmann::json& reqJson) {
+ reqJson = nlohmann::json::parse(req.body, nullptr, false);
+
+ if (reqJson.is_discarded()) {
+ messages::addMessageToErrorJson(res.json_value, messages::malformedJSON());
+
+ res.code = static_cast<int>(HttpRespCode::BAD_REQUEST);
+ res.end();
+
+ return false;
+ }
+
+ return true;
+}
+
+} // namespace json_util
+
+} // namespace redfish