1 /* 2 // Copyright (c) 2018 Intel Corporation 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 */ 16 #pragma once 17 #include <nlohmann/json.hpp> 18 #include <crow/http_request.h> 19 #include <crow/http_response.h> 20 21 namespace redfish { 22 23 namespace json_util { 24 25 /** 26 * @brief Defines JSON utils operation status 27 */ 28 enum class Result { SUCCESS, NOT_EXIST, WRONG_TYPE, NULL_POINTER }; 29 30 /** 31 * @brief Describes JSON utils messages requirement 32 */ 33 enum class MessageSetting { 34 NONE = 0x0, ///< No messages will be added 35 MISSING = 0x1, ///< PropertyMissing message will be added 36 TYPE_ERROR = 0x2 ///< PropertyValueTypeError message will be added 37 }; 38 39 /** 40 * @brief Wrapper function for extracting string from JSON object without 41 * throwing exceptions 42 * 43 * @param[in] fieldName Name of requested field 44 * @param[in] json JSON object from which field should be extracted 45 * @param[out] output Variable to which extracted will be written in case 46 * of success 47 * 48 * @return Result informing about operation status, output will be 49 * written only in case of Result::SUCCESS 50 */ 51 Result getString(const char* fieldName, const nlohmann::json& json, 52 const std::string*& output); 53 54 /** 55 * @brief Wrapper function for extracting object from JSON object without 56 * throwing exceptions 57 * 58 * @param[in] fieldName Name of requested field 59 * @param[in] json JSON object from which field should be extracted 60 * @param[out] output Variable to which extracted will be written in case 61 * of success 62 * 63 * @return Result informing about operation status, output will be 64 * written only in case of Result::SUCCESS 65 */ 66 Result getObject(const char* fieldName, const nlohmann::json& json, 67 nlohmann::json* output); 68 69 /** 70 * @brief Wrapper function for extracting array from JSON object without 71 * throwing exceptions 72 * 73 * @param[in] fieldName Name of requested field 74 * @param[in] json JSON object from which field should be extracted 75 * @param[out] output Variable to which extracted will be written in case 76 * of success 77 * 78 * @return Result informing about operation status, output will be 79 * written only in case of Result::SUCCESS 80 */ 81 Result getArray(const char* fieldName, const nlohmann::json& json, 82 nlohmann::json* output); 83 84 /** 85 * @brief Wrapper function for extracting int from JSON object without 86 * throwing exceptions 87 * 88 * @param[in] fieldName Name of requested field 89 * @param[in] json JSON object from which field should be extracted 90 * @param[out] output Variable to which extracted will be written in case 91 * of success 92 * 93 * @return Result informing about operation status, output will be 94 * written only in case of Result::SUCCESS 95 */ 96 Result getInt(const char* fieldName, const nlohmann::json& json, 97 int64_t& output); 98 99 /** 100 * @brief Wrapper function for extracting uint from JSON object without 101 * throwing exceptions 102 * 103 * @param[in] fieldName Name of requested field 104 * @param[in] json JSON object from which field should be extracted 105 * @param[out] output Variable to which extracted will be written in case 106 * of success 107 * 108 * @return Result informing about operation status, output will be 109 * written only in case of Result::SUCCESS 110 */ 111 Result getUnsigned(const char* fieldName, const nlohmann::json& json, 112 uint64_t& output); 113 114 /** 115 * @brief Wrapper function for extracting bool from JSON object without 116 * throwing exceptions 117 * 118 * @param[in] fieldName Name of requested field 119 * @param[in] json JSON object from which field should be extracted 120 * @param[out] output Variable to which extracted will be written in case 121 * of success 122 * 123 * @return Result informing about operation status, output will be 124 * written only in case of Result::SUCCESS 125 */ 126 Result getBool(const char* fieldName, const nlohmann::json& json, bool& output); 127 128 /** 129 * @brief Wrapper function for extracting float from JSON object without 130 * throwing exceptions (nlohmann stores JSON floats as C++ doubles) 131 * 132 * @param[in] fieldName Name of requested field 133 * @param[in] json JSON object from which field should be extracted 134 * @param[out] output Variable to which extracted will be written in case 135 * of success 136 * 137 * @return Result informing about operation status, output will be 138 * written only in case of Result::SUCCESS 139 */ 140 Result getDouble(const char* fieldName, const nlohmann::json& json, 141 double& output); 142 143 /** 144 * @brief Wrapper function for extracting string from JSON object without 145 * throwing exceptions 146 * 147 * @param[in] fieldName Name of requested field 148 * @param[in] json JSON object from which field should be extracted 149 * @param[out] output Variable to which extracted will be written in case 150 * of success 151 * @param[in] msgCfgMap Map for message addition settings 152 * @param[out] msgJson JSON to which error messages will be added 153 * @param[in] fieldPath Field path in JSON 154 * 155 * @return Result informing about operation status, output will be 156 * written only in case of Result::SUCCESS 157 */ 158 Result getString(const char* fieldName, const nlohmann::json& json, 159 const std::string*& output, uint8_t msgCfgMap, 160 nlohmann::json& msgJson, const std::string&& fieldPath); 161 162 /** 163 * @brief Wrapper function for extracting object from JSON object without 164 * throwing exceptions 165 * 166 * @param[in] fieldName Name of requested field 167 * @param[in] json JSON object from which field should be extracted 168 * @param[out] output Variable to which extracted will be written in case 169 * of success 170 * @param[in] msgCfgMap Map for message addition settings 171 * @param[out] msgJson JSON to which error messages will be added 172 * @param[in] fieldPath Field path in JSON 173 * 174 * @return Result informing about operation status, output will be 175 * written only in case of Result::SUCCESS 176 */ 177 Result getObject(const char* fieldName, const nlohmann::json& json, 178 nlohmann::json* output, uint8_t msgCfgMap, 179 nlohmann::json& msgJson, const std::string&& fieldPath); 180 181 /** 182 * @brief Wrapper function for extracting array from JSON object without 183 * throwing exceptions 184 * 185 * @param[in] fieldName Name of requested field 186 * @param[in] json JSON object from which field should be extracted 187 * @param[out] output Variable to which extracted will be written in case 188 * of success 189 * @param[in] msgCfgMap Map for message addition settings 190 * @param[out] msgJson JSON to which error messages will be added 191 * @param[in] fieldPath Field path in JSON 192 * 193 * @return Result informing about operation status, output will be 194 * written only in case of Result::SUCCESS 195 */ 196 Result getArray(const char* fieldName, const nlohmann::json& json, 197 nlohmann::json* output, uint8_t msgCfgMap, 198 nlohmann::json& msgJson, const std::string&& fieldPath); 199 200 /** 201 * @brief Wrapper function for extracting int from JSON object without 202 * throwing exceptions 203 * 204 * @param[in] fieldName Name of requested field 205 * @param[in] json JSON object from which field should be extracted 206 * @param[out] output Variable to which extracted will be written in case 207 * of success 208 * @param[in] msgCfgMap Map for message addition settings 209 * @param[out] msgJson JSON to which error messages will be added 210 * @param[in] fieldPath Field path in JSON 211 * 212 * @return Result informing about operation status, output will be 213 * written only in case of Result::SUCCESS 214 */ 215 Result getInt(const char* fieldName, const nlohmann::json& json, 216 int64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson, 217 const std::string&& fieldPath); 218 219 /** 220 * @brief Wrapper function for extracting uint from JSON object without 221 * throwing exceptions 222 * 223 * @param[in] fieldName Name of requested field 224 * @param[in] json JSON object from which field should be extracted 225 * @param[out] output Variable to which extracted will be written in case 226 * of success 227 * @param[in] msgCfgMap Map for message addition settings 228 * @param[out] msgJson JSON to which error messages will be added 229 * @param[in] fieldPath Field path in JSON 230 * 231 * @return Result informing about operation status, output will be 232 * written only in case of Result::SUCCESS 233 */ 234 Result getUnsigned(const char* fieldName, const nlohmann::json& json, 235 uint64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson, 236 const std::string&& fieldPath); 237 238 /** 239 * @brief Wrapper function for extracting bool from JSON object without 240 * throwing exceptions 241 * 242 * @param[in] fieldName Name of requested field 243 * @param[in] json JSON object from which field should be extracted 244 * @param[out] output Variable to which extracted will be written in case 245 * of success 246 * @param[in] msgCfgMap Map for message addition settings 247 * @param[out] msgJson JSON to which error messages will be added 248 * @param[in] fieldPath Field path in JSON 249 * 250 * @return Result informing about operation status, output will be 251 * written only in case of Result::SUCCESS 252 */ 253 Result getBool(const char* fieldName, const nlohmann::json& json, bool& output, 254 uint8_t msgCfgMap, nlohmann::json& msgJson, 255 const std::string&& fieldPath); 256 257 /** 258 * @brief Wrapper function for extracting float from JSON object without 259 * throwing exceptions (nlohmann stores JSON floats as C++ doubles) 260 * 261 * @param[in] fieldName Name of requested field 262 * @param[in] json JSON object from which field should be extracted 263 * @param[out] output Variable to which extracted will be written in case 264 * of success 265 * @param[in] msgCfgMap Map for message addition settings 266 * @param[out] msgJson JSON to which error messages will be added 267 * @param[in] fieldPath Field path in JSON 268 * 269 * @return Result informing about operation status, output will be 270 * written only in case of Result::SUCCESS 271 */ 272 Result getDouble(const char* fieldName, const nlohmann::json& json, 273 double& output, uint8_t msgCfgMap, nlohmann::json& msgJson, 274 const std::string&& fieldPath); 275 276 /** 277 * @brief Processes request to extract JSON from its body. If it fails, adds 278 * MalformedJSON message to response and ends it. 279 * 280 * @param[io] res Response object 281 * @param[in] req Request object 282 * @param[out] reqJson JSON object extracted from request's body 283 * 284 * @return true if JSON is valid, false when JSON is invalid and response has 285 * been filled with message and ended. 286 */ 287 bool processJsonFromRequest(crow::Response& res, const crow::Request& req, 288 nlohmann::json& reqJson); 289 290 } // namespace json_util 291 292 } // namespace redfish 293