forked from matrix-org/purple-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-json.c
130 lines (107 loc) · 3.1 KB
/
matrix-json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* matrix-json.c
*
* Convenience wrappers for libjson-glib
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "matrix-json.h"
/* node */
const gchar *matrix_json_node_get_string(JsonNode *node)
{
if(node == NULL)
return NULL;
if(JSON_NODE_TYPE(node) != JSON_NODE_VALUE)
return NULL;
return json_node_get_string(node);
}
gint64 matrix_json_node_get_int(JsonNode *node)
{
if(node == NULL)
return 0;
if(JSON_NODE_TYPE(node) != JSON_NODE_VALUE)
return 0;
return json_node_get_int(node);
}
JsonObject *matrix_json_node_get_object (JsonNode *node)
{
if(node == NULL)
return NULL;
if(JSON_NODE_TYPE(node) != JSON_NODE_OBJECT)
return NULL;
return json_node_get_object(node);
}
JsonArray *matrix_json_node_get_array(JsonNode *node)
{
if(node == NULL)
return NULL;
if(JSON_NODE_TYPE(node) != JSON_NODE_ARRAY)
return NULL;
return json_node_get_array(node);
}
/* object */
JsonNode *matrix_json_object_get_member (JsonObject *object,
const gchar *member_name)
{
g_assert(member_name != NULL);
if(object == NULL)
return NULL;
return json_object_get_member(object, member_name);
}
const gchar *matrix_json_object_get_string_member(JsonObject *object,
const gchar *member_name)
{
JsonNode *member;
member = matrix_json_object_get_member(object, member_name);
return matrix_json_node_get_string(member);
}
gint64 matrix_json_object_get_int_member(JsonObject *object,
const gchar *member_name)
{
JsonNode *member;
member = matrix_json_object_get_member(object, member_name);
return matrix_json_node_get_int(member);
}
JsonObject *matrix_json_object_get_object_member(JsonObject *object,
const gchar *member_name)
{
JsonNode *member;
member = matrix_json_object_get_member(object, member_name);
return matrix_json_node_get_object(member);
}
JsonArray *matrix_json_object_get_array_member(JsonObject *object,
const gchar *member_name)
{
JsonNode *member;
member = matrix_json_object_get_member(object, member_name);
return matrix_json_node_get_array(member);
}
/* array */
JsonNode *matrix_json_array_get_element(JsonArray *array,
guint index)
{
if(array == NULL)
return NULL;
if(json_array_get_length(array) <= index)
return NULL;
return json_array_get_element(array, index);
}
const gchar *matrix_json_array_get_string_element(JsonArray *array,
guint index)
{
JsonNode *element;
element = matrix_json_array_get_element(array, index);
return matrix_json_node_get_string(element);
}