• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 } // namespace
31 
to_json(nlohmann::json & jsonObject,const InnerBundleUserInfo & innerBundleUserInfo)32 void to_json(nlohmann::json& jsonObject, const InnerBundleUserInfo& innerBundleUserInfo)
33 {
34     jsonObject = nlohmann::json {
35         {INNER_BUNDLE_USER_INFO_UID, innerBundleUserInfo.uid},
36         {INNER_BUNDLE_USER_INFO_GIDS, innerBundleUserInfo.gids},
37         {INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID, innerBundleUserInfo.accessTokenId},
38         {INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX, innerBundleUserInfo.accessTokenIdEx},
39         {INNER_BUNDLE_USER_INFO_BUNDLE_NAME, innerBundleUserInfo.bundleName},
40         {INNER_BUNDLE_USER_INFO_INSTALL_TIME, innerBundleUserInfo.installTime},
41         {INNER_BUNDLE_USER_INFO_UPDATE_TIME, innerBundleUserInfo.updateTime},
42         {INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO, innerBundleUserInfo.bundleUserInfo},
43         {INNER_BUNDLE_USER_INFO_IS_REMOVABLE, innerBundleUserInfo.isRemovable},
44     };
45 }
46 
from_json(const nlohmann::json & jsonObject,InnerBundleUserInfo & innerBundleUserInfo)47 void from_json(const nlohmann::json& jsonObject, InnerBundleUserInfo& innerBundleUserInfo)
48 {
49     const auto &jsonObjectEnd = jsonObject.end();
50     int32_t parseResult = ERR_OK;
51     GetValueIfFindKey<int32_t>(jsonObject,
52         jsonObjectEnd,
53         INNER_BUNDLE_USER_INFO_UID,
54         innerBundleUserInfo.uid,
55         JsonType::NUMBER,
56         false,
57         parseResult,
58         ArrayType::NOT_ARRAY);
59     GetValueIfFindKey<std::vector<int32_t>>(jsonObject,
60         jsonObjectEnd,
61         INNER_BUNDLE_USER_INFO_GIDS,
62         innerBundleUserInfo.gids,
63         JsonType::ARRAY,
64         false,
65         parseResult,
66         ArrayType::NUMBER);
67     GetValueIfFindKey<uint32_t>(jsonObject,
68         jsonObjectEnd,
69         INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID,
70         innerBundleUserInfo.accessTokenId,
71         JsonType::NUMBER,
72         false,
73         parseResult,
74         ArrayType::NOT_ARRAY);
75     GetValueIfFindKey<uint64_t>(jsonObject,
76         jsonObjectEnd,
77         INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX,
78         innerBundleUserInfo.accessTokenIdEx,
79         JsonType::NUMBER,
80         false,
81         parseResult,
82         ArrayType::NOT_ARRAY);
83     GetValueIfFindKey<std::string>(jsonObject,
84         jsonObjectEnd,
85         INNER_BUNDLE_USER_INFO_BUNDLE_NAME,
86         innerBundleUserInfo.bundleName,
87         JsonType::STRING,
88         false,
89         parseResult,
90         ArrayType::NOT_ARRAY);
91     GetValueIfFindKey<int64_t>(jsonObject,
92         jsonObjectEnd,
93         INNER_BUNDLE_USER_INFO_INSTALL_TIME,
94         innerBundleUserInfo.installTime,
95         JsonType::NUMBER,
96         false,
97         parseResult,
98         ArrayType::NOT_ARRAY);
99     GetValueIfFindKey<int64_t>(jsonObject,
100         jsonObjectEnd,
101         INNER_BUNDLE_USER_INFO_UPDATE_TIME,
102         innerBundleUserInfo.updateTime,
103         JsonType::NUMBER,
104         false,
105         parseResult,
106         ArrayType::NOT_ARRAY);
107     GetValueIfFindKey<BundleUserInfo>(jsonObject,
108         jsonObjectEnd,
109         INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO,
110         innerBundleUserInfo.bundleUserInfo,
111         JsonType::OBJECT,
112         false,
113         parseResult,
114         ArrayType::NOT_ARRAY);
115     GetValueIfFindKey<bool>(jsonObject,
116         jsonObjectEnd,
117         INNER_BUNDLE_USER_INFO_IS_REMOVABLE,
118         innerBundleUserInfo.isRemovable,
119         JsonType::BOOLEAN,
120         false,
121         parseResult,
122         ArrayType::NOT_ARRAY);
123 }
124 } // namespace AppExecFwk
125 } // namespace OHOS