diff options
author | Ed Tanous <ed@tanous.net> | 2024-03-06 23:04:01 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2024-03-19 03:34:21 +0300 |
commit | b6164cbec4dd7f5c4e6e7667b203874e11cd8b3c (patch) | |
tree | 37f79b6897b8e075be9895314912d70ed9fb910a /test | |
parent | b2896149c39967dd9d1ee79357bdc53537cfabd7 (diff) | |
download | bmcweb-b6164cbec4dd7f5c4e6e7667b203874e11cd8b3c.tar.xz |
Make readJson accept object_t
Redfish supports several type systems for json. This makes parsing into
proper types a challenge. Nlohmann supports 3 core data types,
nlohmann::json, which supports all json types (float, int, array,
object). Nlohmann::json::object_t, which is a specific typedef of
std::map, and nlohmann::json::array_t, which is a specific typedef of
std::map.
Redfish allows reading our arrays of complex objects, similar to
NtpServers: [null, {}, "string"]
Which makes it a challenge to support. This commit allows parsing out
objects as a nlohmann::object_t, which gives the ability to later use it
in a type safe manner, without having to call
get_ptr<nlohmann::json::object_t later>.
Tested:
Unit tests pass.
Change-Id: I4134338951ce27c2f56841a45b56bc64ad1753db
Signed-off-by: Ed Tanous <ed@tanous.net>
Diffstat (limited to 'test')
-rw-r--r-- | test/redfish-core/include/utils/json_utils_test.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/test/redfish-core/include/utils/json_utils_test.cpp b/test/redfish-core/include/utils/json_utils_test.cpp index 5485bba91e..ad4d80538d 100644 --- a/test/redfish-core/include/utils/json_utils_test.cpp +++ b/test/redfish-core/include/utils/json_utils_test.cpp @@ -48,6 +48,27 @@ TEST(ReadJson, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly) EXPECT_THAT(vec, ElementsAre(1, 2, 3)); } +TEST(ReadJson, ValidObjectElementsReturnsTrueResponseOkValuesUnpackedCorrectly) +{ + crow::Response res; + nlohmann::json::object_t jsonRequest; + jsonRequest["integer"] = 1; + jsonRequest["string"] = "hello"; + jsonRequest["vector"] = std::vector<uint64_t>{1, 2, 3}; + + int64_t integer = 0; + std::string str; + std::vector<uint64_t> vec; + ASSERT_TRUE(readJsonObject(jsonRequest, res, "integer", integer, "string", + str, "vector", vec)); + EXPECT_EQ(res.result(), boost::beast::http::status::ok); + EXPECT_THAT(res.jsonValue, IsEmpty()); + + EXPECT_EQ(integer, 1); + EXPECT_EQ(str, "hello"); + EXPECT_THAT(vec, ElementsAre(1, 2, 3)); +} + TEST(readJson, ExtraElementsReturnsFalseReponseIsBadRequest) { crow::Response res; |