]> git.ipfire.org Git - people/ms/network.git/blob - src/networkd/json.h
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / networkd / json.h
1 /*#############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
5 # #
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. #
10 # #
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. #
15 # #
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/>. #
18 # #
19 #############################################################################*/
20
21 #ifndef NETWORKD_JSON_H
22 #define NETWORKD_JSON_H
23
24 #include <errno.h>
25 #include <string.h>
26
27 #include <json.h>
28
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
32
33 static inline int __json_object_add(struct json_object* o,
34 const char* key, struct json_object* value) {
35 int r;
36
37 // Add the object
38 r = json_object_object_add(o, key, value);
39 if (r < 0) {
40 if (value)
41 json_object_unref(value);
42 }
43
44 return r;
45 }
46
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;
50
51 // Create a JSON object from the string
52 element = json_object_new_string(value);
53 if (!element)
54 return -errno;
55
56 return __json_object_add(o, key, element);
57 }
58
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;
62
63 // Create a JSON object
64 element = json_object_new_int64(value);
65 if (!element)
66 return -errno;
67
68 return __json_object_add(o, key, element);
69 }
70
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);
75 if (!buffer)
76 return -errno;
77
78 // Copy the string to the heap
79 *s = strdup(buffer);
80 if (!*s)
81 return -errno;
82
83 // If requested, store the length of the string
84 if (l)
85 *l = strlen(*s);
86
87 return 0;
88 }
89
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);
93 if (!e)
94 return NULL;
95
96 return json_object_get_string(e);
97 }
98
99 #endif /* NETWORKD_JSON_H */