diff --git a/tutorial01/leptjson.c b/tutorial01/leptjson.c index a68718b8..29eaa9be 100644 --- a/tutorial01/leptjson.c +++ b/tutorial01/leptjson.c @@ -24,9 +24,29 @@ static int lept_parse_null(lept_context* c, lept_value* v) { return 0; } +static int lept_parse_true(lept_context* c, lept_value* v) { + EXPECT(c, 't'); + if (c->json[0] != 'r' || c->json[1] != 'u' || c->json[2] != 'e') + return LEPT_PARSE_INVALID_VALUE; + c->json += 3; + v->type = LEPT_TRUE; + return 0; +} + +static int lept_parse_false(lept_context* c, lept_value* v) { + EXPECT(c, 'f'); + if (c->json[0] != 'a' || c->json[1] != 'l' || c->json[2] != 's' || c->json[3] != 'e') + return LEPT_PARSE_INVALID_VALUE; + c->json += 4; + v->type = LEPT_FALSE; + return 0; +} + static int lept_parse_value(lept_context* c, lept_value* v) { switch (*c->json) { case 'n': return lept_parse_null(c, v); + case 't': return lept_parse_true(c, v); + case 'f': return lept_parse_false(c, v); case '\0': return LEPT_PARSE_EXPECT_VALUE; default: return LEPT_PARSE_INVALID_VALUE; } @@ -38,7 +58,16 @@ int lept_parse(lept_value* v, const char* json) { c.json = json; v->type = LEPT_NULL; lept_parse_whitespace(&c); - return lept_parse_value(&c, v); + int rtn = lept_parse_value(&c, v); + if (rtn != 0) + return rtn; + + lept_parse_whitespace(&c); + if (lept_parse_value(&c, v) != LEPT_PARSE_EXPECT_VALUE) + { + return LEPT_PARSE_ROOT_NOT_SINGULAR; + } + return 0; } lept_type lept_get_type(const lept_value* v) { diff --git a/tutorial01/test.c b/tutorial01/test.c index e7672181..cbadb8f9 100644 --- a/tutorial01/test.c +++ b/tutorial01/test.c @@ -27,6 +27,19 @@ static void test_parse_null() { EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v)); } +static void test_parse_true() { + lept_value v; + v.type = LEPT_NULL; + EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "true")); + EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(&v)); +} + +static void test_parse_false() { + lept_value v; + v.type = LEPT_NULL; + EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "false")); + EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v)); +} static void test_parse_expect_value() { lept_value v; @@ -62,6 +75,8 @@ static void test_parse() { test_parse_expect_value(); test_parse_invalid_value(); test_parse_root_not_singular(); + test_parse_true(); + test_parse_false(); } int main() {