1 /*
2 * Copyright (c) 2025 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 "overlay_bundle_info.h"
17
18 #include "bundle_constants.h"
19 #include "hilog_tag_wrapper.h"
20 #include "json_util.h"
21 #include "nlohmann/json.hpp"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const char* BUNDLE_OVERLAY_BUNDLE_NAME = "bundleName";
28 const char* BUNDLE_OVERLAY_BUNDLE_DIR = "bundleDir";
29 const char* BUNDLE_OVERLAY_BUNDLE_STATE = "state";
30 const char* BUNDLE_OVERLAY_BUNDLE_PRIORITY = "priority";
31 } // namespace
32
to_json(nlohmann::json & jsonObject,const OverlayBundleInfo & overlayBundleInfo)33 void to_json(nlohmann::json &jsonObject, const OverlayBundleInfo &overlayBundleInfo)
34 {
35 jsonObject = nlohmann::json {
36 {BUNDLE_OVERLAY_BUNDLE_NAME, overlayBundleInfo.bundleName},
37 {BUNDLE_OVERLAY_BUNDLE_DIR, overlayBundleInfo.bundleDir},
38 {BUNDLE_OVERLAY_BUNDLE_STATE, overlayBundleInfo.state},
39 {BUNDLE_OVERLAY_BUNDLE_PRIORITY, overlayBundleInfo.priority}
40 };
41 }
42
from_json(const nlohmann::json & jsonObject,OverlayBundleInfo & overlayBundleInfo)43 void from_json(const nlohmann::json &jsonObject, OverlayBundleInfo &overlayBundleInfo)
44 {
45 const auto &jsonObjectEnd = jsonObject.end();
46 int32_t parseResult = ERR_OK;
47 GetValueIfFindKey<std::string>(jsonObject,
48 jsonObjectEnd,
49 BUNDLE_OVERLAY_BUNDLE_NAME,
50 overlayBundleInfo.bundleName,
51 JsonType::STRING,
52 true,
53 parseResult,
54 ArrayType::NOT_ARRAY);
55 GetValueIfFindKey<std::string>(jsonObject,
56 jsonObjectEnd,
57 BUNDLE_OVERLAY_BUNDLE_DIR,
58 overlayBundleInfo.bundleDir,
59 JsonType::STRING,
60 true,
61 parseResult,
62 ArrayType::NOT_ARRAY);
63 GetValueIfFindKey<int32_t>(jsonObject,
64 jsonObjectEnd,
65 BUNDLE_OVERLAY_BUNDLE_STATE,
66 overlayBundleInfo.state,
67 JsonType::NUMBER,
68 true,
69 parseResult,
70 ArrayType::NOT_ARRAY);
71 GetValueIfFindKey<int32_t>(jsonObject,
72 jsonObjectEnd,
73 BUNDLE_OVERLAY_BUNDLE_PRIORITY,
74 overlayBundleInfo.priority,
75 JsonType::NUMBER,
76 true,
77 parseResult,
78 ArrayType::NOT_ARRAY);
79 if (parseResult != ERR_OK) {
80 TAG_LOGE(AAFwkTag::ABILITY_SIM, "overlayBundleInfo from_json error : %{public}d", parseResult);
81 }
82 }
83 } // AppExecFwk
84 } // OHOS
85