Skip to content

Commit a3d686c

Browse files
dschomjcheetham
authored andcommitted
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 ec19278 commit a3d686c

File tree

5 files changed

+218
-3
lines changed

5 files changed

+218
-3
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,7 @@ GIT_OBJS += git.o
28162816
.PHONY: git-objs
28172817
git-objs: $(GIT_OBJS)
28182818

2819+
SCALAR_OBJS := json-parser.o
28192820
SCALAR_OBJS += scalar.o
28202821
.PHONY: scalar-objs
28212822
scalar-objs: $(SCALAR_OBJS)
@@ -2971,7 +2972,7 @@ $(REMOTE_CURL_PRIMARY): remote-curl.o http.o http-walker.o $(LAZYLOAD_LIBCURL_OB
29712972
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
29722973
$(CURL_LIBCURL) $(EXPAT_LIBEXPAT) $(LIBS)
29732974

2974-
scalar$X: scalar.o GIT-LDFLAGS $(GITLIBS)
2975+
scalar$X: $(SCALAR_OBJS) GIT-LDFLAGS $(GITLIBS)
29752976
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
29762977
$(filter %.o,$^) $(LIBS)
29772978

contrib/buildsystems/CMakeLists.txt

+1-1
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

+183
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

+31
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ test_dependencies += executable('git-http-backend',
16891689
)
16901690

16911691
bin_wrappers += executable('scalar',
1692-
sources: 'scalar.c',
1692+
sources: ['scalar.c', 'json-parser.c'],
16931693
dependencies: [libgit_commonmain],
16941694
install: true,
16951695
install_dir: get_option('libexecdir') / 'git-core',

0 commit comments

Comments
 (0)