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 COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
17 #define COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
18 #include "edm_log.h"
19 #include "cJSON.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 #define CJSON_CREATE_OBJECT_AND_CHECK(obj, ret)  \
24     do {                                         \
25         (obj) = cJSON_CreateObject();            \
26         if ((obj) == nullptr) {                  \
27             EDMLOGE("cJSON_CreateObject error"); \
28             return (ret);                        \
29         }                                        \
30     } while (0)
31 
32 #define CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(obj, ret, root)  \
33     do {                                                         \
34         (obj) = cJSON_CreateObject();                            \
35         if ((obj) == nullptr) {                                  \
36             EDMLOGE("cJSON_CreateObject error");                 \
37             cJSON_Delete((root));                                \
38             return (ret);                                        \
39         }                                                        \
40     } while (0)
41 
42 #define CJSON_CREATE_ARRAY_AND_CHECK(obj, ret)  \
43     do {                                        \
44         (obj) = cJSON_CreateArray();            \
45         if ((obj) == nullptr) {                 \
46             EDMLOGE("cJSON_CreateArray error"); \
47             return (ret);                       \
48         }                                       \
49     } while (0)
50 } // namespace EDM
51 } // namespace OHOS
52 #endif // COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
53