Skip to content

Commit 9d553c9

Browse files
committed
scalar: implement a minimal JSON parser
No grown-up C project comes without their own JSON parser. Just kidding! We need to parse a JSON result when determining which cache server to use. It would appear that searching for needles `"CacheServers":[`, `"Url":"` and `"GlobalDefault":true` _happens_ to work right now, it is fragile as it depends on no whitespace padding and on the order of the fields remaining as-is. Let's implement a super simple JSON parser (at the cost of being slightly inefficient) for that purpose. To avoid allocating a ton of memory, we implement a callback-based one. And to save on complexity, let's not even bother validating the input properly (we will just go ahead and instead rely on Azure Repos to produce correct JSON). Note: An alternative would have been to use existing solutions such as JSON-C, CentiJSON or JSMN. However, they are all a lot larger than the current solution; The smallest, JSMN, which does not even provide parsed string values (something we actually need) weighs in with 471 lines, while we get away with 182 + 29 lines for the C and the header file, respectively. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 0799883 commit 9d553c9

File tree

5 files changed

+226
-3
lines changed

5 files changed

+226
-3
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,7 @@ GIT_OBJS += git.o
28252825
.PHONY: git-objs
28262826
git-objs: $(GIT_OBJS)
28272827

2828+
SCALAR_OBJS := json-parser.o
28282829
SCALAR_OBJS += scalar.o
28292830
.PHONY: scalar-objs
28302831
scalar-objs: $(SCALAR_OBJS)
@@ -2980,7 +2981,7 @@ $(REMOTE_CURL_PRIMARY): remote-curl.o http.o http-walker.o $(LAZYLOAD_LIBCURL_OB
29802981
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
29812982
$(CURL_LIBCURL) $(EXPAT_LIBEXPAT) $(LIBS)
29822983

2983-
scalar$X: scalar.o GIT-LDFLAGS $(GITLIBS)
2984+
scalar$X: $(SCALAR_OBJS) GIT-LDFLAGS $(GITLIBS)
29842985
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
29852986
$(filter %.o,$^) $(LIBS)
29862987

contrib/buildsystems/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ target_link_libraries(git-sh-i18n--envsubst common-main)
804804
add_executable(git-shell ${CMAKE_SOURCE_DIR}/shell.c)
805805
target_link_libraries(git-shell common-main)
806806

807-
add_executable(scalar ${CMAKE_SOURCE_DIR}/scalar.c)
807+
add_executable(scalar ${CMAKE_SOURCE_DIR}/scalar.c ${CMAKE_SOURCE_DIR}/json-parser.c)
808808
target_link_libraries(scalar common-main)
809809

810810
if(CURL_FOUND)

json-parser.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#include "git-compat-util.h"
2+
#include "hex.h"
3+
#include "json-parser.h"
4+
5+
int reset_iterator(struct json_iterator *it)
6+
{
7+
it->p = it->begin = it->json;
8+
strbuf_release(&it->key);
9+
strbuf_release(&it->string_value);
10+
it->type = JSON_NULL;
11+
return -1;
12+
}
13+
14+
static int parse_json_string(struct json_iterator *it, struct strbuf *out)
15+
{
16+
const char *begin = it->p;
17+
18+
if (*(it->p)++ != '"') {
19+
error("expected double quote: '%.*s'", 5, begin);
20+
return reset_iterator(it);
21+
}
22+
23+
strbuf_reset(&it->string_value);
24+
#define APPEND(c) strbuf_addch(out, c)
25+
while (*it->p != '"') {
26+
switch (*it->p) {
27+
case '\0':
28+
error("incomplete string: '%s'", begin);
29+
return reset_iterator(it);
30+
case '\\':
31+
it->p++;
32+
if (*it->p == '\\' || *it->p == '"')
33+
APPEND(*it->p);
34+
else if (*it->p == 'b')
35+
APPEND(8);
36+
else if (*it->p == 't')
37+
APPEND(9);
38+
else if (*it->p == 'n')
39+
APPEND(10);
40+
else if (*it->p == 'f')
41+
APPEND(12);
42+
else if (*it->p == 'r')
43+
APPEND(13);
44+
else if (*it->p == 'u') {
45+
unsigned char binary[2];
46+
int i;
47+
48+
if (hex_to_bytes(binary, it->p + 1, 2) < 0) {
49+
error("invalid: '%.*s'", 6, it->p - 1);
50+
return reset_iterator(it);
51+
}
52+
it->p += 4;
53+
54+
i = (binary[0] << 8) | binary[1];
55+
if (i < 0x80)
56+
APPEND(i);
57+
else if (i < 0x0800) {
58+
APPEND(0xc0 | ((i >> 6) & 0x1f));
59+
APPEND(0x80 | (i & 0x3f));
60+
} else if (i < 0x10000) {
61+
APPEND(0xe0 | ((i >> 12) & 0x0f));
62+
APPEND(0x80 | ((i >> 6) & 0x3f));
63+
APPEND(0x80 | (i & 0x3f));
64+
} else {
65+
APPEND(0xf0 | ((i >> 18) & 0x07));
66+
APPEND(0x80 | ((i >> 12) & 0x3f));
67+
APPEND(0x80 | ((i >> 6) & 0x3f));
68+
APPEND(0x80 | (i & 0x3f));
69+
}
70+
}
71+
break;
72+
default:
73+
APPEND(*it->p);
74+
}
75+
it->p++;
76+
}
77+
78+
it->end = it->p++;
79+
return 0;
80+
}
81+
82+
static void skip_whitespace(struct json_iterator *it)
83+
{
84+
while (isspace(*it->p))
85+
it->p++;
86+
}
87+
88+
int iterate_json(struct json_iterator *it)
89+
{
90+
skip_whitespace(it);
91+
it->begin = it->p;
92+
93+
switch (*it->p) {
94+
case '\0':
95+
reset_iterator(it);
96+
return 0;
97+
case 'n':
98+
if (!starts_with(it->p, "null")) {
99+
error("unexpected value: %.*s", 4, it->p);
100+
return reset_iterator(it);
101+
}
102+
it->type = JSON_NULL;
103+
it->end = it->p = it->begin + 4;
104+
break;
105+
case 't':
106+
if (!starts_with(it->p, "true")) {
107+
error("unexpected value: %.*s", 4, it->p);
108+
return reset_iterator(it);
109+
}
110+
it->type = JSON_TRUE;
111+
it->end = it->p = it->begin + 4;
112+
break;
113+
case 'f':
114+
if (!starts_with(it->p, "false")) {
115+
error("unexpected value: %.*s", 5, it->p);
116+
return reset_iterator(it);
117+
}
118+
it->type = JSON_FALSE;
119+
it->end = it->p = it->begin + 5;
120+
break;
121+
case '-': case '.':
122+
case '0': case '1': case '2': case '3': case '4':
123+
case '5': case '6': case '7': case '8': case '9':
124+
it->type = JSON_NUMBER;
125+
it->end = it->p = it->begin + strspn(it->p, "-.0123456789");
126+
break;
127+
case '"':
128+
it->type = JSON_STRING;
129+
if (parse_json_string(it, &it->string_value) < 0)
130+
return -1;
131+
break;
132+
case '[': {
133+
const char *save = it->begin;
134+
size_t key_offset = it->key.len;
135+
int i = 0, res;
136+
137+
for (it->p++, skip_whitespace(it); *it->p != ']'; i++) {
138+
strbuf_addf(&it->key, "[%d]", i);
139+
140+
if ((res = iterate_json(it))) {
141+
reset_iterator(it);
142+
return res;
143+
}
144+
strbuf_setlen(&it->key, key_offset);
145+
146+
skip_whitespace(it);
147+
if (*it->p == ',')
148+
it->p++;
149+
}
150+
151+
it->type = JSON_ARRAY;
152+
it->begin = save;
153+
it->end = it->p;
154+
it->p++;
155+
break;
156+
}
157+
case '{': {
158+
const char *save = it->begin;
159+
size_t key_offset = it->key.len;
160+
int res;
161+
162+
strbuf_addch(&it->key, '.');
163+
for (it->p++, skip_whitespace(it); *it->p != '}'; ) {
164+
strbuf_setlen(&it->key, key_offset + 1);
165+
if (parse_json_string(it, &it->key) < 0)
166+
return -1;
167+
skip_whitespace(it);
168+
if (*(it->p)++ != ':') {
169+
error("expected colon: %.*s", 5, it->p);
170+
return reset_iterator(it);
171+
}
172+
173+
if ((res = iterate_json(it)))
174+
return res;
175+
176+
skip_whitespace(it);
177+
if (*it->p == ',')
178+
it->p++;
179+
}
180+
strbuf_setlen(&it->key, key_offset);
181+
182+
it->type = JSON_OBJECT;
183+
it->begin = save;
184+
it->end = it->p;
185+
it->p++;
186+
break;
187+
}
188+
}
189+
190+
return it->fn(it);
191+
}

json-parser.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef JSON_PARSER_H
2+
#define JSON_PARSER_H
3+
4+
#include "strbuf.h"
5+
6+
struct json_iterator {
7+
const char *json, *p, *begin, *end;
8+
struct strbuf key, string_value;
9+
enum {
10+
JSON_NULL = 0,
11+
JSON_FALSE,
12+
JSON_TRUE,
13+
JSON_NUMBER,
14+
JSON_STRING,
15+
JSON_ARRAY,
16+
JSON_OBJECT
17+
} type;
18+
int (*fn)(struct json_iterator *it);
19+
void *fn_data;
20+
};
21+
#define JSON_ITERATOR_INIT(json_, fn_, fn_data_) { \
22+
.json = json_, .p = json_, \
23+
.key = STRBUF_INIT, .string_value = STRBUF_INIT, \
24+
.fn = fn_, .fn_data = fn_data_ \
25+
}
26+
27+
int iterate_json(struct json_iterator *it);
28+
/* Releases the iterator, always returns -1 */
29+
int reset_iterator(struct json_iterator *it);
30+
31+
#endif

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ test_dependencies += executable('git-http-backend',
18101810
)
18111811

18121812
bin_wrappers += executable('scalar',
1813-
sources: 'scalar.c',
1813+
sources: ['scalar.c', 'json-parser.c'],
18141814
dependencies: [libgit_commonmain],
18151815
install: true,
18161816
install_dir: git_exec_path,

0 commit comments

Comments
 (0)