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 "view_data.h"
17
18 #include "ability_base_log_wrapper.h"
19 #include "nlohmann/json.hpp"
20
21 namespace OHOS {
22 namespace AbilityBase {
23 constexpr size_t NODES_SIZE_LIMIT = 20;
24 constexpr const char* VIEW_DATA_BUNDLE_NAME = "bundleName";
25 constexpr const char* VIEW_DATA_MODULE_NAME = "moduleName";
26 constexpr const char* VIEW_DATA_ABILITY_NAME = "abilityName";
27 constexpr const char* VIEW_DATA_PAGE_URL = "pageUrl";
28 constexpr const char* VIEW_DATA_NODES = "nodes";
29 constexpr const char* VIEW_DATA_PAGE_RECT = "pageRect";
30 constexpr const char* VIEW_DATA_USER_SELECTED = "isUserSelected";
31 constexpr const char* VIEW_DATA_OTHER_ACCOUNT = "isOtherAccount";
32
FromJsonString(const std::string & jsonStr)33 void ViewData::FromJsonString(const std::string& jsonStr)
34 {
35 nlohmann::json jsonObject = nlohmann::json::parse(jsonStr, nullptr, false);
36 if (jsonObject.is_discarded()) {
37 ABILITYBASE_LOGE("json parse failed");
38 return;
39 }
40 if (jsonObject.contains(VIEW_DATA_BUNDLE_NAME) && jsonObject[VIEW_DATA_BUNDLE_NAME].is_string()) {
41 bundleName = jsonObject.at(VIEW_DATA_BUNDLE_NAME).get<std::string>();
42 }
43 if (jsonObject.contains(VIEW_DATA_MODULE_NAME) && jsonObject[VIEW_DATA_MODULE_NAME].is_string()) {
44 moduleName = jsonObject.at(VIEW_DATA_MODULE_NAME).get<std::string>();
45 }
46 if (jsonObject.contains(VIEW_DATA_ABILITY_NAME) && jsonObject[VIEW_DATA_ABILITY_NAME].is_string()) {
47 abilityName = jsonObject.at(VIEW_DATA_ABILITY_NAME).get<std::string>();
48 }
49 if (jsonObject.contains(VIEW_DATA_PAGE_URL) && jsonObject[VIEW_DATA_PAGE_URL].is_string()) {
50 pageUrl = jsonObject.at(VIEW_DATA_PAGE_URL).get<std::string>();
51 }
52 if (jsonObject.contains(VIEW_DATA_USER_SELECTED) && jsonObject[VIEW_DATA_USER_SELECTED].is_boolean()) {
53 isUserSelected = jsonObject.at(VIEW_DATA_USER_SELECTED).get<bool>();
54 }
55 if (jsonObject.contains(VIEW_DATA_OTHER_ACCOUNT) && jsonObject[VIEW_DATA_OTHER_ACCOUNT].is_boolean()) {
56 isOtherAccount = jsonObject.at(VIEW_DATA_OTHER_ACCOUNT).get<bool>();
57 }
58 if (jsonObject.contains(VIEW_DATA_NODES) && jsonObject[VIEW_DATA_NODES].is_array()) {
59 nodes.clear();
60 auto size = jsonObject[VIEW_DATA_NODES].size() > NODES_SIZE_LIMIT ? NODES_SIZE_LIMIT :
61 jsonObject[VIEW_DATA_NODES].size();
62 for (size_t i = 0; i < size; i++) {
63 if (jsonObject[VIEW_DATA_NODES][i].is_string()) {
64 PageNodeInfo pageNodeInfo;
65 pageNodeInfo.FromJsonString(jsonObject[VIEW_DATA_NODES][i]);
66 nodes.emplace_back(pageNodeInfo);
67 }
68 }
69 }
70 if (jsonObject.contains(VIEW_DATA_PAGE_RECT) && jsonObject[VIEW_DATA_PAGE_RECT].is_string()) {
71 pageRect.FromJsonString(jsonObject[VIEW_DATA_PAGE_RECT]);
72 }
73 }
74
ToJsonString() const75 std::string ViewData::ToJsonString() const
76 {
77 nlohmann::json jsonNodes = nlohmann::json::array();
78 auto size = nodes.size() > NODES_SIZE_LIMIT ? NODES_SIZE_LIMIT : nodes.size();
79 for (size_t i = 0; i < size; i++) {
80 jsonNodes.emplace_back(nodes[i].ToJsonString());
81 }
82 nlohmann::json jsonObject {
83 {VIEW_DATA_BUNDLE_NAME, bundleName},
84 {VIEW_DATA_MODULE_NAME, moduleName},
85 {VIEW_DATA_ABILITY_NAME, abilityName},
86 {VIEW_DATA_PAGE_URL, pageUrl},
87 {VIEW_DATA_NODES, jsonNodes},
88 {VIEW_DATA_USER_SELECTED, isUserSelected},
89 {VIEW_DATA_OTHER_ACCOUNT, isOtherAccount},
90 {VIEW_DATA_PAGE_RECT, pageRect.ToJsonString()}
91 };
92 return jsonObject.dump();
93 }
94 } // namespace AbilityBase
95 } // namespace OHOS