diff options
| author | James Feist <james.feist@linux.intel.com> | 2018-12-17 14:55:48 -0800 |
|---|---|---|
| committer | James Feist <james.feist@linux.intel.com> | 2018-12-19 13:02:03 -0800 |
| commit | a6acbb3187910fc399152262328ee05e64486da8 (patch) | |
| tree | 98ca2a0a8ad33219db5092d5625167f19b1867b9 /redfish-core/include | |
| parent | 318226c278a18c1492b2235cb2c3b2ce5ed09900 (diff) | |
| download | bmcweb-a6acbb3187910fc399152262328ee05e64486da8.tar.gz bmcweb-a6acbb3187910fc399152262328ee05e64486da8.zip | |
jsonRead: Add floating point support
Add support of reading floating point numbers.
Tested-by: Successfully unpacked doubles
Change-Id: I9cf5e33dbb99367a53013be28b8f905eae2e4518
Signed-off-by: James Feist <james.feist@linux.intel.com>
Diffstat (limited to 'redfish-core/include')
| -rw-r--r-- | redfish-core/include/utils/json_utils.hpp | 82 |
1 files changed, 66 insertions, 16 deletions
diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp index 2eeace1..aa45261 100644 --- a/redfish-core/include/utils/json_utils.hpp +++ b/redfish-core/include/utils/json_utils.hpp @@ -78,40 +78,90 @@ struct is_std_array<std::array<Type, size>> : std::true_type template <typename Type> constexpr bool is_std_array_v = is_std_array<Type>::value; +template <typename ToType, typename FromType> +bool checkRange(const FromType* from, const std::string& key, + nlohmann::json& jsonValue, crow::Response& res) +{ + if (from == nullptr) + { + BMCWEB_LOG_DEBUG << "Value for key " << key + << " was incorrect type: " << __PRETTY_FUNCTION__; + messages::propertyValueTypeError(res, jsonValue.dump(), key); + return false; + } + + if (*from > std::numeric_limits<ToType>::max()) + { + BMCWEB_LOG_DEBUG << "Value for key " << key + << " was greater than max: " << __PRETTY_FUNCTION__; + messages::propertyValueNotInList(res, jsonValue.dump(), key); + return false; + } + if (*from < std::numeric_limits<ToType>::lowest()) + { + BMCWEB_LOG_DEBUG << "Value for key " << key + << " was less than min: " << __PRETTY_FUNCTION__; + messages::propertyValueNotInList(res, jsonValue.dump(), key); + return false; + } + if constexpr (std::is_floating_point_v<ToType>) + { + if (std::isnan(*from)) + { + BMCWEB_LOG_DEBUG << "Value for key " << key << " was NAN"; + messages::propertyValueNotInList(res, jsonValue.dump(), key); + return false; + } + } + + return true; +} + template <typename Type> void unpackValue(nlohmann::json& jsonValue, const std::string& key, crow::Response& res, Type& value) { - if constexpr (std::is_arithmetic_v<Type>) + if constexpr (std::is_floating_point_v<Type>) { - using NumType = - std::conditional_t<std::is_signed_v<Type>, int64_t, uint64_t>; + double helper = 0; + double* jsonPtr = jsonValue.get_ptr<double*>(); - NumType* jsonPtr = jsonValue.get_ptr<NumType*>(); if (jsonPtr == nullptr) { - BMCWEB_LOG_DEBUG - << "Value for key " << key - << " was incorrect type: " << jsonValue.type_name(); - messages::propertyValueTypeError(res, jsonValue.dump(), key); + int64_t* intPtr = jsonValue.get_ptr<int64_t*>(); + if (intPtr != nullptr) + { + helper = static_cast<double>(*intPtr); + jsonPtr = &helper; + } + } + if (!checkRange<Type>(jsonPtr, key, jsonValue, res)) + { return; } - if (*jsonPtr > std::numeric_limits<Type>::max()) + value = static_cast<Type>(*jsonPtr); + } + + else if constexpr (std::is_signed_v<Type>) + { + int64_t* jsonPtr = jsonValue.get_ptr<int64_t*>(); + if (!checkRange<Type>(jsonPtr, key, jsonValue, res)) { - BMCWEB_LOG_DEBUG << "Value for key " << key - << " was out of range: " << jsonValue.type_name(); - messages::propertyValueNotInList(res, jsonValue.dump(), key); return; } - if (*jsonPtr < std::numeric_limits<Type>::min()) + value = static_cast<Type>(*jsonPtr); + } + + else if constexpr (std::is_unsigned_v<Type>) + { + uint64_t* jsonPtr = jsonValue.get_ptr<uint64_t*>(); + if (!checkRange<Type>(jsonPtr, key, jsonValue, res)) { - BMCWEB_LOG_DEBUG << "Value for key " << key - << " was out of range: " << jsonValue.type_name(); - messages::propertyValueNotInList(res, jsonValue.dump(), key); return; } value = static_cast<Type>(*jsonPtr); } + else if constexpr (is_optional_v<Type>) { value.emplace(); |

