forked from bliksemlabs/rrrr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.c
157 lines (132 loc) · 3.38 KB
/
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Copyright 2013 Bliksem Labs.
* See the LICENSE file at the top-level directory of this distribution and at
* https://github.com/bliksemlabs/rrrr/
*/
/* json.c */
#include "json.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
/* private functions */
/* Check an operation that will write multiple characters to the buffer,
* when the maximum number of characters is known.
*/
static bool remaining(json_t *j, size_t n) {
if (j->b + n < j->buf_end) return true;
j->overflowed = true;
return false;
}
/* Overflow-checked copy of a single char to the buffer. */
static void check(json_t *j, char c) {
if (j->b >= j->buf_end) j->overflowed = true;
else *(j->b++) = c;
}
/* Add a comma to the buffer, but only if we are currently in a list. */
static void comma(json_t *j) {
if (j->in_list) check(j, ',');
}
/* Write a string out to the buffer, surrounding it in quotes and
* escaping all quotes or slashes.
*/
static void string (json_t *j, const char *s) {
const char *c;
if (s == NULL) {
if (remaining(j, 4)) j->b += sprintf(j->b, "null");
return;
}
check(j, '"');
for (c = s; *c != '\0'; ++c) {
switch (*c) {
case '\\' :
case '\b' :
case '\f' :
case '\n' :
case '\r' :
case '\t' :
case '\v' :
case '"' :
check(j, '\\');
default:
check(j, *c);
}
}
check(j, '"');
}
/* Escape a key and copy it to the buffer, preparing for a single value.
* This should only be used internally, since it sets in_list _before_
* the value is added.
*/
static void ekey (json_t *j, const char *k) {
comma(j);
string(j, k);
check(j, ':');
j->in_list = true;
}
/* public functions (eventually) */
void json_init (json_t *j, char *buf, size_t buflen) {
j->buf_start = j->b = buf;
j->buf_end = j->b + buflen - 1;
j->in_list = false;
j->overflowed = false;
}
void json_kv(json_t *j, char *key, const char *value) {
ekey(j, key);
string(j, value);
}
void json_kd(json_t *j, char *key, int value) {
ekey(j, key);
if (remaining(j, 11)) j->b += sprintf(j->b, "%d", value);
}
void json_kf(json_t *j, char *key, double value) {
ekey(j, key);
if (remaining(j, 12)) j->b += sprintf(j->b, "%5.5f", value);
}
void json_kl(json_t *j, char *key, int64_t value) {
ekey(j, key);
if (remaining(j, 21)) j->b += sprintf(j->b, "%" PRId64 , value);
}
void json_kb(json_t *j, char *key, bool value) {
ekey(j, key);
if (remaining(j, 5)) j->b += sprintf(j->b, value ? "true" : "false");
}
void json_key_obj(json_t *j, char *key) {
if (key)
ekey(j, key);
else
comma(j);
check(j, '{');
j->in_list = false;
}
void json_key_arr(json_t *j, char *key) {
ekey(j, key);
check(j, '[');
j->in_list = false;
}
void json_obj(json_t *j) {
comma(j );
check(j, '{');
j->in_list = false;
}
void json_arr(json_t *j) {
comma(j);
check(j, '[');
j->in_list = false;
}
void json_end_obj(json_t *j) {
check(j, '}');
j->in_list = true;
}
void json_end_arr(json_t *j) {
check(j, ']');
j->in_list = true;
}
size_t json_length(json_t *j) {
return j->b - j->buf_start;
}
void json_dump(json_t *j) {
*j->b = '\0';
if (j->overflowed) printf ("[JSON OVERFLOW]\n");
printf("%s\n", j->buf_start);
}