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