Line data Source code
1 : /*!
2 : * \file
3 : * \author Deniz Armagan
4 : * \date 2019-04-10
5 : * \version 0.1
6 : *
7 : * \brief Universal JAPI helper functions library.
8 : *
9 : * \details
10 : * japi_utils is a universal JSON API helper library.
11 : *
12 : * \copyright
13 : * Copyright (c) 2023 Fraunhofer IIS
14 : *
15 : * Permission is hereby granted, free of charge, to any person obtaining a copy
16 : * of this software and associated documentation files (the “Software”), to deal
17 : * in the Software without restriction, including without limitation the rights
18 : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 : * copies of the Software, and to permit persons to whom the Software is
20 : * furnished to do so, subject to the following conditions:
21 : *
22 : * The above copyright notice and this permission notice shall be included in
23 : * all copies or substantial portions of the Software.
24 : *
25 : * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 : * THE SOFTWARE.
32 : */
33 :
34 : #include <assert.h>
35 : #include <stdio.h> /* printf, fprintf */
36 : #include <string.h> /* strcasecmp */
37 : #include <json-c/json.h>
38 :
39 : #include "japi_utils.h"
40 :
41 :
42 : /* Look for a JSON object with the key 'key' and return its value as a string.
43 : *
44 : * returns
45 : * 0 if legal string value could be fetched
46 : * -1 if given JSON object is NULL
47 : * -2 if given key is NULL
48 : * -3 if given key doesn't exist
49 : * -4 if the requested value is not a string type
50 : */
51 12 : int japi_get_value_as_str(json_object *jobj, const char *key, const char** val)
52 : {
53 : json_object *jval;
54 :
55 : /* Error Handling */
56 12 : if (jobj == NULL) {
57 1 : return -1;
58 : }
59 11 : if (key == NULL) {
60 1 : return -2;
61 : }
62 :
63 : /* Key doesn't exist */
64 10 : if (!json_object_object_get_ex(jobj, key, &jval)) {
65 1 : return -3;
66 : }
67 :
68 : /* The requested value is not a string type */
69 9 : if (!json_object_is_type(jval, json_type_string)) {
70 1 : return -4;
71 : }
72 :
73 8 : *val = json_object_get_string(jval);
74 :
75 8 : return 0;
76 : }
77 :
78 : /* Look for a JSON object with the key 'key' and return its value as a bool.
79 : *
80 : * returns
81 : * 0 if legal boolean value could be fetched
82 : * -1 if given JSON object is NULL
83 : * -2 if given key is NULL
84 : * -3 if given key doesn't exist
85 : * -4 if the requested value is not a boolean type
86 : */
87 15 : int japi_get_value_as_bool(json_object *jobj, const char *key, bool *val)
88 : {
89 : json_object *jval;
90 :
91 : /* Error Handling */
92 15 : if (jobj == NULL) {
93 1 : return -1;
94 : }
95 14 : if (key == NULL) {
96 1 : return -2;
97 : }
98 :
99 : /* Key doesn't exist */
100 13 : if (!json_object_object_get_ex(jobj, key, &jval)) {
101 1 : return -3;
102 : }
103 :
104 : /* The requested value is not a boolean type */
105 12 : if (!json_object_is_type(jval, json_type_boolean)) {
106 1 : return -4;
107 : }
108 :
109 11 : *val = json_object_get_boolean(jval);
110 :
111 11 : return 0;
112 : }
113 :
114 : /* Look for a JSON object with the key 'key' and return its value as a int.
115 : *
116 : * returns
117 : * 0 if legal int value could be fetched
118 : * -1 if given JSON object is NULL
119 : * -2 if given key is NULL
120 : * -3 if given key doesn't exist
121 : * -4 if the requested value is not a int type
122 : */
123 5 : int japi_get_value_as_int(json_object *jobj, const char *key, int *val)
124 : {
125 : json_object *jval;
126 :
127 : /* Error Handling */
128 5 : if (jobj == NULL) {
129 1 : return -1;
130 : }
131 4 : if (key == NULL) {
132 1 : return -2;
133 : }
134 :
135 : /* Key doesn't exist */
136 3 : if (!json_object_object_get_ex(jobj, key, &jval)) {
137 1 : return -3;
138 : }
139 :
140 : /* The requested value is not a int type */
141 2 : if (!json_object_is_type(jval, json_type_int)) {
142 1 : return -4;
143 : }
144 :
145 1 : *val = json_object_get_int(jval);
146 :
147 1 : return 0;
148 : }
149 : /* Look for a JSON object with the key 'key' and return its value as a int64.
150 : *
151 : * returns
152 : * 0 if legal int64 value could be fetched
153 : * -1 if given JSON object is NULL
154 : * -2 if given key is NULL
155 : * -3 if given key doesn't exist
156 : * -4 if the requested value is not a int64 type
157 : */
158 5 : int japi_get_value_as_int64(json_object *jobj, const char *key, long long int *val)
159 : {
160 : json_object *jval;
161 :
162 : /* Error Handling */
163 5 : if (jobj == NULL) {
164 1 : return -1;
165 : }
166 4 : if (key == NULL) {
167 1 : return -2;
168 : }
169 :
170 : /* Key doesn't exist */
171 3 : if (!json_object_object_get_ex(jobj, key, &jval)) {
172 1 : return -3;
173 : }
174 :
175 : /* The requested value is not a int64 type */
176 2 : if (!json_object_is_type(jval, json_type_int)) {
177 1 : return -4;
178 : }
179 :
180 1 : *val = json_object_get_int64(jval);
181 :
182 1 : return 0;
183 : }
184 :
185 : /* Look for a JSON object with the key 'key' and return its value as a double.
186 : *
187 : * returns
188 : * 0 if legal double value could be fetched
189 : * -1 if given JSON object is NULL
190 : * -2 if given key is NULL
191 : * -3 if given key doesn't exist
192 : * -4 if the requested value is not a double type
193 : */
194 5 : int japi_get_value_as_double(json_object *jobj, const char *key, double *val)
195 : {
196 : json_object *jval;
197 :
198 : /* Error Handling */
199 5 : if (jobj == NULL) {
200 1 : return -1;
201 : }
202 4 : if (key == NULL) {
203 1 : return -2;
204 : }
205 :
206 : /* Key doesn't exist */
207 3 : if (!json_object_object_get_ex(jobj, key, &jval)) {
208 1 : return -3;
209 : }
210 :
211 : /* The requested value is not a double type */
212 2 : if (!json_object_is_type(jval, json_type_double)) {
213 1 : return -4;
214 : }
215 :
216 1 : *val = json_object_get_double(jval);
217 :
218 1 : return 0;
219 : }
220 :
221 :
222 : /* Stringify the JSON object, copy it to a new allocated buffer and append a
223 : * newline. This is necessary to ensure that a call to write() will send the
224 : * message at once, meaning the message and the newline are not separated in
225 : * two strings.
226 : *
227 : * The caller is responsible for freeing the returned string.
228 : */
229 4 : char* japi_get_jobj_as_ndstr(json_object *jobj)
230 : {
231 : const char *tmp_str;
232 : char *response;
233 : size_t tmp_str_len;
234 :
235 4 : tmp_str = json_object_to_json_string(jobj);
236 4 : tmp_str_len = strlen(tmp_str);
237 :
238 4 : response = (char *)malloc(strlen(tmp_str)+2);
239 4 : if (response == NULL) {
240 0 : perror("malloc");
241 0 : return NULL;
242 : }
243 :
244 4 : strcpy(response, tmp_str);
245 4 : response[tmp_str_len+0] = '\n';
246 4 : response[tmp_str_len+1] = '\0';
247 :
248 4 : return response;
249 : }
|