]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: add JSON printfrr dict-key helper
authorDavid Lamparter <equinox@opensourcerouting.org>
Fri, 11 Mar 2022 10:42:17 +0000 (11:42 +0100)
committerDavid Lamparter <equinox@opensourcerouting.org>
Fri, 11 Mar 2022 12:43:00 +0000 (13:43 +0100)
`json_object_object_add()` adds keys/items to objects/dictionaries.
Useful to have a printfrr based variant for the key there.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/json.c
lib/json.h

index 854a3d59d1a86819920ffd91a1a9b325fe7e231c..d85a21215c4c96679d1a375f5f9dfdafd3ad4acd 100644 (file)
@@ -74,6 +74,19 @@ void json_object_string_addv(struct json_object *obj, const char *key,
        json_object_object_add(obj, key, json_object_new_stringv(fmt, args));
 }
 
+void json_object_object_addv(struct json_object *parent,
+                            struct json_object *child, const char *keyfmt,
+                            va_list args)
+{
+       char *text, buf[256];
+
+       text = vasnprintfrr(MTYPE_TMP, buf, sizeof(buf), keyfmt, args);
+       json_object_object_add(parent, text, child);
+
+       if (text != buf)
+               XFREE(MTYPE_TMP, text);
+}
+
 void json_object_int_add(struct json_object *obj, const char *key, int64_t i)
 {
        json_object_object_add(obj, key, json_object_new_int64(i));
index fcaa84c8161c9105289fbd90c231767ba03e35e7..78c38365151b4baf4716a8404d1913758753a619 100644 (file)
@@ -116,6 +116,28 @@ static inline struct json_object *json_object_new_stringf(const char *fmt, ...)
        return ret;
 }
 
+/* NOTE: argument order differs! (due to varargs)
+ *   json_object_object_add(parent, key, child)
+ *   json_object_object_addv(parent, child, key, va)
+ *   json_object_object_addf(parent, child, key, ...)
+ * (would be weird to have the child inbetween the format string and args)
+ */
+PRINTFRR(3, 0)
+extern void json_object_object_addv(struct json_object *parent,
+                                   struct json_object *child,
+                                   const char *keyfmt, va_list args);
+PRINTFRR(3, 4)
+static inline void json_object_object_addf(struct json_object *parent,
+                                          struct json_object *child,
+                                          const char *keyfmt, ...)
+{
+       va_list args;
+
+       va_start(args, keyfmt);
+       json_object_object_addv(parent, child, keyfmt, args);
+       va_end(args);
+}
+
 #define JSON_STR "JavaScript Object Notation\n"
 
 /* NOTE: json-c lib has following commit 316da85 which