Skip to content

Commit 5a5e189

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 <[email protected]>
1 parent 63dfa6e commit 5a5e189

File tree

5 files changed

+218
-3
lines changed

5 files changed

+218
-3
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,7 @@ GIT_OBJS += git.o
27872787
.PHONY: git-objs
27882788
git-objs: $(GIT_OBJS)
27892789

2790+
SCALAR_OBJS := json-parser.o
27902791
SCALAR_OBJS += scalar.o
27912792
.PHONY: scalar-objs
27922793
scalar-objs: $(SCALAR_OBJS)
@@ -2938,7 +2939,7 @@ $(REMOTE_CURL_PRIMARY): remote-curl.o http.o http-walker.o $(LAZYLOAD_LIBCURL_OB
29382939
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
29392940
$(CURL_LIBCURL) $(EXPAT_LIBEXPAT) $(LIBS)
29402941

2941-
scalar$X: scalar.o GIT-LDFLAGS $(GITLIBS)
2942+
scalar$X: $(SCALAR_OBJS) GIT-LDFLAGS $(GITLIBS)
29422943
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
29432944
$(filter %.o,$^) $(LIBS)
29442945

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

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
@@ -1609,7 +1609,7 @@ test_dependencies += executable('git-http-backend',
16091609
)
16101610

16111611
bin_wrappers += executable('scalar',
1612-
sources: 'scalar.c',
1612+
sources: ['scalar.c', 'json-parser.c'],
16131613
dependencies: [libgit, common_main],
16141614
install: true,
16151615
install_dir: get_option('libexecdir') / 'git-core',

0 commit comments

Comments
 (0)