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