From 46977858226905d6c0eb3826b1be65cb4d92284f Mon Sep 17 00:00:00 2001 From: Arion Date: Thu, 15 Mar 2018 22:55:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorial01/leptjson.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tutorial01/leptjson.c b/tutorial01/leptjson.c index 5299fe1d..edcacd00 100644 --- a/tutorial01/leptjson.c +++ b/tutorial01/leptjson.c @@ -1,6 +1,7 @@ #include "leptjson.h" #include /* assert() */ #include /* NULL */ +#include #define EXPECT(c, ch) do { assert(*c->json == (ch)); c->json++; } while(0) @@ -24,8 +25,28 @@ static int lept_parse_null(lept_context* c, lept_value* v) { return LEPT_PARSE_OK; } +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 LEPT_PARSE_OK; +} + +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 LEPT_PARSE_OK; +} + static int lept_parse_value(lept_context* c, lept_value* v) { switch (*c->json) { + case 't': return lept_parse_true(c, v); + case 'f': return lept_parse_false(c, v); case 'n': return lept_parse_null(c, v); case '\0': return LEPT_PARSE_EXPECT_VALUE; default: return LEPT_PARSE_INVALID_VALUE; @@ -38,7 +59,13 @@ 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 ret; + if ((ret = lept_parse_value(&c, v)) == LEPT_PARSE_OK) { + lept_parse_whitespace(&c); + if (*c.json != 0) + ret = LEPT_PARSE_ROOT_NOT_SINGULAR; + } + return ret; } lept_type lept_get_type(const lept_value* v) {