doc: add new document quick-tutorial
[src/app-framework-main.git] / src / utils-json.c
1 /*
2  Copyright 2015 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         json_object_object_add(obj, key, val);
99         return 1;
100 }
101
102 int j_add_string(struct json_object *obj, const char *key, const char *val)
103 {
104         struct json_object *str = json_object_new_string (val ? val : "");
105         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
106 }
107
108 int j_add_boolean(struct json_object *obj, const char *key, int val)
109 {
110         struct json_object *str = json_object_new_boolean (val);
111         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
112 }
113
114 int j_add_integer(struct json_object *obj, const char *key, int val)
115 {
116         struct json_object *str = json_object_new_int (val);
117         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
118 }
119
120 struct json_object *j_add_new_array(struct json_object *obj, const char *key)
121 {
122         struct json_object *result = json_object_new_array();
123         if (result != NULL && !j_add(obj, key, result)) {
124                 json_object_put(result);
125                 result = NULL;
126         }
127         return result;
128 }
129
130 struct json_object *j_add_new_object(struct json_object *obj, const char *key)
131 {
132         struct json_object *result = json_object_new_object();
133         if (result != NULL && !j_add(obj, key, result)) {
134                 json_object_put(result);
135                 result = NULL;
136         }
137         return result;
138 }
139