1 /*
2  * Copyright (c) 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 "inner_bundle_user_info.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
20 namespace {
21 const std::string INNER_BUNDLE_USER_INFO_UID = "uid";
22 const std::string INNER_BUNDLE_USER_INFO_GIDS = "gids";
23 const std::string INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID = "accessTokenId";
24 const std::string INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX = "accessTokenIdEx";
25 const std::string INNER_BUNDLE_USER_INFO_BUNDLE_NAME = "bundleName";
26 const std::string INNER_BUNDLE_USER_INFO_INSTALL_TIME = "installTime";
27 const std::string INNER_BUNDLE_USER_INFO_UPDATE_TIME = "updateTime";
28 const std::string INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO = "bundleUserInfo";
29 const std::string INNER_BUNDLE_USER_INFO_IS_REMOVABLE = "isRemovable";
30 const std::string INNER_BUNDLE_USER_INFO_CLONE_INFOS = "cloneInfos";
31 const std::string INNER_BUNDLE_USER_INFO_KEYID = "keyId";
32 } // namespace
33 
to_json(nlohmann::json & jsonObject,const InnerBundleUserInfo & innerBundleUserInfo)34 void to_json(nlohmann::json& jsonObject, const InnerBundleUserInfo& innerBundleUserInfo)
35 {
36     jsonObject = nlohmann::json {
37         {INNER_BUNDLE_USER_INFO_UID, innerBundleUserInfo.uid},
38         {INNER_BUNDLE_USER_INFO_GIDS, innerBundleUserInfo.gids},
39         {INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID, innerBundleUserInfo.accessTokenId},
40         {INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX, innerBundleUserInfo.accessTokenIdEx},
41         {INNER_BUNDLE_USER_INFO_BUNDLE_NAME, innerBundleUserInfo.bundleName},
42         {INNER_BUNDLE_USER_INFO_INSTALL_TIME, innerBundleUserInfo.installTime},
43         {INNER_BUNDLE_USER_INFO_UPDATE_TIME, innerBundleUserInfo.updateTime},
44         {INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO, innerBundleUserInfo.bundleUserInfo},
45         {INNER_BUNDLE_USER_INFO_IS_REMOVABLE, innerBundleUserInfo.isRemovable},
46         {INNER_BUNDLE_USER_INFO_CLONE_INFOS, innerBundleUserInfo.cloneInfos},
47         {INNER_BUNDLE_USER_INFO_KEYID, innerBundleUserInfo.keyId},
48     };
49 }
50 
from_json(const nlohmann::json & jsonObject,InnerBundleUserInfo & innerBundleUserInfo)51 void from_json(const nlohmann::json& jsonObject, InnerBundleUserInfo& innerBundleUserInfo)
52 {
53     const auto &jsonObjectEnd = jsonObject.end();
54     int32_t parseResult = ERR_OK;
55     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_UID,
56         innerBundleUserInfo.uid, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
57     GetValueIfFindKey<std::vector<int32_t>>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_GIDS,
58         innerBundleUserInfo.gids, JsonType::ARRAY, false, parseResult, ArrayType::NUMBER);
59     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID,
60         innerBundleUserInfo.accessTokenId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
61     GetValueIfFindKey<uint64_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX,
62         innerBundleUserInfo.accessTokenIdEx, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
63     GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_BUNDLE_NAME,
64         innerBundleUserInfo.bundleName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
65     GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_INSTALL_TIME,
66         innerBundleUserInfo.installTime, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
67     GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_UPDATE_TIME,
68         innerBundleUserInfo.updateTime, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
69     GetValueIfFindKey<BundleUserInfo>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO,
70         innerBundleUserInfo.bundleUserInfo, JsonType::OBJECT, false, parseResult, ArrayType::NOT_ARRAY);
71     GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_IS_REMOVABLE,
72         innerBundleUserInfo.isRemovable, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
73     GetValueIfFindKey<std::map<std::string, InnerBundleCloneInfo>>(jsonObject, jsonObjectEnd,
74         INNER_BUNDLE_USER_INFO_CLONE_INFOS,
75         innerBundleUserInfo.cloneInfos, JsonType::OBJECT, false, parseResult, ArrayType::NOT_ARRAY);
76     GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_KEYID,
77         innerBundleUserInfo.keyId, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
78 }
79 } // namespace AppExecFwk
80 } // namespace OHOS