74df9a949bf015237a372b28a6efa1ad928a70f8
[src/app-framework-main.git] / src / utils-json.c
1 /*
2  Copyright 2015, 2016 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
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
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
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.
17 */
18
19 #include <errno.h>
20
21 #include <json-c/json.h>
22
23 #include "utils-json.h"
24
25 int j_read_string(struct json_object *obj, const char **value)
26 {
27         return j_is_string(obj) && (*value = json_object_get_string(obj)) != NULL;
28 }
29
30 int j_read_boolean(struct json_object *obj, int *value)
31 {
32         return j_is_boolean(obj) && ((*value = (int)json_object_get_boolean(obj)), 1);
33 }
34
35 int j_read_integer(struct json_object *obj, int *value)
36 {
37         return j_is_integer(obj) && ((*value = (int)json_object_get_int(obj)), 1);
38 }
39
40 const char *j_string(struct json_object *obj, const char *defval)
41 {
42         return j_is_string(obj) ? json_object_get_string(obj) : defval;
43 }
44
45 int j_boolean(struct json_object *obj, int defval)
46 {
47         return j_is_boolean(obj) ? json_object_get_boolean(obj) : defval;
48 }
49
50 int j_integer(struct json_object *obj, int defval)
51 {
52         return j_is_integer(obj) ? json_object_get_int(obj) : defval;
53 }
54
55 int j_read_object_at(struct json_object *obj, const char *key, struct json_object **value)
56 {
57         return json_object_object_get_ex(obj, key, value);
58 }
59
60 int j_read_string_at(struct json_object *obj, const char *key, const char **value)
61 {
62         json_object *data;
63         return j_read_object_at(obj, key, &data) && j_read_string(data, value);
64 }
65
66 int j_read_boolean_at(struct json_object *obj, const char *key, int *value)
67 {
68         json_object *data;
69         return j_read_object_at(obj, key, &data) && j_read_boolean(data, value);
70 }
71
72 int j_read_integer_at(struct json_object *obj, const char *key, int *value)
73 {
74         json_object *data;
75         return j_read_object_at(obj, key, &data) && j_read_integer(data, value);
76 }
77
78 const char *j_string_at(struct json_object *obj, const char *key, const char *defval)
79 {
80         struct json_object *data;
81         return j_read_object_at(obj, key, &data) ? j_string(data, defval) : defval;
82 }
83
84 int j_boolean_at(struct json_object *obj, const char *key, int defval)
85 {
86         struct json_object *data;
87         return j_read_object_at(obj, key, &data) ? j_boolean(data, defval) : defval;
88 }
89
90 int j_integer_at(struct json_object *obj, const char *key, int defval)
91 {
92         struct json_object *data;
93         return j_read_object_at(obj, key, &data) ? j_integer(data, defval) : defval;
94 }
95
96 int j_add(struct json_object *obj, const char *key, struct json_object *val)
97 {
98         if (key)
99                 json_object_object_add(obj, key, val);
100         else
101                 json_object_array_add(obj, val);
102         return 1;
103 }
104
105 int j_add_string(struct json_object *obj, const char *key, const char *val)
106 {
107         struct json_object *str = json_object_new_string (val ? val : "");
108         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
109 }
110
111 int j_add_boolean(struct json_object *obj, const char *key, int val)
112 {
113         struct json_object *str = json_object_new_boolean (val);
114         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
115 }
116
117 int j_add_integer(struct json_object *obj, const char *key, int val)
118 {
119         struct json_object *str = json_object_new_int (val);
120         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
121 }
122
123 struct json_object *j_add_new_array(struct json_object *obj, const char *key)
124 {
125         struct json_object *result = json_object_new_array();
126         if (result != NULL && !j_add(obj, key, result)) {
127                 json_object_put(result);
128                 result = NULL;
129         }
130         return result;
131 }
132
133 struct json_object *j_add_new_object(struct json_object *obj, const char *key)
134 {
135         struct json_object *result = json_object_new_object();
136         if (result != NULL && !j_add(obj, key, result)) {
137                 json_object_put(result);
138                 result = NULL;
139         }
140         return result;
141 }
142