diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-11-17 14:15:48 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-12-01 18:02:06 +0100 |
commit | b0eeb945881b86f6740b84c055f0ad2be7a09ad1 (patch) | |
tree | 5dcfff24a6a935ed6aa9ff390feed888a6c114ab /src/shared/json.c | |
parent | 50f2c32cd5477bb42bf9a26e4aefd7230c2574c7 (diff) | |
download | systemd-b0eeb945881b86f6740b84c055f0ad2be7a09ad1.tar.gz |
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.
Diffstat (limited to 'src/shared/json.c')
-rw-r--r-- | src/shared/json.c | 58 |
1 files changed, 44 insertions, 14 deletions
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) { |