• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_JSON_CONVERT_H
16 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_JSON_CONVERT_H
17 
18 #include <string>
19 #include "ans_log_wrapper.h"
20 #include "nlohmann/json.hpp"
21 
22 namespace OHOS {
23 namespace Notification {
24 class NotificationJsonConvertionBase {
25 public:
26     virtual ~NotificationJsonConvertionBase() = default;
27 
28     /**
29      * @brief Converts NotificationJsonConvertionBase object to json object.
30      *
31      * @param jsonObject Indicates the json object.
32      */
33     virtual bool ToJson(nlohmann::json &jsonObject) const = 0;
34 };
35 
36 class NotificationJsonConverter {
37 public:
38     /**
39      * @brief Converts NotificationJsonConvertionBase object to json object.
40      *
41      * @param convertionBase Indicates the NotificationJsonConvertionBase object.
42      * @param jsonObject Indicates the json object.
43      * @return Returns true if the conversion is successful; returns false otherwise.
44      */
ConvertToJson(const NotificationJsonConvertionBase * convertionBase,nlohmann::json & jsonObject)45     static bool ConvertToJson(const NotificationJsonConvertionBase *convertionBase, nlohmann::json &jsonObject)
46     {
47         if (convertionBase == nullptr) {
48             ANS_LOGE("Converter : Invalid base object");
49             return false;
50         }
51 
52         return convertionBase->ToJson(jsonObject);
53     }
54 
55     /**
56      * @brief Converts NotificationJsonConvertionBase object to json string.
57      *
58      * @param convertionBase Indicates the NotificationJsonConvertionBase object.
59      * @param jsonString Indicates the json string.
60      * @return Returns true if the conversion is successful; returns false otherwise.
61      */
ConvertToJsonString(const NotificationJsonConvertionBase * convertionBase,std::string & jsonString)62     static bool ConvertToJsonString(const NotificationJsonConvertionBase *convertionBase, std::string &jsonString)
63     {
64         if (convertionBase == nullptr) {
65             ANS_LOGE("Converter : Invalid base object");
66             return false;
67         }
68 
69         nlohmann::json jsonObject;
70         if (!convertionBase->ToJson(jsonObject)) {
71             ANS_LOGE("Converter : Cannot convert to JSON object");
72             return false;
73         }
74         jsonString = jsonObject.dump();
75 
76         return true;
77     }
78 
79     /**
80      * @brief Converts json object to a subclass object whose base class is NotificationJsonConvertionBase.
81      *
82      * @param jsonObject Indicates the json object.
83      * @return Returns the subclass object.
84      */
85     template <typename T>
ConvertFromJson(const nlohmann::json & jsonObject)86     static T *ConvertFromJson(const nlohmann::json &jsonObject)
87     {
88         if (jsonObject.is_null() or !jsonObject.is_object()) {
89             ANS_LOGE("Converter : Invalid JSON object");
90             return nullptr;
91         }
92 
93         return T::FromJson(jsonObject);
94     }
95 
96     /**
97      * @brief Converts json string to a subclass object whose base class is NotificationJsonConvertionBase.
98      *
99      * @param jsonString Indicates the json string.
100      * @return Returns the subclass object.
101      */
102     template <typename T>
ConvertFromJsonString(const std::string & jsonString)103     static T *ConvertFromJsonString(const std::string &jsonString)
104     {
105         if (jsonString.empty()) {
106             ANS_LOGE("Converter : Invalid JSON string");
107             return nullptr;
108         }
109 
110         auto jsonObject = nlohmann::json::parse(jsonString);
111         if (jsonObject.is_null() or !jsonObject.is_object()) {
112             ANS_LOGE("Converter : Invalid JSON object");
113             return nullptr;
114         }
115 
116         return T::FromJson(jsonObject);
117     }
118 };
119 }  // namespace Notification
120 }  // namespace OHOS
121 
122 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_JSON_CONVERT_H
123