1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "page_node_info.h"
17
18 #include "ability_base_log_wrapper.h"
19
20 namespace OHOS {
21 namespace AbilityBase {
22 constexpr const char* PAGE_NODE_INFO_ID = "id";
23 constexpr const char* PAGE_NODE_INFO_DEPTH = "depth";
24 constexpr const char* PAGE_NODE_INFO_AUTO_FILL_TYPE = "autoFillType";
25 constexpr const char* PAGE_NODE_INFO_TAG = "tag";
26 constexpr const char* PAGE_NODE_INFO_VALUE = "value";
27 constexpr const char* PAGE_NODE_INFO_PLACEHOLDER = "placeholder";
28 constexpr const char* PAGE_NODE_INFO_PASSWORD_RULES = "passwordRules";
29 constexpr const char* PAGE_NODE_INFO_META_DATA = "metadata";
30 constexpr const char* PAGE_NODE_INFO_ENABLE_AUTO_FILL = "enableAutoFill";
31 constexpr const char* PAGE_NODE_INFO_RECT = "rect";
32 constexpr const char* PAGE_NODE_INFO_IS_FOCUS = "isFocus";
33
FromJsonString(const std::string & jsonStr)34 void PageNodeInfo::FromJsonString(const std::string& jsonStr)
35 {
36 nlohmann::json jsonObject = nlohmann::json::parse(jsonStr, nullptr, false);
37 if (jsonObject.is_discarded()) {
38 ABILITYBASE_LOGE("json parse failed");
39 return;
40 }
41 if (jsonObject.contains(PAGE_NODE_INFO_ID) && jsonObject[PAGE_NODE_INFO_ID].is_number()) {
42 id = jsonObject.at(PAGE_NODE_INFO_ID).get<int32_t>();
43 }
44 if (jsonObject.contains(PAGE_NODE_INFO_DEPTH) && jsonObject[PAGE_NODE_INFO_DEPTH].is_number()) {
45 depth = jsonObject.at(PAGE_NODE_INFO_DEPTH).get<int32_t>();
46 }
47 if (jsonObject.contains(PAGE_NODE_INFO_AUTO_FILL_TYPE) && jsonObject[PAGE_NODE_INFO_AUTO_FILL_TYPE].is_number()) {
48 autoFillType = static_cast<AutoFillType>(jsonObject.at(PAGE_NODE_INFO_AUTO_FILL_TYPE).get<int32_t>());
49 }
50 if (jsonObject.contains(PAGE_NODE_INFO_TAG) && jsonObject[PAGE_NODE_INFO_TAG].is_string()) {
51 tag = jsonObject.at(PAGE_NODE_INFO_TAG).get<std::string>();
52 }
53 if (jsonObject.contains(PAGE_NODE_INFO_VALUE) && jsonObject[PAGE_NODE_INFO_VALUE].is_string()) {
54 value = jsonObject.at(PAGE_NODE_INFO_VALUE).get<std::string>();
55 }
56 if (jsonObject.contains(PAGE_NODE_INFO_PLACEHOLDER) && jsonObject[PAGE_NODE_INFO_PLACEHOLDER].is_string()) {
57 placeholder = jsonObject.at(PAGE_NODE_INFO_PLACEHOLDER).get<std::string>();
58 }
59 if (jsonObject.contains(PAGE_NODE_INFO_PASSWORD_RULES) && jsonObject[PAGE_NODE_INFO_PASSWORD_RULES].is_string()) {
60 passwordRules = jsonObject.at(PAGE_NODE_INFO_PASSWORD_RULES).get<std::string>();
61 }
62 if (jsonObject.contains(PAGE_NODE_INFO_META_DATA) && jsonObject[PAGE_NODE_INFO_META_DATA].is_string()) {
63 metadata = jsonObject.at(PAGE_NODE_INFO_META_DATA).get<std::string>();
64 }
65 if (jsonObject.contains(PAGE_NODE_INFO_ENABLE_AUTO_FILL) &&
66 jsonObject[PAGE_NODE_INFO_ENABLE_AUTO_FILL].is_boolean()) {
67 enableAutoFill = jsonObject.at(PAGE_NODE_INFO_ENABLE_AUTO_FILL).get<bool>();
68 }
69 ParseJsonToPageNodeInfo(jsonObject);
70 }
71
ParseJsonToPageNodeInfo(const nlohmann::json & jsonObject)72 void PageNodeInfo::ParseJsonToPageNodeInfo(const nlohmann::json& jsonObject)
73 {
74 if (jsonObject.is_discarded()) {
75 ABILITYBASE_LOGE("json parse failed");
76 return;
77 }
78 if (jsonObject.contains(PAGE_NODE_INFO_RECT)) {
79 rect.FromJsonString(jsonObject[PAGE_NODE_INFO_RECT]);
80 }
81 if (jsonObject.contains(PAGE_NODE_INFO_IS_FOCUS) && jsonObject[PAGE_NODE_INFO_IS_FOCUS].is_boolean()) {
82 isFocus = jsonObject.at(PAGE_NODE_INFO_IS_FOCUS).get<bool>();
83 }
84 }
85
ToJsonString() const86 std::string PageNodeInfo::ToJsonString() const
87 {
88 nlohmann::json jsonObject {
89 {PAGE_NODE_INFO_ID, id},
90 {PAGE_NODE_INFO_DEPTH, depth},
91 {PAGE_NODE_INFO_AUTO_FILL_TYPE, static_cast<int32_t>(autoFillType)},
92 {PAGE_NODE_INFO_TAG, tag},
93 {PAGE_NODE_INFO_VALUE, value},
94 {PAGE_NODE_INFO_PLACEHOLDER, placeholder},
95 {PAGE_NODE_INFO_PASSWORD_RULES, passwordRules},
96 {PAGE_NODE_INFO_META_DATA, metadata},
97 {PAGE_NODE_INFO_ENABLE_AUTO_FILL, enableAutoFill},
98 {PAGE_NODE_INFO_RECT, rect.ToJsonString()},
99 {PAGE_NODE_INFO_IS_FOCUS, isFocus}
100 };
101 return jsonObject.dump();
102 }
103 } // namespace AbilityBase
104 } // namespace OHOS