Skip to content

Commit

Permalink
lib: json-parser: Add support for skipping a value that is already pa…
Browse files Browse the repository at this point in the history
…rtially parsed.

Adds json_parse_skip(), which skips the remainder of the value parsed earlier by
json_parse_next(). This is needed when values need to be skipped by their value
type rather than their object member key.
  • Loading branch information
stephanbosch authored and sirainen committed Mar 4, 2019
1 parent f3020c1 commit b8c1ea7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib/json-parser.c
Expand Up @@ -672,6 +672,24 @@ void json_parse_skip_next(struct json_parser *parser)
parser->state = JSON_STATE_ARRAY_NEXT_SKIP;
}

void json_parse_skip(struct json_parser *parser)
{
i_assert(!parser->skipping);
i_assert(parser->strinput == NULL);
i_assert(parser->state == JSON_STATE_OBJECT_NEXT ||
parser->state == JSON_STATE_OBJECT_OPEN ||
parser->state == JSON_STATE_ARRAY_NEXT ||
parser->state == JSON_STATE_ARRAY_OPEN);

if (parser->state == JSON_STATE_OBJECT_OPEN ||
parser->state == JSON_STATE_ARRAY_OPEN)
parser->nested_skip_count++;

parser->skipping = TRUE;
if (parser->state == JSON_STATE_ARRAY_NEXT)
parser->state = JSON_STATE_ARRAY_NEXT_SKIP;
}

static void json_strinput_destroyed(struct json_parser *parser)
{
i_assert(parser->strinput != NULL);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/json-parser.h
Expand Up @@ -42,6 +42,8 @@ int json_parse_next(struct json_parser *parser, enum json_type *type_r,
/* Skip the next object value. If it's an object, its members are also
skipped. */
void json_parse_skip_next(struct json_parser *parser);
/* Skip the remainder of the value parsed earlier by json_parse_next(). */
void json_parse_skip(struct json_parser *parser);
/* Return the following string as input stream. Returns 1 if ok, 0 if
input stream is non-blocking and needs more input, -1 if the next token
isn't a string (call json_parse_next()). */
Expand Down

0 comments on commit b8c1ea7

Please sign in to comment.