82908f48c3c51d0a7d860e799863e48986e70208
[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.h>
22
23 #include "utils-json.h"
24
25 int j_object(struct json_object *obj, const char *key, struct json_object **value)
26 {
27         return json_object_object_get_ex(obj, key, value);
28 }
29
30 int j_string(struct json_object *obj, const char *key, const char **value)
31 {
32         json_object *data;
33         return j_object(obj, key, &data)
34                 && json_object_get_type(data) == json_type_string
35                 && (*value = json_object_get_string(data)) != NULL;
36 }
37
38 int j_boolean(struct json_object *obj, const char *key, int *value)
39 {
40         json_object *data;
41         return json_object_object_get_ex(obj, key, &data)
42                 && json_object_get_type(data) == json_type_boolean
43                 && ((*value = (int)json_object_get_boolean(data)), 1);
44 }
45
46 int j_integer(struct json_object *obj, const char *key, int *value)
47 {
48         json_object *data;
49         return json_object_object_get_ex(obj, key, &data)
50                 && json_object_get_type(data) == json_type_int
51                 && ((*value = (int)json_object_get_int(data)), 1);
52 }
53
54 const char *j_get_string(struct json_object *obj, const char *key, const char *defval)
55 {
56         struct json_object *o;
57         return json_object_object_get_ex(obj, key, &o)
58                 && json_object_get_type(o) == json_type_string
59                         ? json_object_get_string(o) : defval;
60 }
61
62 int j_get_boolean(struct json_object *obj, const char *key, int defval)
63 {
64         struct json_object *o;
65         return json_object_object_get_ex(obj, key, &o)
66                 && json_object_get_type(o) == json_type_boolean
67                         ? json_object_get_boolean(o) : defval;
68 }
69
70 int j_get_integer(struct json_object *obj, const char *key, int defval)
71 {
72         struct json_object *o;
73         return json_object_object_get_ex(obj, key, &o)
74                 && json_object_get_type(o) == json_type_int
75                         ? json_object_get_int(o) : defval;
76 }
77
78 int j_add(struct json_object *obj, const char *key, struct json_object *val)
79 {
80         json_object_object_add(obj, key, val);
81         return 1;
82 }
83
84 int j_add_string(struct json_object *obj, const char *key, const char *val)
85 {
86         struct json_object *str = json_object_new_string (val ? val : "");
87         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
88 }
89
90 int j_add_boolean(struct json_object *obj, const char *key, int val)
91 {
92         struct json_object *str = json_object_new_boolean (val);
93         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
94 }
95
96 int j_add_integer(struct json_object *obj, const char *key, int val)
97 {
98         struct json_object *str = json_object_new_int (val);
99         return str ? j_add(obj, key, str) : (errno = ENOMEM, 0);
100 }
101