lib: add vty_json() helper

... this is copypasted all over the codebase & should've been a helper
to begin with really.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
David Lamparter 2021-11-17 11:39:00 +01:00
parent 30f0195d0a
commit a8dfd147a0
3 changed files with 23 additions and 0 deletions

View file

@ -59,6 +59,7 @@ extern struct json_object *json_object_lock(struct json_object *obj);
extern void json_object_free(struct json_object *obj);
extern void json_array_string_add(json_object *json, const char *str);
#define JSON_STR "JavaScript Object Notation\n"
/* NOTE: json-c lib has following commit 316da85 which

View file

@ -48,6 +48,7 @@
#include "lib_errors.h"
#include "northbound_cli.h"
#include "printfrr.h"
#include "json.h"
#include <arpa/telnet.h>
#include <termios.h>
@ -280,6 +281,21 @@ done:
return len;
}
int vty_json(struct vty *vty, struct json_object *json)
{
const char *text;
if (!json)
return CMD_SUCCESS;
text = json_object_to_json_string_ext(
json, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE);
vty_out(vty, "%s\n", text);
json_object_free(json);
return CMD_SUCCESS;
}
/* Output current time to the vty. */
void vty_time_print(struct vty *vty, int cr)
{

View file

@ -39,6 +39,8 @@
extern "C" {
#endif
struct json_object;
#define VTY_BUFSIZ 4096
#define VTY_MAXHIST 20
#define VTY_MAXDEPTH 8
@ -322,6 +324,10 @@ extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3);
extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3);
extern void vty_endframe(struct vty *, const char *);
extern bool vty_set_include(struct vty *vty, const char *regexp);
/* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
* NULL check and json_object_free() is included.
*/
extern int vty_json(struct vty *vty, struct json_object *json);
extern bool vty_read_config(struct nb_config *config, const char *config_file,
char *config_default_dir);