-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsontidy.c
More file actions
132 lines (119 loc) · 3.58 KB
/
jsontidy.c
File metadata and controls
132 lines (119 loc) · 3.58 KB
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
/* gcc -I/usr/local/include jsontidy.c -o jsontidy -L/usr/local/lib -ljsonparser */
/* ./jsontidy -i 4 < example.json */
#include <jsonparser.h>
#include <assert.h>
#include <unistd.h>
typedef struct UserDataStruct {
int depth;
int indent;
} UserData, *UserDataPtr;
#define userDataDepth (((UserDataPtr)userData)->depth)
#define userDataIndent (((UserDataPtr)userData)->indent)
char *jsonTypes[] = { NULL, "string", "number", "{}", "[]", "true", "false", "null" };
bool writeArrayElement(void *userData, JSONParserValue *value)
{
char *elementValue = value->string; /* string and number */
elementValue = elementValue ? elementValue : jsonTypes[value->type];
return fprintf(stdout, "%*s\n", (userDataDepth + 1) * userDataIndent, elementValue) > 0;
}
bool writeObjectMember(void *userData, char *name, JSONParserValue *value)
{
char *memberValue = value->string; /* string and number */
memberValue = memberValue ? memberValue : jsonTypes[value->type];
return fprintf(stdout, "%*s = %s\n", (userDataDepth + 1) * userDataIndent, name, memberValue) > 0;
}
bool writeStart(void *userData)
{
/* userDataContext = newDocument(); */
userDataDepth = 0;
return true;
}
bool writeStartArray(void *userData, char *name)
{
/* userDataContext = addChild(userDataContext, newArray(), name); */
if (name && userDataDepth) {
fprintf(stdout, "%*s = %s\n", (userDataDepth + 1) * userDataIndent, name, "[");
} else {
fprintf(stdout, "%*s\n", userDataDepth * userDataIndent, "[");
}
userDataDepth++;
return true;
}
bool writeStartObject(void *userData, char *name)
{
/* userDataContext = addChild(userDataContext, newObject(), name); */
if (name && userDataDepth) {
fprintf(stdout, "%*s = %s\n", (userDataDepth + 1) * userDataIndent, name, "{");
} else {
fprintf(stdout, "%*s\n", userDataDepth * userDataIndent, "{");
}
userDataDepth++;
return true;
}
bool writeStop(void *userData)
{
assert(userDataDepth == 0);
return true;
}
bool writeStopArray(void *userData)
{
/* userDataContext = getParent(userDataContext); */
userDataDepth--;
fprintf(stdout, "%*s\n", userDataDepth * userDataIndent, "]");
return true;
}
bool writeStopObject(void *userData)
{
/* userDataContext = getParent(userDataContext); */
userDataDepth--;
fprintf(stdout, "%*s\n", userDataDepth * userDataIndent, "}");
return true;
}
int main(int argc, char **argv)
{
char *ivalue = NULL;
int c;
while ((c = getopt(argc, argv, "i:")) != -1) {
switch (c) {
case 'i': /* Indentation. */
ivalue = optarg;
break;
case '?':
/* fall through */
default:
fprintf(stderr, "Usage: %s [-i indent]\n", argv[0]);
return 1;
}
}
JSONParserConfig config = {
NULL, NULL, NULL,
writeArrayElement, writeObjectMember,
writeStart, writeStartArray, writeStartObject,
writeStop, writeStopArray, writeStopObject
};
JSONParser parser = createJSONParser(&config);
if (parser) {
UserData userData = { 0, ivalue ? atoi(ivalue) : 8 };
jsonParserSetUserData(parser, &userData);
JSONParserBuffer *buffer = createJSONParserBuffer(JSON_PARSER_BUFFER_SIZE);
if (buffer) {
if (!jsonParserParseStream(parser, buffer, NULL, NULL, NULL)) {
fprintf(stderr, "Error: parser error: %d %s (line %d)\n",
jsonParserGetErrorCode(parser), jsonParserGetErrorString(parser),
jsonParserGetCurrentLine(parser));
return 1;
}
jsonParserBufferFree(buffer);
buffer = NULL;
} else {
fprintf(stderr, "Error: could not allocate buffer: %lld\n", (long long)buffer->size);
return 1;
}
jsonParserFree(parser);
parser = NULL;
} else {
fprintf(stderr, "Error: could not create parser\n");
return 1;
}
return 0;
}