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 GetValueIfFindKey<std::string>(jsonObject,
83 jsonObjectEnd,
84 DATA_GROUP_ID,
85 dataGroupInfo.dataGroupId,
86 JsonType::STRING,
87 false,
88 parseResult,
89 ArrayType::NOT_ARRAY);
90 GetValueIfFindKey<std::string>(jsonObject,
91 jsonObjectEnd,
92 UUID,
93 dataGroupInfo.uuid,
94 JsonType::STRING,
95 false,
96 parseResult,
97 ArrayType::NOT_ARRAY);
98 GetValueIfFindKey<int32_t>(jsonObject,
99 jsonObjectEnd,
100 UID,
101 dataGroupInfo.uid,
102 JsonType::NUMBER,
103 false,
104 parseResult,
105 ArrayType::NOT_ARRAY);
106 GetValueIfFindKey<int32_t>(jsonObject,
107 jsonObjectEnd,
108 GID,
109 dataGroupInfo.gid,
110 JsonType::NUMBER,
111 false,
112 parseResult,
113 ArrayType::NOT_ARRAY);
114 GetValueIfFindKey<int32_t>(jsonObject,
115 jsonObjectEnd,
116 USER_ID,
117 dataGroupInfo.userId,
118 JsonType::NUMBER,
119 false,
120 parseResult,
121 ArrayType::NOT_ARRAY);
122 if (parseResult != ERR_OK) {
123 APP_LOGE("read dataGroupInfo error : %{public}d", parseResult);
124 }
125 }
126
ToString() const127 std::string DataGroupInfo::ToString() const
128 {
129 return "[ dataGroupId = " + dataGroupId
130 + ", uuid = " + uuid
131 + ", uid = " + std::to_string(uid)
132 + ", gid = " + std::to_string(gid)
133 + ", userId = " + std::to_string(userId)
134 + "]";
135 }
136 } // namespace AppExecFwk
137 } // namespace OHOS
138