• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef INPUT_METHOD_SERIALIZABLE_H
17 #define INPUT_METHOD_SERIALIZABLE_H
18 #include <string>
19 
20 #include "cJSON.h"
21 #include "global.h"
22 namespace OHOS {
23 namespace MiscServices {
24 #ifndef GET_NAME
25 #define GET_NAME(value) #value
26 #endif
27 struct Serializable {
28 public:
~SerializableSerializable29     virtual ~Serializable(){};
30     bool Unmarshall(const std::string &content);
31     bool Marshall(std::string &content) const;
UnmarshalSerializable32     virtual bool Unmarshal(cJSON *node)
33     {
34         return false;
35     }
MarshalSerializable36     virtual bool Marshal(cJSON *node) const
37     {
38         return false;
39     }
40     static bool GetValue(cJSON *node, const std::string &name, std::string &value);
41     static bool GetValue(cJSON *node, const std::string &name, int32_t &value);
42     static bool GetValue(cJSON *node, const std::string &name, uint32_t &value);
43     static bool GetValue(cJSON *node, const std::string &name, bool &value);
44     static bool GetValue(cJSON *node, const std::string &name, Serializable &value);
45     static bool GetValue(cJSON *node, const std::string &name, std::vector<std::vector<std::string>> &values);
46     template<typename T>
47     static bool GetValue(cJSON *node, const std::string &name, std::vector<T> &values, int32_t maxNum = 0)
48     {
49         auto subNode = GetSubNode(node, name);
50         if (!cJSON_IsArray(subNode)) {
51             IMSA_HILOGE("not array");
52             return false;
53         }
54         auto size = cJSON_GetArraySize(subNode);
55         IMSA_HILOGD("size:%{public}d, maxNum:%{public}d", size, maxNum);
56         if (maxNum > 0 && size > maxNum) {
57             size = maxNum;
58         }
59         values.resize(size);
60         bool ret = true;
61         for (int32_t i = 0; i < size; i++) {
62             auto item = cJSON_GetArrayItem(subNode, i);
63             if (item == NULL) {
64                 return false;
65             }
66             ret = GetValue(item, "", values[i]) && ret;
67         }
68         return ret;
69     }
70     static bool SetValue(cJSON *node, const std::string &name, const std::string &value);
71     static bool SetValue(cJSON *node, const std::string &name, const int32_t &value);
72     static bool SetValue(cJSON *node, const std::string &name, const bool &value);
73     static bool SetValue(cJSON *node, const std::string &name, const Serializable &value);
74     static bool SetValue(cJSON *node, const std::string &name, const std::vector<std::vector<std::string>> &values);
75     static bool SetValue(cJSON *node, const std::string &name, const std::vector<std::string> &values);
SetValueSerializable76     template<typename T> static bool SetValue(cJSON *node, const std::string &name, const std::vector<T> &values)
77     {
78         auto array = cJSON_CreateArray();
79         for (const auto &value : values) {
80             auto *item = cJSON_CreateObject();
81             auto ret = value.Marshal(item);
82             if (!ret || !cJSON_AddItemToArray(array, item)) {
83                 cJSON_Delete(item);
84             }
85         }
86         auto ret = cJSON_AddItemToObject(node, name.c_str(), array);
87         if (!ret) {
88             cJSON_Delete(array);
89         }
90         return ret;
91     }
92     static cJSON *GetSubNode(cJSON *node, const std::string &name);
93 };
94 } // namespace MiscServices
95 } // namespace OHOS
96 #endif // INPUT_METHOD_SERIALIZABLE_H
97