From b0eeb945881b86f6740b84c055f0ad2be7a09ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 17 Nov 2022 14:15:48 +0100 Subject: shared/json: make it possible to specify source name for strings too, add tests The source would be set implicitly when parsing from a named file. But it's useful to specify the source also for cases where we're parsing a ready string. I noticed the lack of this API when trying to write tests, but it seems generally useful to be specify a source name when parsing things. --- src/shared/json.c | 58 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 14 deletions(-) (limited to 'src/shared/json.c') diff --git a/src/shared/json.c b/src/shared/json.c index 94d7b31557..98bae0f194 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -3180,16 +3180,53 @@ finish: return r; } -int json_parse(const char *input, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { - return json_parse_internal(&input, NULL, flags, ret, ret_line, ret_column, false); +int json_parse_with_source( + const char *input, + const char *source, + JsonParseFlags flags, + JsonVariant **ret, + unsigned *ret_line, + unsigned *ret_column) { + + _cleanup_(json_source_unrefp) JsonSource *s = NULL; + + if (source) { + s = json_source_new(source); + if (!s) + return -ENOMEM; + } + + return json_parse_internal(&input, s, flags, ret, ret_line, ret_column, false); } -int json_parse_continue(const char **p, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { - return json_parse_internal(p, NULL, flags, ret, ret_line, ret_column, true); +int json_parse_with_source_continue( + const char **p, + const char *source, + JsonParseFlags flags, + JsonVariant **ret, + unsigned *ret_line, + unsigned *ret_column) { + + _cleanup_(json_source_unrefp) JsonSource *s = NULL; + + if (source) { + s = json_source_new(source); + if (!s) + return -ENOMEM; + } + + return json_parse_internal(p, s, flags, ret, ret_line, ret_column, true); } -int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { - _cleanup_(json_source_unrefp) JsonSource *source = NULL; +int json_parse_file_at( + FILE *f, + int dir_fd, + const char *path, + JsonParseFlags flags, + JsonVariant **ret, + unsigned *ret_line, + unsigned *ret_column) { + _cleanup_free_ char *text = NULL; int r; @@ -3205,14 +3242,7 @@ int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonParseFlags fla if (isempty(text)) return -ENODATA; - if (path) { - source = json_source_new(path); - if (!source) - return -ENOMEM; - } - - const char *p = text; - return json_parse_internal(&p, source, flags, ret, ret_line, ret_column, false); + return json_parse_with_source(text, path, flags, ret, ret_line, ret_column); } int json_buildv(JsonVariant **ret, va_list ap) { -- cgit v1.2.1