1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #ifndef NETWORKD_JSON_H
22 #define NETWORKD_JSON_H
29 // Give some sane names to the reference count functions
30 #define json_object_ref json_object_get
31 #define json_object_unref json_object_put
33 static inline int __json_object_add(struct json_object
* o
,
34 const char* key
, struct json_object
* value
) {
38 r
= json_object_object_add(o
, key
, value
);
41 json_object_unref(value
);
47 static inline int json_object_add_string(struct json_object
* o
,
48 const char* key
, const char* value
) {
49 struct json_object
* element
= NULL
;
51 // Create a JSON object from the string
52 element
= json_object_new_string(value
);
56 return __json_object_add(o
, key
, element
);
59 static inline int json_object_add_int64(struct json_object
* o
,
60 const char* key
, const int64_t value
) {
61 struct json_object
* element
= NULL
;
63 // Create a JSON object
64 element
= json_object_new_int64(value
);
68 return __json_object_add(o
, key
, element
);
71 static inline int json_to_string(struct json_object
* o
, char** s
, size_t* l
) {
72 // Format JSON to string
73 const char* buffer
= json_object_to_json_string_ext(o
,
74 JSON_C_TO_STRING_PRETTY
|JSON_C_TO_STRING_PRETTY_TAB
);
78 // Copy the string to the heap
83 // If requested, store the length of the string
90 static inline const char* json_object_fetch_string(
91 struct json_object
* o
, const char* key
) {
92 struct json_object
* e
= json_object_object_get(o
, key
);
96 return json_object_get_string(e
);
99 #endif /* NETWORKD_JSON_H */