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 "bool_serializer.h"
17
18 namespace OHOS {
19 namespace EDM {
Deserialize(const std::string & jsonString,bool & dataObj)20 bool BoolSerializer::Deserialize(const std::string &jsonString, bool &dataObj)
21 {
22 dataObj = false;
23 if (jsonString.empty()) {
24 return true;
25 }
26 constexpr std::size_t TRUE_STRING_SIZE = 4;
27 constexpr std::size_t FALSE_STRING_SIZE = 5;
28 if (jsonString.size() != TRUE_STRING_SIZE && jsonString.size() != FALSE_STRING_SIZE) {
29 return false;
30 }
31 std::string temp;
32 temp.resize(jsonString.size());
33 std::transform(jsonString.begin(), jsonString.end(), temp.begin(), ::tolower);
34 if (temp == TRUE_VALUE) {
35 dataObj = true;
36 return true;
37 }
38 if (temp == FALSE_VALUE) {
39 return true;
40 }
41 return false;
42 }
43
Serialize(const bool & dataObj,std::string & jsonString)44 bool BoolSerializer::Serialize(const bool &dataObj, std::string &jsonString)
45 {
46 jsonString = dataObj ? TRUE_VALUE : "";
47 return true;
48 }
49
GetPolicy(MessageParcel & data,bool & result)50 bool BoolSerializer::GetPolicy(MessageParcel &data, bool &result)
51 {
52 return data.ReadBool(result);
53 }
54
WritePolicy(MessageParcel & reply,bool & result)55 bool BoolSerializer::WritePolicy(MessageParcel &reply, bool &result)
56 {
57 return reply.WriteBool(result);
58 }
59
MergePolicy(std::vector<bool> & data,bool & result)60 bool BoolSerializer::MergePolicy(std::vector<bool> &data, bool &result)
61 {
62 result = std::any_of(data.begin(), data.end(), [](bool it) { return it; });
63 return true;
64 }
65 } // namespace EDM
66 } // namespace OHOS