libubox
C utility functions for OpenWrt.
blobmsg_json.h File Reference
#include <stdbool.h>
#include "blobmsg.h"

Go to the source code of this file.

Typedefs

typedef const char *(* blobmsg_json_format_t) (void *priv, struct blob_attr *attr)
 

Functions

bool blobmsg_add_object (struct blob_buf *b, struct json_object *obj)
 
bool blobmsg_add_json_element (struct blob_buf *b, const char *name, struct json_object *obj)
 
bool blobmsg_add_json_from_string (struct blob_buf *b, const char *str)
 
bool blobmsg_add_json_from_file (struct blob_buf *b, const char *file)
 
char * blobmsg_format_json_with_cb (struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
 
static char * blobmsg_format_json (struct blob_attr *attr, bool list)
 
static char * blobmsg_format_json_indent (struct blob_attr *attr, bool list, int indent)
 
char * blobmsg_format_json_value_with_cb (struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
 
static char * blobmsg_format_json_value (struct blob_attr *attr)
 
static char * blobmsg_format_json_value_indent (struct blob_attr *attr, int indent)
 

Typedef Documentation

◆ blobmsg_json_format_t

typedef const char*(* blobmsg_json_format_t) (void *priv, struct blob_attr *attr)

Definition at line 29 of file blobmsg_json.h.

Function Documentation

◆ blobmsg_add_json_element()

bool blobmsg_add_json_element ( struct blob_buf b,
const char *  name,
struct json_object *  obj 
)

Definition at line 47 of file blobmsg_json.c.

48 {
49  bool ret = true;
50  void *c;
51 
52  switch (json_object_get_type(obj)) {
53  case json_type_object:
55  ret = blobmsg_add_object(b, obj);
57  break;
58  case json_type_array:
60  ret = blobmsg_add_array(b, json_object_get_array(obj));
62  break;
63  case json_type_string:
64  blobmsg_add_string(b, name, json_object_get_string(obj));
65  break;
66  case json_type_boolean:
67  blobmsg_add_u8(b, name, json_object_get_boolean(obj));
68  break;
69  case json_type_int: {
70  int64_t i64 = json_object_get_int64(obj);
71  if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
72  blobmsg_add_u32(b, name, (uint32_t)i64);
73  } else {
74  blobmsg_add_u64(b, name, (uint64_t)i64);
75  }
76  break;
77  }
78  case json_type_double:
79  blobmsg_add_double(b, name, json_object_get_double(obj));
80  break;
81  case json_type_null:
83  break;
84  default:
85  return false;
86  }
87  return ret;
88 }
int blobmsg_add_field(struct blob_buf *buf, int type, const char *name, const void *data, unsigned int len)
Definition: blobmsg.c:377
static void * blobmsg_open_table(struct blob_buf *buf, const char *name)
Definition: blobmsg.h:256
static void blobmsg_close_table(struct blob_buf *buf, void *cookie)
Definition: blobmsg.h:268
static int blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
Definition: blobmsg.h:208
static int blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
Definition: blobmsg.h:228
static int blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
Definition: blobmsg.h:235
static void blobmsg_close_array(struct blob_buf *buf, void *cookie)
Definition: blobmsg.h:262
uint8_t name[]
Definition: blobmsg.h:1
@ BLOBMSG_TYPE_UNSPEC
Definition: blobmsg.h:26
static void * blobmsg_open_array(struct blob_buf *buf, const char *name)
Definition: blobmsg.h:250
static int blobmsg_add_double(struct blob_buf *buf, const char *name, double val)
Definition: blobmsg.h:196
static int blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
Definition: blobmsg.h:221
bool blobmsg_add_object(struct blob_buf *b, json_object *obj)
Definition: blobmsg_json.c:26
static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
Definition: blobmsg_json.c:35
static struct blob_buf b
Definition: jshn.c:40
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_add_json_from_file()

bool blobmsg_add_json_from_file ( struct blob_buf b,
const char *  file 
)

Definition at line 107 of file blobmsg_json.c.

108 {
109  return __blobmsg_add_json(b, json_object_from_file(file));
110 }
static bool __blobmsg_add_json(struct blob_buf *b, json_object *obj)
Definition: blobmsg_json.c:90
Here is the call graph for this function:

◆ blobmsg_add_json_from_string()

bool blobmsg_add_json_from_string ( struct blob_buf b,
const char *  str 
)

Definition at line 112 of file blobmsg_json.c.

113 {
114  return __blobmsg_add_json(b, json_tokener_parse(str));
115 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_add_object()

bool blobmsg_add_object ( struct blob_buf b,
struct json_object *  obj 
)

Definition at line 26 of file blobmsg_json.c.

27 {
28  json_object_object_foreach(obj, key, val) {
29  if (!blobmsg_add_json_element(b, key, val))
30  return false;
31  }
32  return true;
33 }
bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
Definition: blobmsg_json.c:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_format_json()

static char* blobmsg_format_json ( struct blob_attr attr,
bool  list 
)
inlinestatic

Definition at line 35 of file blobmsg_json.h.

36 {
37  return blobmsg_format_json_with_cb(attr, list, NULL, NULL, -1);
38 }
char * blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
Definition: blobmsg_json.c:322
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_format_json_indent()

static char* blobmsg_format_json_indent ( struct blob_attr attr,
bool  list,
int  indent 
)
inlinestatic

Definition at line 40 of file blobmsg_json.h.

41 {
42  return blobmsg_format_json_with_cb(attr, list, NULL, NULL, indent);
43 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_format_json_value()

static char* blobmsg_format_json_value ( struct blob_attr attr)
inlinestatic

Definition at line 49 of file blobmsg_json.h.

50 {
51  return blobmsg_format_json_value_with_cb(attr, NULL, NULL, -1);
52 }
char * blobmsg_format_json_value_with_cb(struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
Definition: blobmsg_json.c:356
Here is the call graph for this function:

◆ blobmsg_format_json_value_indent()

static char* blobmsg_format_json_value_indent ( struct blob_attr attr,
int  indent 
)
inlinestatic

Definition at line 54 of file blobmsg_json.h.

55 {
56  return blobmsg_format_json_value_with_cb(attr, NULL, NULL, indent);
57 }
Here is the call graph for this function:

◆ blobmsg_format_json_value_with_cb()

char* blobmsg_format_json_value_with_cb ( struct blob_attr attr,
blobmsg_json_format_t  cb,
void *  priv,
int  indent 
)

Definition at line 356 of file blobmsg_json.c.

357 {
358  struct strbuf s = {0};
359  char *ret;
360 
361  setup_strbuf(&s, attr, cb, priv, indent);
362  if (!s.buf)
363  return NULL;
364 
365  blobmsg_format_element(&s, attr, true, false);
366 
367  if (!s.len) {
368  free(s.buf);
369  return NULL;
370  }
371 
372  ret = realloc(s.buf, s.pos + 1);
373  if (!ret) {
374  free(s.buf);
375  return NULL;
376  }
377 
378  ret[s.pos] = 0;
379 
380  return ret;
381 }
static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool without_name, bool head)
Definition: blobmsg_json.c:225
static void setup_strbuf(struct strbuf *s, struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
Definition: blobmsg_json.c:307
char * buf
Definition: blobmsg_json.c:121
bool indent
Definition: blobmsg_json.c:125
void * priv
Definition: blobmsg_json.c:124
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_format_json_with_cb()

char* blobmsg_format_json_with_cb ( struct blob_attr attr,
bool  list,
blobmsg_json_format_t  cb,
void *  priv,
int  indent 
)

Definition at line 322 of file blobmsg_json.c.

323 {
324  struct strbuf s = {0};
325  bool array;
326  char *ret;
327 
328  setup_strbuf(&s, attr, cb, priv, indent);
329  if (!s.buf)
330  return NULL;
331 
332  array = blob_is_extended(attr) &&
334 
335  if (list)
336  blobmsg_format_json_list(&s, blobmsg_data(attr), blobmsg_data_len(attr), array);
337  else
338  blobmsg_format_element(&s, attr, false, false);
339 
340  if (!s.len) {
341  free(s.buf);
342  return NULL;
343  }
344 
345  ret = realloc(s.buf, s.pos + 1);
346  if (!ret) {
347  free(s.buf);
348  return NULL;
349  }
350 
351  ret[s.pos] = 0;
352 
353  return ret;
354 }
static bool blob_is_extended(const struct blob_attr *attr)
Definition: blob.h:91
static size_t blobmsg_data_len(const struct blob_attr *attr)
Definition: blobmsg.h:95
blobmsg_type
Definition: blobmsg.h:25
@ BLOBMSG_TYPE_ARRAY
Definition: blobmsg.h:27
static void * blobmsg_data(const struct blob_attr *attr)
Definition: blobmsg.h:78
static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
Definition: blobmsg_json.c:284
Here is the call graph for this function:
Here is the caller graph for this function: