From e787b211a5aa9c06cb319f50479665d43a8f426e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 4 Jul 2019 17:38:17 +0200 Subject: [PATCH] json: add new json_variant_is_blank_{object,array}() helpers --- src/shared/json.c | 13 +++++++++++++ src/shared/json.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/shared/json.c b/src/shared/json.c index c3efb11bbbc..84ad6dd02ba 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -946,6 +946,19 @@ mismatch: return false; } +bool json_variant_is_blank_object(JsonVariant *v) { + /* Returns true if the specified object is null or empty */ + return !v || + json_variant_is_null(v) || + (json_variant_is_object(v) && json_variant_elements(v) == 0); +} + +bool json_variant_is_blank_array(JsonVariant *v) { + return !v || + json_variant_is_null(v) || + (json_variant_is_array(v) && json_variant_elements(v) == 0); +} + JsonVariantType json_variant_type(JsonVariant *v) { if (!v) diff --git a/src/shared/json.h b/src/shared/json.h index 1f9c620ebb8..59962049d70 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -120,6 +120,8 @@ static inline bool json_variant_is_null(JsonVariant *v) { } bool json_variant_is_negative(JsonVariant *v); +bool json_variant_is_blank_object(JsonVariant *v); +bool json_variant_is_blank_array(JsonVariant *v); size_t json_variant_elements(JsonVariant *v); JsonVariant *json_variant_by_index(JsonVariant *v, size_t index);