• 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_ANS_STANDARD_NOTIFICATION_JSON_CONVERT_H
16 #define BASE_NOTIFICATION_ANS_STANDARD_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     /**
27      * Default deconstructor used to deconstruct.
28      */
29     virtual ~NotificationJsonConvertionBase() = default;
30 
31     virtual bool ToJson(nlohmann::json &jsonObject) const = 0;
32 };
33 
34 class NotificationJsonConverter {
35 public:
36     /**
37      * Convert NotificationJsonConvertionBase object to json object.
38      */
ConvertToJosn(const NotificationJsonConvertionBase * convertionBase,nlohmann::json & jsonObject)39     static bool ConvertToJosn(const NotificationJsonConvertionBase *convertionBase, nlohmann::json &jsonObject)
40     {
41         if (convertionBase == nullptr) {
42             ANS_LOGE("Converter : Invalid base object");
43             return false;
44         }
45 
46         return convertionBase->ToJson(jsonObject);
47     }
48 
ConvertToJosnString(const NotificationJsonConvertionBase * convertionBase,std::string & jsonString)49     static bool ConvertToJosnString(const NotificationJsonConvertionBase *convertionBase, std::string &jsonString)
50     {
51         if (convertionBase == nullptr) {
52             ANS_LOGE("Converter : Invalid base object");
53             return false;
54         }
55 
56         nlohmann::json jsonObject;
57         if (!convertionBase->ToJson(jsonObject)) {
58             ANS_LOGE("Converter : Cannot convert to JSON object");
59             return false;
60         }
61         jsonString = jsonObject.dump();
62 
63         return true;
64     }
65 
66     template <typename T>
ConvertFromJosn(const nlohmann::json & jsonObject)67     static T *ConvertFromJosn(const nlohmann::json &jsonObject)
68     {
69         if (jsonObject.is_null() or !jsonObject.is_object()) {
70             ANS_LOGE("Converter : Invalid JSON object");
71             return nullptr;
72         }
73 
74         return T::FromJson(jsonObject);
75     }
76 
77     template <typename T>
ConvertFromJosnString(const std::string & jsonString)78     static T *ConvertFromJosnString(const std::string &jsonString)
79     {
80         if (jsonString.empty()) {
81             ANS_LOGE("Converter : Invalid JSON string");
82             return nullptr;
83         }
84 
85         auto jsonObject = nlohmann::json::parse(jsonString);
86         if (jsonObject.is_null() or !jsonObject.is_object()) {
87             ANS_LOGE("Converter : Invalid JSON object");
88             return nullptr;
89         }
90 
91         return T::FromJson(jsonObject);
92     }
93 };
94 }  // namespace Notification
95 }  // namespace OHOS
96 
97 #endif  // BASE_NOTIFICATION_ANS_STANDARD_NOTIFICATION_JSON_CONVERT_H
98