2 Copyright 2015, 2016 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.
21 #include <json-c/json.h>
23 #include "utils-json.h"
25 int j_read_string(struct json_object *obj, const char **value)
27 return j_is_string(obj) && (*value = json_object_get_string(obj)) != NULL;
30 int j_read_boolean(struct json_object *obj, int *value)
32 return j_is_boolean(obj) && ((*value = (int)json_object_get_boolean(obj)), 1);
35 int j_read_integer(struct json_object *obj, int *value)
37 return j_is_integer(obj) && ((*value = (int)json_object_get_int(obj)), 1);
40 const char *j_string(struct json_object *obj, const char *defval)
42 return j_is_string(obj) ? json_object_get_string(obj) : defval;
45 int j_boolean(struct json_object *obj, int defval)
47 return j_is_boolean(obj) ? json_object_get_boolean(obj) : defval;
50 int j_integer(struct json_object *obj, int defval)
52 return j_is_integer(obj) ? json_object_get_int(obj) : defval;
55 int j_read_object_at(struct json_object *obj, const char *key, struct json_object **value)
57 return json_object_object_get_ex(obj, key, value);
60 int j_read_string_at(struct json_object *obj, const char *key, const char **value)
63 return j_read_object_at(obj, key, &data) && j_read_string(data, value);
66 int j_read_boolean_at(struct json_object *obj, const char *key, int *value)
69 return j_read_object_at(obj, key, &data) && j_read_boolean(data, value);
72 int j_read_integer_at(struct json_object *obj, const char *key, int *value)
75 return j_read_object_at(obj, key, &data) && j_read_integer(data, value);
78 const char *j_string_at(struct json_object *obj, const char *key, const char *defval)
80 struct json_object *data;
81 return j_read_object_at(obj, key, &data) ? j_string(data, defval) : defval;
84 int j_boolean_at(struct json_object *obj, const char *key, int defval)
86 struct json_object *data;
87 return j_read_object_at(obj, key, &data) ? j_boolean(data, defval) : defval;
90 int j_integer_at(struct json_object *obj, const char *key, int defval)
92 struct json_object *data;
93 return j_read_object_at(obj, key, &data) ? j_integer(data, defval) : defval;
96 int j_add(struct json_object *obj, const char *key, struct json_object *val)
99 json_object_object_add(obj, key, val);
101 json_object_array_add(obj, val);
105 int j_add_string(struct json_object *obj, const char *key, const char *val)
107 struct json_object *str = json_object_new_string (val ? val : "");
108 return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
111 int j_add_boolean(struct json_object *obj, const char *key, int val)
113 struct json_object *str = json_object_new_boolean (val);
114 return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
117 int j_add_integer(struct json_object *obj, const char *key, int val)
119 struct json_object *str = json_object_new_int (val);
120 return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
123 struct json_object *j_add_new_array(struct json_object *obj, const char *key)
125 struct json_object *result = json_object_new_array();
126 if (result != NULL && !j_add(obj, key, result)) {
127 json_object_put(result);
133 struct json_object *j_add_new_object(struct json_object *obj, const char *key)
135 struct json_object *result = json_object_new_object();
136 if (result != NULL && !j_add(obj, key, result)) {
137 json_object_put(result);