1 /*
2 * Copyright (c) 2025 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 "uint_serializer.h"
17
18 #include <charconv>
19 #include <string_ex.h>
20 #include "edm_constants.h"
21
22 namespace OHOS {
23 namespace EDM {
24
Deserialize(const std::string & jsonString,uint32_t & dataObj)25 bool UIntSerializer::Deserialize(const std::string &jsonString, uint32_t &dataObj)
26 {
27 dataObj = 0;
28 if (jsonString.empty()) {
29 return true;
30 }
31 const char* begin = jsonString.data();
32 const char* end = begin + jsonString.size();
33 auto [ptr, ec] = std::from_chars(begin, end, dataObj);
34 return (ec == std::errc() && ptr == end);
35 }
36
Serialize(const uint32_t & dataObj,std::string & jsonString)37 bool UIntSerializer::Serialize(const uint32_t &dataObj, std::string &jsonString)
38 {
39 jsonString = std::to_string(dataObj);
40 return true;
41 }
42
GetPolicy(MessageParcel & data,uint32_t & result)43 bool UIntSerializer::GetPolicy(MessageParcel &data, uint32_t &result)
44 {
45 result = data.ReadUint32();
46 return true;
47 }
48
WritePolicy(MessageParcel & reply,uint32_t & result)49 bool UIntSerializer::WritePolicy(MessageParcel &reply, uint32_t &result)
50 {
51 return reply.WriteUint32(result);
52 }
53
MergePolicy(std::vector<uint32_t> & data,uint32_t & result)54 bool UIntSerializer::MergePolicy(std::vector<uint32_t> &data, uint32_t &result)
55 {
56 for (auto policy : data) {
57 if (policy != 0) {
58 result = policy;
59 }
60 }
61 return true;
62 }
63 } // namespace EDM
64 } // namespace OHOS