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