From 9febdf333c3720e1e28811a0cd28c980444e9ad3 Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Sun, 29 May 2011 12:53:25 +0300 Subject: [PATCH] Reduce code duplication in the decoder --- src/load.c | 48 ++++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/src/load.c b/src/load.c index 98837a4..3dc8ac0 100644 --- a/src/load.c +++ b/src/load.c @@ -815,13 +815,26 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error) static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error) { + json_t *result; + lex_scan(lex, error); if(lex->token != '[' && lex->token != '{') { error_set(error, lex, "'[' or '{' expected"); return NULL; } - return parse_value(lex, flags, error); + result = parse_value(lex, flags, error); + if(!result) + return NULL; + + lex_scan(lex, error); + if(lex->token != TOKEN_EOF) { + error_set(error, lex, "end of file expected"); + json_decref(result); + result = NULL; + } + + return result; } typedef struct @@ -857,19 +870,8 @@ json_t *json_loads(const char *string, size_t flags, json_error_t *error) return NULL; jsonp_error_init(error, ""); - result = parse_json(&lex, flags, error); - if(!result) - goto out; - lex_scan(&lex, error); - if(lex.token != TOKEN_EOF) { - error_set(error, &lex, "end of file expected"); - json_decref(result); - result = NULL; - } - -out: lex_close(&lex); return result; } @@ -907,19 +909,8 @@ json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t return NULL; jsonp_error_init(error, ""); - result = parse_json(&lex, flags, error); - if(!result) - goto out; - lex_scan(&lex, error); - if(lex.token != TOKEN_EOF) { - error_set(error, &lex, "end of file expected"); - json_decref(result); - result = NULL; - } - -out: lex_close(&lex); return result; } @@ -939,19 +930,8 @@ json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) source = ""; jsonp_error_init(error, source); - result = parse_json(&lex, flags, error); - if(!result) - goto out; - lex_scan(&lex, error); - if(lex.token != TOKEN_EOF) { - error_set(error, &lex, "end of file expected"); - json_decref(result); - result = NULL; - } - -out: lex_close(&lex); return result; }