代码检视修改 Signed-off-by:baozewei@huawei.com

Signed-off-by: b30052170 <baozewei@huawei.com>
This commit is contained in:
b30052170 2024-11-07 10:41:23 +08:00
parent 046dc69283
commit 2aec2df5e8

View File

@ -73,26 +73,35 @@ public:
: pagesizeId(pagesizeId), orientation(orientation), duplex(duplex), quality(quality),
defaultSetting(defaultSetting), setting(setting) {}
static void GetArrayFromJson(const json& value, std::vector<std::string> &target, const std::string& key)
{
if (value.contains(key) && value.is_object()) {
const json& jsonValue = value.at(key);
for (auto &element : jsonValue.items()) {
nlohmann::json item = element.value();
if (!item.is_string()) {
PRINT_HILOGW("this item in preference is not string");
continue;
}
target.emplace_back(item.get<std::string>());
}
} else {
PRINT_HILOGW("can not find target array in preference, key: %{public}s", key.c_str());
}
}
static PrinterPreference BuildPrinterPreferenceFromJson(const json& printerAttrs)
{
PrinterPreference printerPreference;
if (printerAttrs.contains("pagesizeId")) {
printerPreference.pagesizeId = printerAttrs.at("pagesizeId").get<std::vector<std::string>>();
}
if (printerAttrs.contains("orientation")) {
printerPreference.orientation = printerAttrs.at("orientation").get<std::vector<std::string>>();
}
if (printerAttrs.contains("duplex")) {
printerPreference.duplex = printerAttrs.at("duplex").get<std::vector<std::string>>();
}
if (printerAttrs.contains("quality")) {
printerPreference.quality = printerAttrs.at("quality").get<std::vector<std::string>>();
}
if (printerAttrs.contains("defaultSetting")) {
GetArrayFromJson(printerAttrs, printerPreference.pagesizeId, "pagesizeId");
GetArrayFromJson(printerAttrs, printerPreference.orientation, "orientation");
GetArrayFromJson(printerAttrs, printerPreference.duplex, "duplex");
GetArrayFromJson(printerAttrs, printerPreference.quality, "quality");
if (printerAttrs.contains("defaultSetting") && printerAttrs["defaultSetting"].is_object()) {
printerPreference.defaultSetting =
PreferenceSetting::BuildPreferenceSettingFromJson(printerAttrs.at("defaultSetting"));
}
if (printerAttrs.contains("setting")) {
if (printerAttrs.contains("setting") && printerAttrs["setting"].is_object()) {
printerPreference.setting = PreferenceSetting::BuildPreferenceSettingFromJson(printerAttrs.at("setting"));
}
return printerPreference;