• 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_BUNDLE_NAME = "bundleName";
25 const std::string INNER_BUNDLE_USER_INFO_INSTALL_TIME = "installTime";
26 const std::string INNER_BUNDLE_USER_INFO_UPDATE_TIME = "updateTime";
27 const std::string INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO = "bundleUserInfo";
28 } // namespace
29 
to_json(nlohmann::json & jsonObject,const InnerBundleUserInfo & innerBundleUserInfo)30 void to_json(nlohmann::json& jsonObject, const InnerBundleUserInfo& innerBundleUserInfo)
31 {
32     jsonObject = nlohmann::json {
33         {INNER_BUNDLE_USER_INFO_UID, innerBundleUserInfo.uid},
34         {INNER_BUNDLE_USER_INFO_GIDS, innerBundleUserInfo.gids},
35         {INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID, innerBundleUserInfo.accessTokenId},
36         {INNER_BUNDLE_USER_INFO_BUNDLE_NAME, innerBundleUserInfo.bundleName},
37         {INNER_BUNDLE_USER_INFO_INSTALL_TIME, innerBundleUserInfo.installTime},
38         {INNER_BUNDLE_USER_INFO_UPDATE_TIME, innerBundleUserInfo.updateTime},
39         {INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO, innerBundleUserInfo.bundleUserInfo},
40     };
41 }
42 
from_json(const nlohmann::json & jsonObject,InnerBundleUserInfo & innerBundleUserInfo)43 void from_json(const nlohmann::json& jsonObject, InnerBundleUserInfo& innerBundleUserInfo)
44 {
45     const auto &jsonObjectEnd = jsonObject.end();
46     int32_t parseResult = ERR_OK;
47     GetValueIfFindKey<int32_t>(jsonObject,
48         jsonObjectEnd,
49         INNER_BUNDLE_USER_INFO_UID,
50         innerBundleUserInfo.uid,
51         JsonType::NUMBER,
52         false,
53         parseResult,
54         ArrayType::NOT_ARRAY);
55     GetValueIfFindKey<std::vector<int32_t>>(jsonObject,
56         jsonObjectEnd,
57         INNER_BUNDLE_USER_INFO_GIDS,
58         innerBundleUserInfo.gids,
59         JsonType::ARRAY,
60         false,
61         parseResult,
62         ArrayType::NUMBER);
63     GetValueIfFindKey<uint32_t>(jsonObject,
64         jsonObjectEnd,
65         INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID,
66         innerBundleUserInfo.accessTokenId,
67         JsonType::NUMBER,
68         false,
69         parseResult,
70         ArrayType::NOT_ARRAY);
71     GetValueIfFindKey<std::string>(jsonObject,
72         jsonObjectEnd,
73         INNER_BUNDLE_USER_INFO_BUNDLE_NAME,
74         innerBundleUserInfo.bundleName,
75         JsonType::STRING,
76         false,
77         parseResult,
78         ArrayType::NOT_ARRAY);
79     GetValueIfFindKey<int64_t>(jsonObject,
80         jsonObjectEnd,
81         INNER_BUNDLE_USER_INFO_INSTALL_TIME,
82         innerBundleUserInfo.installTime,
83         JsonType::NUMBER,
84         false,
85         parseResult,
86         ArrayType::NOT_ARRAY);
87     GetValueIfFindKey<int64_t>(jsonObject,
88         jsonObjectEnd,
89         INNER_BUNDLE_USER_INFO_UPDATE_TIME,
90         innerBundleUserInfo.updateTime,
91         JsonType::NUMBER,
92         false,
93         parseResult,
94         ArrayType::NOT_ARRAY);
95     GetValueIfFindKey<BundleUserInfo>(jsonObject,
96         jsonObjectEnd,
97         INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO,
98         innerBundleUserInfo.bundleUserInfo,
99         JsonType::OBJECT,
100         false,
101         parseResult,
102         ArrayType::NOT_ARRAY);
103 }
104 } // namespace AppExecFwk
105 } // namespace OHOS