forked from dylex/slack-libpurple
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack-json.c
60 lines (52 loc) · 1.18 KB
/
slack-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
#include <string.h>
#include "slack-json.h"
json_value *json_get_prop(json_value *val, const char *index) {
if (!val || val->type != json_object) {
return NULL;
}
for (unsigned int i = 0; i < val->u.object.length; ++ i) {
if (!strcmp (val->u.object.values[i].name, index)) {
return val->u.object.values[i].value;
}
}
return NULL;
}
GString *append_json_string(GString *str, const char *s) {
g_string_append_c(str, '"');
const char *p = s;
char c;
for (;;) {
switch ((c = *p)) {
case '\0':
case '"':
case '\\': break;
case '\b': c = 'b'; break;
case '\f': c = 'f'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\t': c = 't'; break;
default:
p++;
continue;
}
g_string_append_len(str, s, p-s);
if (!c)
break;
g_string_append_c(str, '\\');
g_string_append_c(str, c);
s = ++p;
}
return g_string_append_c(str, '"');
}
time_t slack_parse_time(json_value *val) {
if (!val)
return 0;
if (val->type == json_integer)
return val->u.integer;
if (val->type == json_double)
return val->u.dbl;
if (val->type == json_string)
/* "EPOCH.0000ID", atol is sufficient */
return atol(val->u.string.ptr);
return 0;
}