]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: If json-parser has JSON_PARSER_NO_ROOT_OBJECT set, return the ending root "...
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 4 Feb 2016 11:31:59 +0000 (13:31 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 4 Feb 2016 11:31:59 +0000 (13:31 +0200)
src/lib/json-parser.c
src/lib/test-json-parser.c

index 1ff8c05340b377e1a01e2b7270bfc3e108db62e7..a92a5ae12b720da27c22914622bf20ce5182c9b5 100644 (file)
@@ -28,6 +28,7 @@ enum json_state {
 struct json_parser {
        struct istream *input;
        uoff_t highwater_offset;
+       enum json_parser_flags flags;
 
        const unsigned char *start, *end, *data;
        const char *error;
@@ -111,6 +112,7 @@ struct json_parser *json_parser_init_flags(struct istream *input,
 
        parser = i_new(struct json_parser, 1);
        parser->input = input;
+       parser->flags = flags;
        parser->value = str_new(default_pool, 128);
        i_array_init(&parser->nesting, 8);
        i_stream_ref(input);
@@ -356,7 +358,10 @@ static int json_parse_denest(struct json_parser *parser)
        if (count == 1) {
                /* closing root */
                parser->state = JSON_STATE_DONE;
-               return 0;
+               if ((parser->flags & JSON_PARSER_NO_ROOT_OBJECT) == 0)
+                       return 0;
+               /* we want to return the ending "]" or "}" to caller */
+               return 1;
        }
 
        /* closing a nested object */
index ab86c2e268cd1676d92f5e0bb057c454900b6be2..33403404bba3c85b0f98686bb2573da0e9745682 100644 (file)
@@ -184,21 +184,24 @@ test_json_parse_input(const char *test_input, enum json_parser_flags flags)
 
 static void test_json_parser_primitive_values(void)
 {
-       static const char *test_inputs[] = {
-               "\"hello\"",
-               "null",
-               "1234",
-               "1234.1234",
-               "{}",
-               "[]",
-               "true",
-               "false"
+       static struct {
+               const char *str;
+               int ret;
+       } test_inputs[] = {
+               { "\"hello\"", 1 },
+               { "null", 1 },
+               { "1234", 1 },
+               { "1234.1234", 1 },
+               { "{}", 2 },
+               { "[]", 2 },
+               { "true", 1 },
+               { "false", 1 }
        };
        unsigned int i;
 
        test_begin("json_parser (primitives)");
        for (i = 0; i < N_ELEMENTS(test_inputs); i++)
-               test_assert_idx(test_json_parse_input(test_inputs[i], JSON_PARSER_NO_ROOT_OBJECT) == 1, i);
+               test_assert_idx(test_json_parse_input(test_inputs[i].str, JSON_PARSER_NO_ROOT_OBJECT) == test_inputs[i].ret, i);
        test_end();
 }