• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "data_group_info.h"
17 
18 #include "app_log_wrapper.h"
19 #include "json_util.h"
20 #include "nlohmann/json.hpp"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const char* DATA_GROUP_ID = "dataGroupId";
28 const char* UUID = "uuid";
29 const char* UID = "uid";
30 const char* GID = "gid";
31 const char* USER_ID = "userId";
32 }  // namespace
33 
ReadFromParcel(Parcel & parcel)34 bool DataGroupInfo::ReadFromParcel(Parcel &parcel)
35 {
36     dataGroupId = Str16ToStr8(parcel.ReadString16());
37     uuid = Str16ToStr8(parcel.ReadString16());
38     uid = parcel.ReadInt32();
39     gid = parcel.ReadInt32();
40     userId = parcel.ReadInt32();
41 
42     return true;
43 }
44 
Marshalling(Parcel & parcel) const45 bool DataGroupInfo::Marshalling(Parcel &parcel) const
46 {
47     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(dataGroupId));
48     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(uuid));
49     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, uid);
50     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, gid);
51     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
52 
53     return true;
54 }
55 
Unmarshalling(Parcel & parcel)56 DataGroupInfo *DataGroupInfo::Unmarshalling(Parcel &parcel)
57 {
58     DataGroupInfo *info = new (std::nothrow) DataGroupInfo();
59     if (info && !info->ReadFromParcel(parcel)) {
60         APP_LOGW("read from parcel failed");
61         delete info;
62         info = nullptr;
63     }
64     return info;
65 }
66 
to_json(nlohmann::json & jsonObject,const DataGroupInfo & dataGroupInfo)67 void to_json(nlohmann::json &jsonObject, const DataGroupInfo &dataGroupInfo)
68 {
69     jsonObject = nlohmann::json {
70         {DATA_GROUP_ID, dataGroupInfo.dataGroupId},
71         {UUID, dataGroupInfo.uuid},
72         {UID, dataGroupInfo.uid},
73         {GID, dataGroupInfo.gid},
74         {USER_ID, dataGroupInfo.userId}
75     };
76 }
77 
from_json(const nlohmann::json & jsonObject,DataGroupInfo & dataGroupInfo)78 void from_json(const nlohmann::json &jsonObject, DataGroupInfo &dataGroupInfo)
79 {
80     const auto &jsonObjectEnd = jsonObject.end();
81     int32_t parseResult = ERR_OK;
82     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
83         jsonObjectEnd,
84         DATA_GROUP_ID,
85         dataGroupInfo.dataGroupId,
86         false,
87         parseResult);
88     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
89         jsonObjectEnd,
90         UUID,
91         dataGroupInfo.uuid,
92         false,
93         parseResult);
94     GetValueIfFindKey<int32_t>(jsonObject,
95         jsonObjectEnd,
96         UID,
97         dataGroupInfo.uid,
98         JsonType::NUMBER,
99         false,
100         parseResult,
101         ArrayType::NOT_ARRAY);
102     GetValueIfFindKey<int32_t>(jsonObject,
103         jsonObjectEnd,
104         GID,
105         dataGroupInfo.gid,
106         JsonType::NUMBER,
107         false,
108         parseResult,
109         ArrayType::NOT_ARRAY);
110     GetValueIfFindKey<int32_t>(jsonObject,
111         jsonObjectEnd,
112         USER_ID,
113         dataGroupInfo.userId,
114         JsonType::NUMBER,
115         false,
116         parseResult,
117         ArrayType::NOT_ARRAY);
118     if (parseResult != ERR_OK) {
119         APP_LOGE("read dataGroupInfo error : %{public}d", parseResult);
120     }
121 }
122 
ToString() const123 std::string DataGroupInfo::ToString() const
124 {
125     return "[ dataGroupId = " + dataGroupId
126             + ", uuid = " + uuid
127             + ", uid = " + std::to_string(uid)
128             + ", gid = " + std::to_string(gid)
129             + ", userId = " + std::to_string(userId)
130             + "]";
131 }
132 }  // namespace AppExecFwk
133 }  // namespace OHOS
134