1 /*
2 * Copyright (c) 2021-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 "pre_install_bundle_info.h"
17
18 namespace OHOS {
19 namespace AppExecFwk {
20 namespace {
21 const std::string BUNDLE_NAME = "bundleName";
22 const std::string VERSION_CODE = "versionCode";
23 const std::string BUNDLE_PATHS = "bundlePaths";
24 const std::string APP_TYPE = "appType";
25 const std::string REMOVABLE = "removable";
26 } // namespace
27
ToJson(nlohmann::json & jsonObject) const28 void PreInstallBundleInfo::ToJson(nlohmann::json &jsonObject) const
29 {
30 jsonObject[BUNDLE_NAME] = bundleName_;
31 jsonObject[VERSION_CODE] = versionCode_;
32 jsonObject[BUNDLE_PATHS] = bundlePaths_;
33 jsonObject[APP_TYPE] = appType_;
34 jsonObject[REMOVABLE] = removable_;
35 }
36
FromJson(const nlohmann::json & jsonObject)37 int32_t PreInstallBundleInfo::FromJson(const nlohmann::json &jsonObject)
38 {
39 const auto &jsonObjectEnd = jsonObject.end();
40 int32_t parseResult = ERR_OK;
41 GetValueIfFindKey<std::string>(jsonObject,
42 jsonObjectEnd,
43 BUNDLE_NAME,
44 bundleName_,
45 JsonType::STRING,
46 true,
47 parseResult,
48 ArrayType::NOT_ARRAY);
49 GetValueIfFindKey<uint32_t>(jsonObject,
50 jsonObjectEnd,
51 VERSION_CODE,
52 versionCode_,
53 JsonType::NUMBER,
54 true,
55 parseResult,
56 ArrayType::NOT_ARRAY);
57 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
58 jsonObjectEnd,
59 BUNDLE_PATHS,
60 bundlePaths_,
61 JsonType::ARRAY,
62 true,
63 parseResult,
64 ArrayType::STRING);
65 GetValueIfFindKey<Constants::AppType>(jsonObject,
66 jsonObjectEnd,
67 APP_TYPE,
68 appType_,
69 JsonType::NUMBER,
70 false,
71 parseResult,
72 ArrayType::NOT_ARRAY);
73 GetValueIfFindKey<bool>(jsonObject,
74 jsonObjectEnd,
75 REMOVABLE,
76 removable_,
77 JsonType::BOOLEAN,
78 false,
79 parseResult,
80 ArrayType::NOT_ARRAY);
81 return parseResult;
82 }
83
ToString() const84 std::string PreInstallBundleInfo::ToString() const
85 {
86 nlohmann::json j;
87 j[BUNDLE_NAME] = bundleName_;
88 j[VERSION_CODE] = versionCode_;
89 j[BUNDLE_PATHS] = bundlePaths_;
90 j[APP_TYPE] = appType_;
91 j[REMOVABLE] = removable_;
92 return j.dump();
93 }
94 } // namespace AppExecFwk
95 } // namespace OHOS
96