Skip to content

Commit 44673d7

Browse files
committed
updata code
1 parent 645d80e commit 44673d7

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

tutorial01/leptjson.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ static void lept_parse_whitespace(lept_context* c) {
1515
c->json = p;
1616
}
1717

18+
static int lept_parse_true(lept_context* c, lept_value* v) {
19+
EXPECT(c, 't');
20+
if (c->json[0] != 'r' || c->json[1] != 'u' || c->json[2] != 'e')
21+
return LEPT_PARSE_INVALID_VALUE;
22+
c->json += 3;
23+
v->type = LEPT_TRUE;
24+
return LEPT_PARSE_OK;
25+
}
26+
27+
static int lept_parse_false(lept_context* c, lept_value* v) {
28+
EXPECT(c, 'f');
29+
if (c->json[0] != 'a' || c->json[1] != 'l' || c->json[2] != 's' || c->json[3] != 'e')
30+
return LEPT_PARSE_INVALID_VALUE;
31+
c->json += 4;
32+
v->type = LEPT_FALSE;
33+
return LEPT_PARSE_OK;
34+
}
35+
1836
static int lept_parse_null(lept_context* c, lept_value* v) {
1937
EXPECT(c, 'n');
2038
if (c->json[0] != 'u' || c->json[1] != 'l' || c->json[2] != 'l')
@@ -27,18 +45,26 @@ static int lept_parse_null(lept_context* c, lept_value* v) {
2745
static int lept_parse_value(lept_context* c, lept_value* v) {
2846
switch (*c->json) {
2947
case 'n': return lept_parse_null(c, v);
48+
case 't': return lept_parse_true(c, v);
49+
case 'f': return lept_parse_false(c, v);
3050
case '\0': return LEPT_PARSE_EXPECT_VALUE;
3151
default: return LEPT_PARSE_INVALID_VALUE;
3252
}
3353
}
3454

3555
int lept_parse(lept_value* v, const char* json) {
3656
lept_context c;
57+
int ret;
3758
assert(v != NULL);
3859
c.json = json;
3960
v->type = LEPT_NULL;
4061
lept_parse_whitespace(&c);
41-
return lept_parse_value(&c, v);
62+
if ((ret = lept_parse_value(&c, v)) == LEPT_PARSE_OK) {
63+
lept_parse_whitespace(&c);
64+
if (*c.json != 0)
65+
ret = LEPT_PARSE_ROOT_NOT_SINGULAR;
66+
}
67+
return ret;
4268
}
4369

4470
lept_type lept_get_type(const lept_value* v) {

tutorial01/test.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ static int test_pass = 0;
2020

2121
#define EXPECT_EQ_INT(expect, actual) EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%d")
2222

23+
static void test_parse_true() {
24+
lept_value v;
25+
v.type = LEPT_NULL;
26+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "true"));
27+
EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(&v));
28+
}
29+
30+
static void test_parse_false() {
31+
lept_value v;
32+
v.type = LEPT_NULL;
33+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "false"));
34+
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v));
35+
}
36+
2337
static void test_parse_null() {
2438
lept_value v;
2539
v.type = LEPT_FALSE;

0 commit comments

Comments
 (0)