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