From a596c41426566f54078f854e138e73a88252e7b9 Mon Sep 17 00:00:00 2001 From: Ana Rey Date: Wed, 4 Jun 2014 13:03:31 +0200 Subject: expr: log: Do not print unset values in json It changes the parse and the snprint functions to omit unset values. Also, It fixes an unnecessary comma after key-value pair type. This comma is not necessary if there is not more key-value pairs in this expr. Example: "expr":[{"type":"log"}] If It uses this rule: nft add rule ip test output log It gets this json file: [...] {"expr":[{"type":"log","prefix":"(null)","group":0,"snaplen":0,"qthreshold":0}]} [...] Now, That rule creates this json file without null values: {"expr":[{"type":"log"}]} Signed-off-by: Ana Rey Signed-off-by: Pablo Neira Ayuso --- src/rule.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/rule.c') diff --git a/src/rule.c b/src/rule.c index ac88abb..88e9f71 100644 --- a/src/rule.c +++ b/src/rule.c @@ -816,6 +816,15 @@ static int nft_rule_snprintf_json(char *buf, size_t size, struct nft_rule *r, ret = expr->ops->snprintf(buf+offset, len, type, flags, expr); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + /* + * Remove comma from the first element if there is type + * key-value pair only. Example: "expr":[{"type":"log"}] + */ + if (ret == 0) { + offset--; + len--; + } + ret = snprintf(buf+offset, len, "},"); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); -- cgit v1.2.3