]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
json: Print single fib flag as non-array
authorPhil Sutter <phil@nwl.cc>
Thu, 8 May 2025 14:40:41 +0000 (16:40 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 13 May 2025 08:48:53 +0000 (10:48 +0200)
Check array size and reduce the array if possible.

The zero array length check is dead code here due to the surrounding 'if
(flags)' block, but it's a common idiom one could replace by a shared
routine later.

Signed-off-by: Phil Sutter <phil@nwl.cc>
src/json.c

index 6b27ccb9270177f4af507c6f1694d16d7b749bd8..a8b0abeba6396f7d10750c19e79ca1c4c69896f6 100644 (file)
@@ -939,7 +939,15 @@ json_t *fib_expr_json(const struct expr *expr, struct output_ctx *octx)
                }
                if (flags)
                        json_array_append_new(tmp, json_integer(flags));
-               json_object_set_new(root, "flags", tmp);
+
+               if (json_array_size(tmp) > 1) {
+                       json_object_set_new(root, "flags", tmp);
+               } else {
+                       if (json_array_size(tmp))
+                               json_object_set(root, "flags",
+                                               json_array_get(tmp, 0));
+                       json_decref(tmp);
+               }
        }
        return json_pack("{s:o}", "fib", root);
 }