From: Andrea Marchesini Date: Fri, 11 Nov 2011 18:17:29 +0000 (+0100) Subject: JSON_DECODE_ANY X-Git-Tag: v2.4-moonshot~1^2~51 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=jansson.git;a=commitdiff_plain;h=1e36667193fe51a7a886f9b2560e83ca6765df4b JSON_DECODE_ANY Closes GH-4. --- diff --git a/doc/apiref.rst b/doc/apiref.rst index 7d69004..042f78f 100644 --- a/doc/apiref.rst +++ b/doc/apiref.rst @@ -828,6 +828,16 @@ macros can be ORed together to obtain *flags*. .. versionadded:: 2.1 +``JSON_DECODE_ANY`` + By default, the decoder expects that its whole input constitutes a + valid JSON array or a valid JSON object. With this flag enabled, + the decoder accepts any valid JSON value. + This can be incompatible with the JSON_DISABLE_EOF_CHECK flag, + because the decoder may read up to 4 extra bytes from the input + (one utf-8 encoded character). + + .. versionadded:: 2.3 + The following functions perform the actual JSON decoding. .. function:: json_t *json_loads(const char *input, size_t flags, json_error_t *error) diff --git a/src/jansson.h b/src/jansson.h index 405c840..ca9ac11 100644 --- a/src/jansson.h +++ b/src/jansson.h @@ -222,6 +222,7 @@ json_t *json_deep_copy(json_t *value); #define JSON_REJECT_DUPLICATES 0x1 #define JSON_DISABLE_EOF_CHECK 0x2 +#define JSON_DECODE_ANY 0x4 json_t *json_loads(const char *input, size_t flags, json_error_t *error); json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error); diff --git a/src/load.c b/src/load.c index d60a93e..ef65f0f 100644 --- a/src/load.c +++ b/src/load.c @@ -820,9 +820,11 @@ 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; + if(!(flags & JSON_DECODE_ANY)) { + if(lex->token != '[' && lex->token != '{') { + error_set(error, lex, "'[' or '{' expected"); + return NULL; + } } result = parse_value(lex, flags, error); diff --git a/test/suites/api/test_load.c b/test/suites/api/test_load.c index d60869d..fb4c734 100644 --- a/test/suites/api/test_load.c +++ b/test/suites/api/test_load.c @@ -50,9 +50,36 @@ static void disable_eof_check() json_decref(json); } +static void decode_any() +{ + json_t *json; + json_error_t error; + + json = json_loads("\"foo\"", JSON_DECODE_ANY, &error); + if (!json || !json_is_string(json)) + fail("json_load decoded any failed - string"); + json_decref(json); + + json = json_loads("42", JSON_DECODE_ANY, &error); + if (!json || !json_is_integer(json)) + fail("json_load decoded any failed - integer"); + json_decref(json); + + json = json_loads("true", JSON_DECODE_ANY, &error); + if (!json || !json_is_true(json)) + fail("json_load decoded any failed - boolean"); + json_decref(json); + + json = json_loads("null", JSON_DECODE_ANY, &error); + if (!json || !json_is_null(json)) + fail("json_load decoded any failed - null"); + json_decref(json); +} + static void run_tests() { file_not_found(); reject_duplicates(); disable_eof_check(); + decode_any(); }