122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci
1622736c2fSopenharmony_ci#ifndef INPUT_METHOD_SERIALIZABLE_H
1722736c2fSopenharmony_ci#define INPUT_METHOD_SERIALIZABLE_H
1822736c2fSopenharmony_ci#include <string>
1922736c2fSopenharmony_ci#include <vector>
2022736c2fSopenharmony_ci
2122736c2fSopenharmony_ci#include "cJSON.h"
2222736c2fSopenharmony_ci#include "global.h"
2322736c2fSopenharmony_cinamespace OHOS {
2422736c2fSopenharmony_cinamespace MiscServices {
2522736c2fSopenharmony_ci#ifndef GET_NAME
2622736c2fSopenharmony_ci#define GET_NAME(value) #value
2722736c2fSopenharmony_ci#endif
2822736c2fSopenharmony_cistruct Serializable {
2922736c2fSopenharmony_cipublic:
3022736c2fSopenharmony_ci    virtual ~Serializable(){};
3122736c2fSopenharmony_ci    bool Unmarshall(const std::string &content);
3222736c2fSopenharmony_ci    bool Marshall(std::string &content) const;
3322736c2fSopenharmony_ci    virtual bool Unmarshal(cJSON *node) = 0;
3422736c2fSopenharmony_ci    virtual bool Marshal(cJSON *node) const
3522736c2fSopenharmony_ci    {
3622736c2fSopenharmony_ci        return false;
3722736c2fSopenharmony_ci    }
3822736c2fSopenharmony_ci    static bool GetValue(cJSON *node, const std::string &name, std::string &value);
3922736c2fSopenharmony_ci    static bool GetValue(cJSON *node, const std::string &name, int32_t &value);
4022736c2fSopenharmony_ci    static bool GetValue(cJSON *node, const std::string &name, uint32_t &value);
4122736c2fSopenharmony_ci    static bool GetValue(cJSON *node, const std::string &name, bool &value);
4222736c2fSopenharmony_ci    static bool GetValue(cJSON *node, const std::string &name, Serializable &value);
4322736c2fSopenharmony_ci    template<typename T>
4422736c2fSopenharmony_ci    static bool GetValue(cJSON *node, const std::string &name, std::vector<T> &values, int32_t maxNum = 0)
4522736c2fSopenharmony_ci    {
4622736c2fSopenharmony_ci        auto subNode = GetSubNode(node, name);
4722736c2fSopenharmony_ci        if (!cJSON_IsArray(subNode)) {
4822736c2fSopenharmony_ci            IMSA_HILOGE("not array");
4922736c2fSopenharmony_ci            return false;
5022736c2fSopenharmony_ci        }
5122736c2fSopenharmony_ci        auto size = cJSON_GetArraySize(subNode);
5222736c2fSopenharmony_ci        IMSA_HILOGD("size:%{public}d, maxNum:%{public}d", size, maxNum);
5322736c2fSopenharmony_ci        if (maxNum > 0 && size > maxNum) {
5422736c2fSopenharmony_ci            size = maxNum;
5522736c2fSopenharmony_ci        }
5622736c2fSopenharmony_ci        values.resize(size);
5722736c2fSopenharmony_ci        bool ret = true;
5822736c2fSopenharmony_ci        for (int32_t i = 0; i < size; i++) {
5922736c2fSopenharmony_ci            auto item = cJSON_GetArrayItem(subNode, i);
6022736c2fSopenharmony_ci            if (item == NULL) {
6122736c2fSopenharmony_ci                return false;
6222736c2fSopenharmony_ci            }
6322736c2fSopenharmony_ci            ret = GetValue(item, "", values[i]) && ret;
6422736c2fSopenharmony_ci        }
6522736c2fSopenharmony_ci        return ret;
6622736c2fSopenharmony_ci    }
6722736c2fSopenharmony_ci    static bool SetValue(cJSON *node, const std::string &name, const std::string &value);
6822736c2fSopenharmony_ci    static bool SetValue(cJSON *node, const std::string &name, const int32_t &value);
6922736c2fSopenharmony_ci    template<typename T> static bool SetValue(cJSON *node, const std::string &name, const std::vector<T> &values)
7022736c2fSopenharmony_ci    {
7122736c2fSopenharmony_ci        auto array = cJSON_CreateArray();
7222736c2fSopenharmony_ci        for (const auto &value : values) {
7322736c2fSopenharmony_ci            auto *item = cJSON_CreateObject();
7422736c2fSopenharmony_ci            auto ret = value.Marshal(item);
7522736c2fSopenharmony_ci            if (!ret || !cJSON_AddItemToArray(array, item)) {
7622736c2fSopenharmony_ci                cJSON_Delete(item);
7722736c2fSopenharmony_ci            }
7822736c2fSopenharmony_ci        }
7922736c2fSopenharmony_ci        auto ret = cJSON_AddItemToObject(node, name.c_str(), array);
8022736c2fSopenharmony_ci        if (!ret) {
8122736c2fSopenharmony_ci            cJSON_Delete(array);
8222736c2fSopenharmony_ci        }
8322736c2fSopenharmony_ci        return ret;
8422736c2fSopenharmony_ci    }
8522736c2fSopenharmony_ci
8622736c2fSopenharmony_ciprivate:
8722736c2fSopenharmony_ci    static cJSON *GetSubNode(cJSON *node, const std::string &name);
8822736c2fSopenharmony_ci};
8922736c2fSopenharmony_ci} // namespace MiscServices
9022736c2fSopenharmony_ci} // namespace OHOS
9122736c2fSopenharmony_ci#endif // INPUT_METHOD_SERIALIZABLE_H
92