diff mbox series

[nft,3/6] json: Print single fib flag as non-array

Message ID 20250508214722.20808-4-phil@nwl.cc
State New
Headers show
Series [nft] parser_json: Introduce parse_flags_array() | expand

Commit Message

Phil Sutter May 8, 2025, 9:47 p.m. UTC
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 | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/src/json.c b/src/json.c
index 6b27ccb927017..a8b0abeba6396 100644
--- a/src/json.c
+++ b/src/json.c
@@ -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);
 }