1f7a47986Sopenharmony_ci/*
2f7a47986Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3f7a47986Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f7a47986Sopenharmony_ci * you may not use this file except in compliance with the License.
5f7a47986Sopenharmony_ci * You may obtain a copy of the License at
6f7a47986Sopenharmony_ci *
7f7a47986Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f7a47986Sopenharmony_ci *
9f7a47986Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f7a47986Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f7a47986Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f7a47986Sopenharmony_ci * See the License for the specific language governing permissions and
13f7a47986Sopenharmony_ci * limitations under the License.
14f7a47986Sopenharmony_ci */
15f7a47986Sopenharmony_ci
16f7a47986Sopenharmony_ci#ifndef COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
17f7a47986Sopenharmony_ci#define COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
18f7a47986Sopenharmony_ci#include "edm_log.h"
19f7a47986Sopenharmony_ci#include "cJSON.h"
20f7a47986Sopenharmony_ci
21f7a47986Sopenharmony_cinamespace OHOS {
22f7a47986Sopenharmony_cinamespace EDM {
23f7a47986Sopenharmony_ci#define CJSON_CREATE_OBJECT_AND_CHECK(obj, ret)  \
24f7a47986Sopenharmony_ci    do {                                         \
25f7a47986Sopenharmony_ci        (obj) = cJSON_CreateObject();            \
26f7a47986Sopenharmony_ci        if ((obj) == nullptr) {                  \
27f7a47986Sopenharmony_ci            EDMLOGE("cJSON_CreateObject error"); \
28f7a47986Sopenharmony_ci            return (ret);                        \
29f7a47986Sopenharmony_ci        }                                        \
30f7a47986Sopenharmony_ci    } while (0)
31f7a47986Sopenharmony_ci
32f7a47986Sopenharmony_ci#define CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(obj, ret, root)  \
33f7a47986Sopenharmony_ci    do {                                                         \
34f7a47986Sopenharmony_ci        (obj) = cJSON_CreateObject();                            \
35f7a47986Sopenharmony_ci        if ((obj) == nullptr) {                                  \
36f7a47986Sopenharmony_ci            EDMLOGE("cJSON_CreateObject error");                 \
37f7a47986Sopenharmony_ci            cJSON_Delete((root));                                \
38f7a47986Sopenharmony_ci            return (ret);                                        \
39f7a47986Sopenharmony_ci        }                                                        \
40f7a47986Sopenharmony_ci    } while (0)
41f7a47986Sopenharmony_ci
42f7a47986Sopenharmony_ci#define CJSON_CREATE_ARRAY_AND_CHECK(obj, ret)  \
43f7a47986Sopenharmony_ci    do {                                        \
44f7a47986Sopenharmony_ci        (obj) = cJSON_CreateArray();            \
45f7a47986Sopenharmony_ci        if ((obj) == nullptr) {                 \
46f7a47986Sopenharmony_ci            EDMLOGE("cJSON_CreateArray error"); \
47f7a47986Sopenharmony_ci            return (ret);                       \
48f7a47986Sopenharmony_ci        }                                       \
49f7a47986Sopenharmony_ci    } while (0)
50f7a47986Sopenharmony_ci} // namespace EDM
51f7a47986Sopenharmony_ci} // namespace OHOS
52f7a47986Sopenharmony_ci#endif // COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
53