json_parser_utils: Add parseUint16
Add new parseUint16() function to the json_parser_utils namespace. The
function parses a uint16_t value within a JSON element.
Tested:
* Ran automated test cases.
Change-Id: Id7c7c63f7a944a1910818927c3c70cb579f07dbd
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/json_parser_utils.cpp b/json_parser_utils.cpp
index 25b2e9a..78064fd 100644
--- a/json_parser_utils.cpp
+++ b/json_parser_utils.cpp
@@ -186,6 +186,17 @@
return static_cast<uint8_t>(value);
}
+uint16_t parseUint16(const nlohmann::json& element,
+ const std::map<std::string, std::string>& variables)
+{
+ int value = parseInteger(element, variables);
+ if ((value < 0) || (value > UINT16_MAX))
+ {
+ throw std::invalid_argument{"Element is not a 16-bit unsigned integer"};
+ }
+ return static_cast<uint16_t>(value);
+}
+
unsigned int parseUnsignedInteger(
const nlohmann::json& element,
const std::map<std::string, std::string>& variables)