forked from JayFoxRox/pba-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.h
87 lines (76 loc) · 1.64 KB
/
json.h
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
// JSON helper routines
static unsigned int nesting = 0;
static bool isFirst = true; // Wether an element is the first within the object / array
void putComma() {
if (!isFirst) {
fprintf(out, ", ");
} else {
isFirst = false;
}
}
// Internal use only!
void _putEscapedString(const char* value) {
const char* cursor = value;
fputc('"', out);
while(*cursor != '\0') {
// Escape quotes (end of string) and backslashes (escape symbol)
if((*cursor == '"') || (*cursor == '\\')) {
fputc('\\', out);
}
fputc(*cursor++, out);
}
fputc('"', out);
}
void putNull() {
putComma();
fprintf(out, "null");
}
void putString(const char* value) {
putComma();
_putEscapedString(value);
}
void putInteger(long long value) {
putComma();
fprintf(out, "%lld", value);
}
void putFloat(float value) {
putComma();
fprintf(out, "%f", value);
}
void putNesting() {
for(unsigned int i = 0; i < nesting; i++) {
fprintf(out, " "); // Let the "spaces vs. tabs" var begin!
}
}
void putKey(const char* key) {
// This is generated errornously once as we didn't have a key to close
if (!isFirst) {
fprintf(out, ",\n");
}
putNesting();
_putEscapedString(key);
fprintf(out, ": ");
isFirst = true;
}
//FIXME: Rename to "Table" to stay in PBA lingo here?!
void startObject() {
putComma();
fprintf(out, "{\n");
nesting++;
isFirst = true;
}
void endObject() {
nesting--;
fprintf(out, "\n");
putNesting();
fprintf(out, "}");
}
//FIXME: Make it possible to have this either inline or downwards
void startArray() {
putComma();
fprintf(out, "[ ");
isFirst = true;
}
void endArray() {
fprintf(out, " ]");
}