From: Lennart Poettering Date: Mon, 4 Dec 2023 17:10:02 +0000 (+0100) Subject: json: add new dispatch flag JSON_ALLOW_EXTENSIONS X-Git-Tag: v256-rc1~1265^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a617fd904789cd3a05cf4cb2f54649e2a1f73d33;p=thirdparty%2Fsystemd.git json: add new dispatch flag JSON_ALLOW_EXTENSIONS This is a subset of JSON_PERMISSIVE focussed on allowing parsing of varlink replies that get extended, i.e. gain new fields, without allowing more than that (i.e. without allowing missing fields, or bad field types or such). --- diff --git a/src/shared/json.c b/src/shared/json.c index 19e8e0c77ef..aeb1b36af35 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -4615,8 +4615,12 @@ int json_dispatch_full( done++; } else { - json_log(value, flags, 0, "Unexpected object field '%s'.", json_variant_string(key)); + if (flags & JSON_ALLOW_EXTENSIONS) { + json_log(value, flags, 0, "Unrecognized object field '%s', assuming extension.", json_variant_string(key)); + continue; + } + json_log(value, flags, 0, "Unexpected object field '%s'.", json_variant_string(key)); if (flags & JSON_PERMISSIVE) continue; diff --git a/src/shared/json.h b/src/shared/json.h index 975cf562a96..8fb7c1be78f 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -376,15 +376,16 @@ int json_buildv(JsonVariant **ret, va_list ap); * entry, as well the bitmask specified for json_log() calls */ typedef enum JsonDispatchFlags { /* The following three may be set in JsonDispatch's .flags field or the json_dispatch() flags parameter */ - JSON_PERMISSIVE = 1 << 0, /* Shall parsing errors be considered fatal for this property? */ - JSON_MANDATORY = 1 << 1, /* Should existence of this property be mandatory? */ - JSON_LOG = 1 << 2, /* Should the parser log about errors? */ - JSON_SAFE = 1 << 3, /* Don't accept "unsafe" strings in json_dispatch_string() + json_dispatch_string() */ - JSON_RELAX = 1 << 4, /* Use relaxed user name checking in json_dispatch_user_group_name */ + JSON_PERMISSIVE = 1 << 0, /* Shall parsing errors be considered fatal for this field or object? */ + JSON_MANDATORY = 1 << 1, /* Should existence of this property be mandatory? */ + JSON_LOG = 1 << 2, /* Should the parser log about errors? */ + JSON_SAFE = 1 << 3, /* Don't accept "unsafe" strings in json_dispatch_string() + json_dispatch_string() */ + JSON_RELAX = 1 << 4, /* Use relaxed user name checking in json_dispatch_user_group_name */ + JSON_ALLOW_EXTENSIONS = 1 << 5, /* Subset of JSON_PERMISSIVE: allow additional fields, but no other permissive handling */ /* The following two may be passed into log_json() in addition to those above */ - JSON_DEBUG = 1 << 5, /* Indicates that this log message is a debug message */ - JSON_WARNING = 1 << 6, /* Indicates that this log message is a warning message */ + JSON_DEBUG = 1 << 6, /* Indicates that this log message is a debug message */ + JSON_WARNING = 1 << 7, /* Indicates that this log message is a warning message */ } JsonDispatchFlags; typedef int (*JsonDispatchCallback)(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);