diff mbox

[libnftnl,4/4] expr: match: Do not print unset values in json file

Message ID 1403630104-13118-5-git-send-email-anarey@gmail.com
State Accepted
Headers show

Commit Message

Ana Rey June 24, 2014, 5:15 p.m. UTC
It changes the parse and snprintf functions json to omit unset values.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/expr/match.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
diff mbox

Patch

diff --git a/src/expr/match.c b/src/expr/match.c
index 604a8e6..2c4b3dc 100644
--- a/src/expr/match.c
+++ b/src/expr/match.c
@@ -177,10 +177,8 @@  static int nft_rule_expr_match_json_parse(struct nft_rule_expr *e, json_t *root,
 	const char *name;
 
 	name = nft_jansson_parse_str(root, "name", err);
-	if (name == NULL)
-		return -1;
-
-	nft_rule_expr_set_str(e, NFT_EXPR_MT_NAME, name);
+	if (name != NULL)
+		nft_rule_expr_set_str(e, NFT_EXPR_MT_NAME, name);
 
 	return 0;
 #else
@@ -216,9 +214,10 @@  static int nft_rule_expr_match_snprintf_json(char *buf, size_t len,
 	struct nft_expr_match *mt = nft_expr_data(e);
 	int ret, size = len, offset = 0;
 
-	ret = snprintf(buf, len, "\"name\":\"%s\"",
-				mt->name);
-	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	if (e->flags & (1 << NFT_EXPR_MT_NAME)) {
+		ret = snprintf(buf, len, "\"name\":\"%s\"", mt->name);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
 
 	return offset;
 }