2 Copyright (C) 2015-2018 IoT.bzh
4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
25 #include <json-c/json.h>
27 #include "utils-json.h"
29 int j_read_string(struct json_object *obj, const char **value)
31 return j_is_string(obj) && (*value = json_object_get_string(obj)) != NULL;
34 int j_read_boolean(struct json_object *obj, int *value)
36 return j_is_boolean(obj) && ((*value = (int)json_object_get_boolean(obj)), 1);
39 int j_read_integer(struct json_object *obj, int *value)
41 return j_is_integer(obj) && ((*value = (int)json_object_get_int(obj)), 1);
44 const char *j_string(struct json_object *obj, const char *defval)
46 return j_is_string(obj) ? json_object_get_string(obj) : defval;
49 int j_boolean(struct json_object *obj, int defval)
51 return j_is_boolean(obj) ? json_object_get_boolean(obj) : defval;
54 int j_integer(struct json_object *obj, int defval)
56 return j_is_integer(obj) ? json_object_get_int(obj) : defval;
59 int j_has(struct json_object *obj, const char *key)
61 return json_object_object_get_ex(obj, key, NULL);
64 int j_read_object_at(struct json_object *obj, const char *key, struct json_object **value)
66 return json_object_object_get_ex(obj, key, value);
69 int j_read_string_at(struct json_object *obj, const char *key, const char **value)
72 return j_read_object_at(obj, key, &data) && j_read_string(data, value);
75 int j_read_boolean_at(struct json_object *obj, const char *key, int *value)
78 return j_read_object_at(obj, key, &data) && j_read_boolean(data, value);
81 int j_read_integer_at(struct json_object *obj, const char *key, int *value)
84 return j_read_object_at(obj, key, &data) && j_read_integer(data, value);
87 const char *j_string_at(struct json_object *obj, const char *key, const char *defval)
89 struct json_object *data;
90 return j_read_object_at(obj, key, &data) ? j_string(data, defval) : defval;
93 int j_boolean_at(struct json_object *obj, const char *key, int defval)
95 struct json_object *data;
96 return j_read_object_at(obj, key, &data) ? j_boolean(data, defval) : defval;
99 int j_integer_at(struct json_object *obj, const char *key, int defval)
101 struct json_object *data;
102 return j_read_object_at(obj, key, &data) ? j_integer(data, defval) : defval;
105 int j_add(struct json_object *obj, const char *key, struct json_object *val)
108 json_object_object_add(obj, key, val);
110 json_object_array_add(obj, val);
114 int j_add_string(struct json_object *obj, const char *key, const char *val)
116 struct json_object *str = json_object_new_string (val ? val : "");
117 return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
120 int j_add_boolean(struct json_object *obj, const char *key, int val)
122 struct json_object *str = json_object_new_boolean (val);
123 return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
126 int j_add_integer(struct json_object *obj, const char *key, int val)
128 struct json_object *str = json_object_new_int (val);
129 return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
132 struct json_object *j_add_new_array(struct json_object *obj, const char *key)
134 struct json_object *result = json_object_new_array();
135 if (result != NULL && !j_add(obj, key, result)) {
136 json_object_put(result);
142 struct json_object *j_add_new_object(struct json_object *obj, const char *key)
144 struct json_object *result = json_object_new_object();
145 if (result != NULL && !j_add(obj, key, result)) {
146 json_object_put(result);
152 int j_enter_m(struct json_object **obj, const char **mkey, int create)
155 struct json_object *o, *x;
163 n = strndupa(k, (size_t)(f - k));
164 if (!json_object_object_get_ex(o, n, &x)) {
167 x = j_add_new_object(o, n);
182 int j_has_m(struct json_object *obj, const char *mkey)
184 return !j_enter_m(&obj, &mkey, 0) && j_has(obj, mkey);
187 int j_read_object_at_m(struct json_object *obj, const char *mkey, struct json_object **value)
189 return !j_enter_m(&obj, &mkey, 0) && j_read_object_at(obj, mkey, value);
192 int j_read_string_at_m(struct json_object *obj, const char *mkey, const char **value)
195 return j_read_object_at_m(obj, mkey, &data) && j_read_string(data, value);
198 int j_read_boolean_at_m(struct json_object *obj, const char *mkey, int *value)
201 return j_read_object_at_m(obj, mkey, &data) && j_read_boolean(data, value);
204 int j_read_integer_at_m(struct json_object *obj, const char *mkey, int *value)
207 return j_read_object_at_m(obj, mkey, &data) && j_read_integer(data, value);
210 const char *j_string_at_m(struct json_object *obj, const char *mkey, const char *defval)
212 struct json_object *data;
213 return j_read_object_at_m(obj, mkey, &data) ? j_string(data, defval) : defval;
216 int j_boolean_at_m(struct json_object *obj, const char *mkey, int defval)
218 struct json_object *data;
219 return j_read_object_at_m(obj, mkey, &data) ? j_boolean(data, defval) : defval;
222 int j_integer_at_m(struct json_object *obj, const char *mkey, int defval)
224 struct json_object *data;
225 return j_read_object_at_m(obj, mkey, &data) ? j_integer(data, defval) : defval;
228 int j_add_m(struct json_object *obj, const char *mkey, struct json_object *val)
231 return !j_enter_m(&obj, &mkey, 1) && j_add(obj, mkey, val);
232 json_object_array_add(obj, val);
236 int j_add_string_m(struct json_object *obj, const char *mkey, const char *val)
238 struct json_object *str = json_object_new_string (val ? val : "");
239 return str ? j_add_m(obj, mkey, str) : (errno = ENOMEM, 0);
242 int j_add_boolean_m(struct json_object *obj, const char *mkey, int val)
244 struct json_object *str = json_object_new_boolean (val);
245 return str ? j_add_m(obj, mkey, str) : (errno = ENOMEM, 0);
248 int j_add_integer_m(struct json_object *obj, const char *mkey, int val)
250 struct json_object *str = json_object_new_int (val);
251 return str ? j_add_m(obj, mkey, str) : (errno = ENOMEM, 0);
254 struct json_object *j_add_new_array_m(struct json_object *obj, const char *mkey)
256 struct json_object *result = json_object_new_array();
257 if (result != NULL && !j_add_m(obj, mkey, result)) {
258 json_object_put(result);
264 struct json_object *j_add_new_object_m(struct json_object *obj, const char *mkey)
266 struct json_object *result = json_object_new_object();
267 if (result != NULL && !j_add_m(obj, mkey, result)) {
268 json_object_put(result);
274 int j_add_many_strings_m(struct json_object *obj, ...)
276 const char *key, *val;
282 key = va_arg(ap, const char *);
284 val = va_arg(ap, const char *);
286 if (!j_add_string_m(obj, key, val))
289 key = va_arg(ap, const char *);