199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License.
599552fe9Sopenharmony_ci * You may obtain a copy of the License at
699552fe9Sopenharmony_ci *
799552fe9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899552fe9Sopenharmony_ci *
999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and
1399552fe9Sopenharmony_ci * limitations under the License.
1499552fe9Sopenharmony_ci */
1599552fe9Sopenharmony_ci
1699552fe9Sopenharmony_ci#ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_JSON_UTILS_H
1799552fe9Sopenharmony_ci#define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_JSON_UTILS_H
1899552fe9Sopenharmony_ci
1999552fe9Sopenharmony_ci#include <string>
2099552fe9Sopenharmony_ci
2199552fe9Sopenharmony_ci#include "nlohmann/json.hpp"
2299552fe9Sopenharmony_ci#include "standby_service_errors.h"
2399552fe9Sopenharmony_ci
2499552fe9Sopenharmony_cinamespace OHOS {
2599552fe9Sopenharmony_cinamespace DevStandbyMgr {
2699552fe9Sopenharmony_ciclass JsonUtils {
2799552fe9Sopenharmony_cipublic:
2899552fe9Sopenharmony_ci    JsonUtils() = delete;
2999552fe9Sopenharmony_ci
3099552fe9Sopenharmony_ci    /**
3199552fe9Sopenharmony_ci     * @brief load json value from file
3299552fe9Sopenharmony_ci     *
3399552fe9Sopenharmony_ci     * @param jsonValue json value from the file content
3499552fe9Sopenharmony_ci     * @param filePath the absolute file path
3599552fe9Sopenharmony_ci     * @return true true if succeed
3699552fe9Sopenharmony_ci     */
3799552fe9Sopenharmony_ci    static bool LoadJsonValueFromFile(nlohmann::json& jsonValue, const std::string& filePath);
3899552fe9Sopenharmony_ci
3999552fe9Sopenharmony_ci    /**
4099552fe9Sopenharmony_ci     * @brief load json value from string
4199552fe9Sopenharmony_ci     *
4299552fe9Sopenharmony_ci     * @param jsonValue json value from the string
4399552fe9Sopenharmony_ci     * @param content string content
4499552fe9Sopenharmony_ci     * @return true true if succeed
4599552fe9Sopenharmony_ci     */
4699552fe9Sopenharmony_ci    static bool LoadJsonValueFromContent(nlohmann::json& jsonValue, const std::string& content);
4799552fe9Sopenharmony_ci
4899552fe9Sopenharmony_ci    /**
4999552fe9Sopenharmony_ci     * @brief dump json value to file
5099552fe9Sopenharmony_ci     *
5199552fe9Sopenharmony_ci     * @param jsonValue json value to be dumped to the file
5299552fe9Sopenharmony_ci     * @param filePath the absolute file path to dump json value
5399552fe9Sopenharmony_ci     * @return true true true if succeed
5499552fe9Sopenharmony_ci     */
5599552fe9Sopenharmony_ci    static bool DumpJsonValueToFile(const nlohmann::json& jsonValue, const std::string& filePath);
5699552fe9Sopenharmony_ci
5799552fe9Sopenharmony_ci    /**
5899552fe9Sopenharmony_ci     * @brief get the 32-bit int from json value object
5999552fe9Sopenharmony_ci     *
6099552fe9Sopenharmony_ci     * @param jsonValue json object
6199552fe9Sopenharmony_ci     * @param key the name of int object
6299552fe9Sopenharmony_ci     * @param value value of the int object
6399552fe9Sopenharmony_ci     * @return true true if succeed
6499552fe9Sopenharmony_ci     */
6599552fe9Sopenharmony_ci    static bool GetInt32FromJsonValue(const nlohmann::json& jsonValue, const std::string& key, int32_t& value);
6699552fe9Sopenharmony_ci
6799552fe9Sopenharmony_ci    /**
6899552fe9Sopenharmony_ci     * @brief get the bool from json value object
6999552fe9Sopenharmony_ci     *
7099552fe9Sopenharmony_ci     * @param jsonValue json object
7199552fe9Sopenharmony_ci     * @param key the name of bool object
7299552fe9Sopenharmony_ci     * @param value value of the bool object
7399552fe9Sopenharmony_ci     * @return true if succeed
7499552fe9Sopenharmony_ci     */
7599552fe9Sopenharmony_ci    static bool GetBoolFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, bool& value);
7699552fe9Sopenharmony_ci
7799552fe9Sopenharmony_ci    /**
7899552fe9Sopenharmony_ci     * @brief get the string from json value object
7999552fe9Sopenharmony_ci     *
8099552fe9Sopenharmony_ci     * @param jsonValue json object
8199552fe9Sopenharmony_ci     * @param key the name of string object
8299552fe9Sopenharmony_ci     * @param value value of the string object
8399552fe9Sopenharmony_ci     * @return true if succeed
8499552fe9Sopenharmony_ci     */
8599552fe9Sopenharmony_ci    static bool GetStringFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, std::string& value);
8699552fe9Sopenharmony_ci
8799552fe9Sopenharmony_ci    /**
8899552fe9Sopenharmony_ci     * @brief Get the Obj from json value object
8999552fe9Sopenharmony_ci     *
9099552fe9Sopenharmony_ci     * @param jsonValue json object
9199552fe9Sopenharmony_ci     * @param key the name of json object
9299552fe9Sopenharmony_ci     * @param value c
9399552fe9Sopenharmony_ci     * @return true if succeed
9499552fe9Sopenharmony_ci     */
9599552fe9Sopenharmony_ci    static bool GetObjFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value);
9699552fe9Sopenharmony_ci
9799552fe9Sopenharmony_ci    /**
9899552fe9Sopenharmony_ci     * @brief Get the Array from json value object
9999552fe9Sopenharmony_ci     *
10099552fe9Sopenharmony_ci     * @param jsonValue json object
10199552fe9Sopenharmony_ci     * @param key the name of array object
10299552fe9Sopenharmony_ci     * @param value value of array object
10399552fe9Sopenharmony_ci     * @return true if succeed
10499552fe9Sopenharmony_ci     */
10599552fe9Sopenharmony_ci    static bool GetArrayFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value);
10699552fe9Sopenharmony_ci
10799552fe9Sopenharmony_ci    /**
10899552fe9Sopenharmony_ci     * @brief Get the String Array from json value object
10999552fe9Sopenharmony_ci     *
11099552fe9Sopenharmony_ci     * @param jsonValue json object
11199552fe9Sopenharmony_ci     * @param key the name of string array object
11299552fe9Sopenharmony_ci     * @param strArray string array
11399552fe9Sopenharmony_ci     * @return true if succeed
11499552fe9Sopenharmony_ci     */
11599552fe9Sopenharmony_ci    static bool GetStrArrFromJsonValue(const nlohmann::json& jsonValue, const std::string& key,
11699552fe9Sopenharmony_ci        std::vector<std::string>& strArray);
11799552fe9Sopenharmony_ci    static bool GetRealPath(const std::string& partialPath, std::string& fullPath);
11899552fe9Sopenharmony_ci    static std::vector<std::string> SplitVersion(const std::string& versionStr, char versionDelim);
11999552fe9Sopenharmony_ciprivate:
12099552fe9Sopenharmony_ci    static bool CreateNodeFile(const std::string &filePath);
12199552fe9Sopenharmony_ci    static bool GetFileContent(const std::string& filePath, std::string& content);
12299552fe9Sopenharmony_ci};
12399552fe9Sopenharmony_ci}  // namespace DevStandbyMgr
12499552fe9Sopenharmony_ci}  // namespace OHOS
12599552fe9Sopenharmony_ci#endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_JSON_UTILS_H
126