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 "int_serializer.h"
17
18 #include <string_ex.h>
19 #include "edm_constants.h"
20
21 namespace OHOS {
22 namespace EDM {
23
Deserialize(const std::string & jsonString,int32_t & dataObj)24 bool IntSerializer::Deserialize(const std::string &jsonString, int32_t &dataObj)
25 {
26 dataObj = 0;
27 if (jsonString.empty()) {
28 return true;
29 }
30 char* endptr;
31 errno = 0;
32 dataObj = strtol(jsonString.c_str(), &endptr, EdmConstants::DECIMAL);
33 if (errno == ERANGE || endptr == jsonString.c_str() || *endptr != '\0') {
34 return false;
35 }
36 return true;
37 }
38
Serialize(const int32_t & dataObj,std::string & jsonString)39 bool IntSerializer::Serialize(const int32_t &dataObj, std::string &jsonString)
40 {
41 jsonString = std::to_string(dataObj);
42 return true;
43 }
44
GetPolicy(MessageParcel & data,int32_t & result)45 bool IntSerializer::GetPolicy(MessageParcel &data, int32_t &result)
46 {
47 result = data.ReadInt32();
48 return true;
49 }
50
WritePolicy(MessageParcel & reply,int32_t & result)51 bool IntSerializer::WritePolicy(MessageParcel &reply, int32_t &result)
52 {
53 return reply.WriteInt32(result);
54 }
55
MergePolicy(std::vector<int32_t> & data,int32_t & result)56 bool IntSerializer::MergePolicy(std::vector<int32_t> &data, int32_t &result)
57 {
58 for (auto policy : data) {
59 if (policy != 0) {
60 result = policy;
61 }
62 }
63 return true;
64 }
65 } // namespace EDM
66 } // namespace OHOS