-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from microsoft/jsonattrs
Add AccessPointAttributes JSON serialization/deserialization support
- Loading branch information
Showing
9 changed files
with
234 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
#include <istream> | ||
#include <optional> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include <microsoft/net/wifi/AccessPointAttributes.hxx> | ||
|
||
#include "AccessPointAttributesJsonSerialization.hxx" | ||
|
||
using namespace Microsoft::Net::Wifi; | ||
|
||
/* static */ | ||
std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
AccessPointAttributes::TryParseJson(const std::string& json) | ||
{ | ||
return ParseAccessPointAttributesFromJson(json); | ||
} | ||
|
||
/* static */ | ||
std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
AccessPointAttributes::TryParseJson(std::istream& json) | ||
{ | ||
return ParseAccessPointAttributesFromJson(json); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/common/net/wifi/core/AccessPointAttributesJsonSerialization.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
#include <microsoft/net/wifi/AccessPointAttributes.hxx> | ||
#include <nlohmann/json.hpp> | ||
|
||
#include "AccessPointAttributesJsonSerialization.hxx" | ||
|
||
namespace Microsoft::Net::Wifi | ||
{ | ||
void | ||
to_json(nlohmann::json& accessPointAttributesJson, const AccessPointAttributes& accessPointAttributes) | ||
{ | ||
accessPointAttributesJson = nlohmann::json{ | ||
{ "Properties", accessPointAttributes.Properties } | ||
}; | ||
} | ||
|
||
void | ||
from_json(const nlohmann::json& accessPointAttributesJson, AccessPointAttributes& accessPointAttributes) | ||
{ | ||
accessPointAttributesJson.at("Properties").get_to(accessPointAttributes.Properties); | ||
} | ||
|
||
} // namespace Microsoft::Net::Wifi |
62 changes: 62 additions & 0 deletions
62
src/common/net/wifi/core/AccessPointAttributesJsonSerialization.hxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
#ifndef ACCESS_POINT_ATTRIBUTES_JSON_SERIALIZATION_HXX | ||
#define ACCESS_POINT_ATTRIBUTES_JSON_SERIALIZATION_HXX | ||
|
||
#include <format> | ||
#include <optional> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include <microsoft/net/wifi/AccessPointAttributes.hxx> | ||
#include <nlohmann/json.hpp> | ||
#include <plog/Log.h> | ||
|
||
namespace Microsoft::Net::Wifi | ||
{ | ||
/** | ||
* @brief Serialize access point attributes to JSON. | ||
* | ||
* @param accessPointAttributesJson The JSON object to hold the serialized data. | ||
* @param accessPointAttributes The access point attributes to serialize. | ||
*/ | ||
void | ||
to_json(nlohmann::json& accessPointAttributesJson, const AccessPointAttributes& accessPointAttributes); | ||
|
||
/** | ||
* @brief Deserialize access point attributes from JSON. | ||
* | ||
* @param accessPointAttributesJson The JSON object to deserialize. | ||
* @param accessPointAttributes The access point attributes to populate. | ||
*/ | ||
void | ||
from_json(const nlohmann::json& accessPointAttributesJson, AccessPointAttributes& accessPointAttributes); | ||
|
||
/** | ||
* @brief Parse access point attributes from a JSON input type. | ||
* | ||
* @tparam InputType The input type holding the JSON to parse. This must be one of the types supported by | ||
* nlohmann::json::parse (input stream, string, iterator pair, contiguous container, character array). | ||
* @param json The JSON input to parse. | ||
* @return std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
*/ | ||
template <typename InputType> | ||
std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
ParseAccessPointAttributesFromJson(InputType& json) | ||
{ | ||
std::unordered_map<std::string, AccessPointAttributes> accessPointAttributesMap; | ||
nlohmann::json accessPointAttributesJson; | ||
|
||
try { | ||
accessPointAttributesJson = nlohmann::json::parse(json); | ||
accessPointAttributesJson.get_to(accessPointAttributesMap); | ||
} catch (const nlohmann::json::parse_error& e) { | ||
LOGE << std::format("Failed to parse access point access point attributes JSON: {}", e.what()); | ||
return std::nullopt; | ||
} | ||
|
||
return accessPointAttributesMap; | ||
} | ||
|
||
} // namespace Microsoft::Net::Wifi | ||
|
||
#endif // ACCESS_POINT_ATTRIBUTES_JSON_SERIALIZATION_HXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/common/net/wifi/core/include/microsoft/net/wifi/AccessPointAttributes.hxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
#ifndef ACCESS_POINT_ATTRIBUTES_HXX | ||
#define ACCESS_POINT_ATTRIBUTES_HXX | ||
|
||
#include <istream> | ||
#include <optional> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace Microsoft::Net::Wifi | ||
{ | ||
/** | ||
* @brief Container to hold static attributes about an access point. | ||
*/ | ||
struct AccessPointAttributes | ||
{ | ||
/** | ||
* @brief Attempt to deserialize a JSON string into a map of access point attributes. | ||
* | ||
* @param json The JSON input string to parse. | ||
* @return std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
*/ | ||
static std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
TryParseJson(const std::string& json); | ||
|
||
/** | ||
* @brief Attempt to deserialize a JSON stream into a map of access point attributes. | ||
* | ||
* @param json The JSON input stream to parse. | ||
* @return std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
*/ | ||
static std::optional<std::unordered_map<std::string, AccessPointAttributes>> | ||
TryParseJson(std::istream& json); | ||
|
||
std::unordered_map<std::string, std::string> Properties{}; | ||
}; | ||
} // namespace Microsoft::Net::Wifi | ||
|
||
#endif // ACCESS_POINT_ATTRIBUTES_HXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
#include <microsoft/net/wifi/AccessPointAttributes.hxx> | ||
#include <nlohmann/json.hpp> | ||
|
||
TEST_CASE("AccessPointAttributes JSON Serialization and Deserialization", "[wifi][core][ap][serialization]") | ||
{ | ||
using namespace Microsoft::Net::Wifi; | ||
|
||
auto accessPointAttributesJson = R"( | ||
{ | ||
"wlan0": { | ||
"Properties": { | ||
"key1": "value1", | ||
"key2": "value2" | ||
} | ||
}, | ||
"wlan1": { | ||
"Properties": { | ||
"key1": "value1", | ||
"key2": "value2" | ||
} | ||
} | ||
} | ||
)"; | ||
|
||
// clang-format off | ||
std::unordered_map<std::string, AccessPointAttributes> AccessPointAttributesMap{ | ||
{ | ||
"wlan0", | ||
AccessPointAttributes{ | ||
{ | ||
{ "key1", "value1" }, | ||
{ "key2", "value2" } | ||
} | ||
}, | ||
}, | ||
{ | ||
"wlan1", | ||
AccessPointAttributes{ | ||
{ | ||
{ "key1", "value1" }, | ||
{ "key2", "value2" } | ||
} | ||
} | ||
} | ||
}; | ||
// clang-format on | ||
|
||
SECTION("Deserialization (direct) doesn't cause a crash") | ||
{ | ||
REQUIRE_NOTHROW(nlohmann::json::parse(accessPointAttributesJson)); | ||
} | ||
|
||
SECTION("Deserialization (wrapped) doesn't cause a crash") | ||
{ | ||
REQUIRE_NOTHROW(AccessPointAttributes::TryParseJson(accessPointAttributesJson)); | ||
} | ||
|
||
SECTION("Deserialization (wrapped) populates the access point attributes") | ||
{ | ||
auto deserializedAccessPointAttributesOpt = AccessPointAttributes::TryParseJson(accessPointAttributesJson); | ||
REQUIRE(deserializedAccessPointAttributesOpt.has_value()); | ||
auto& deserializedAccessPointAttributes = deserializedAccessPointAttributesOpt.value(); | ||
REQUIRE(std::size(deserializedAccessPointAttributes) == std::size(AccessPointAttributesMap)); | ||
|
||
for (const auto& [interfaceName, accessPointAttributes] : AccessPointAttributesMap) { | ||
REQUIRE(deserializedAccessPointAttributes.contains(interfaceName)); | ||
REQUIRE(deserializedAccessPointAttributes[interfaceName].Properties == accessPointAttributes.Properties); | ||
} | ||
} | ||
} |