1 /*
2 * Copyright (c) 2023 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
FromJsonString(const std::string & jsonStr)30 void ViewData::FromJsonString(const std::string& jsonStr)
31 {
32 nlohmann::json jsonObject = nlohmann::json::parse(jsonStr, nullptr, false);
33 if (jsonObject.is_discarded()) {
34 ABILITYBASE_LOGE("Failed to parse json string.");
35 return;
36 }
37 if (jsonObject.contains(VIEW_DATA_BUNDLE_NAME) && jsonObject[VIEW_DATA_BUNDLE_NAME].is_string()) {
38 bundleName = jsonObject.at(VIEW_DATA_BUNDLE_NAME).get<std::string>();
39 }
40 if (jsonObject.contains(VIEW_DATA_MODULE_NAME) && jsonObject[VIEW_DATA_MODULE_NAME].is_string()) {
41 moduleName = jsonObject.at(VIEW_DATA_MODULE_NAME).get<std::string>();
42 }
43 if (jsonObject.contains(VIEW_DATA_ABILITY_NAME) && jsonObject[VIEW_DATA_ABILITY_NAME].is_string()) {
44 abilityName = jsonObject.at(VIEW_DATA_ABILITY_NAME).get<std::string>();
45 }
46 if (jsonObject.contains(VIEW_DATA_PAGE_URL) && jsonObject[VIEW_DATA_PAGE_URL].is_string()) {
47 pageUrl = jsonObject.at(VIEW_DATA_PAGE_URL).get<std::string>();
48 }
49 if (jsonObject.contains(VIEW_DATA_NODES) && jsonObject[VIEW_DATA_NODES].is_array()) {
50 nodes.clear();
51 auto size = jsonObject[VIEW_DATA_NODES].size() > NODES_SIZE_LIMIT ? NODES_SIZE_LIMIT :
52 jsonObject[VIEW_DATA_NODES].size();
53 for (size_t i = 0; i < size; i++) {
54 if (jsonObject[VIEW_DATA_NODES][i].is_string()) {
55 PageNodeInfo pageNodeInfo;
56 pageNodeInfo.FromJsonString(jsonObject[VIEW_DATA_NODES][i]);
57 nodes.emplace_back(pageNodeInfo);
58 }
59 }
60 }
61 }
62
ToJsonString() const63 std::string ViewData::ToJsonString() const
64 {
65 nlohmann::json jsonNodes = nlohmann::json::array();
66 auto size = nodes.size() > NODES_SIZE_LIMIT ? NODES_SIZE_LIMIT : nodes.size();
67 for (size_t i = 0; i < size; i++) {
68 jsonNodes.emplace_back(nodes[i].ToJsonString());
69 }
70 nlohmann::json jsonObject {
71 {VIEW_DATA_BUNDLE_NAME, bundleName},
72 {VIEW_DATA_MODULE_NAME, moduleName},
73 {VIEW_DATA_ABILITY_NAME, abilityName},
74 {VIEW_DATA_PAGE_URL, pageUrl},
75 {VIEW_DATA_NODES, jsonNodes}
76 };
77 return jsonObject.dump();
78 }
79 } // namespace AbilityBase
80 } // namespace OHOS