2c2e703f09c0b564d5ddecdeea53cbc44464aec2
4 #include "blobmsg_json.h"
6 static const char *indent_str
= "\t\t\t\t\t\t\t\t\t\t\t\t\t";
8 #define indent_printf(indent, ...) do { \
10 fwrite(indent_str, indent, 1, stderr); \
11 fprintf(stderr, __VA_ARGS__); \
14 static void dump_attr_data(void *data
, int len
, int type
, int indent
, int next_indent
);
17 dump_table(struct blob_attr
*head
, int len
, int indent
, bool array
)
19 struct blob_attr
*attr
, *last_attr
;
20 struct blobmsg_hdr
*hdr
;
22 indent_printf(indent
, "{\n");
23 __blob_for_each_attr(attr
, head
, len
) {
24 hdr
= blob_data(attr
);
26 indent_printf(indent
+ 1, "%s : ", hdr
->name
);
27 dump_attr_data(blobmsg_data(attr
), blobmsg_data_len(attr
), blob_id(attr
), 0, indent
+ 1);
30 indent_printf(indent
, "}\n");
33 static void dump_attr_data(void *data
, int len
, int type
, int indent
, int next_indent
)
36 case BLOBMSG_TYPE_STRING
:
37 indent_printf(indent
, "%s\n", (char *) data
);
39 case BLOBMSG_TYPE_INT8
:
40 indent_printf(indent
, "%d\n", *(uint8_t *)data
);
42 case BLOBMSG_TYPE_INT16
:
43 indent_printf(indent
, "%d\n", *(uint16_t *)data
);
45 case BLOBMSG_TYPE_INT32
:
46 indent_printf(indent
, "%d\n", *(uint32_t *)data
);
48 case BLOBMSG_TYPE_INT64
:
49 indent_printf(indent
, "%lld\n", *(uint64_t *)data
);
51 case BLOBMSG_TYPE_TABLE
:
52 case BLOBMSG_TYPE_ARRAY
:
54 indent_printf(indent
, "\n");
55 dump_table(data
, len
, next_indent
, type
== BLOBMSG_TYPE_ARRAY
);
66 static const struct blobmsg_policy pol
[] = {
69 .type
= BLOBMSG_TYPE_STRING
,
73 .type
= BLOBMSG_TYPE_ARRAY
,
77 .type
= BLOBMSG_TYPE_TABLE
,
82 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
85 static void dump_message(struct blob_buf
*buf
)
87 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
89 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
90 fprintf(stderr
, "Parse failed\n");
94 fprintf(stderr
, "Message: %s\n", (char *) blobmsg_data(tb
[FOO_MESSAGE
]));
97 fprintf(stderr
, "List: ");
98 dump_table(blobmsg_data(tb
[FOO_LIST
]), blob_len(tb
[FOO_LIST
]), 0, true);
100 if (tb
[FOO_TESTDATA
]) {
101 fprintf(stderr
, "Testdata: ");
102 dump_table(blobmsg_data(tb
[FOO_TESTDATA
]), blob_len(tb
[FOO_TESTDATA
]), 0, false);
107 fill_message(struct blob_buf
*buf
)
111 blobmsg_add_string(buf
, "message", "Hello, world!");
113 tbl
= blobmsg_open_table(buf
, "testdata");
114 blobmsg_add_u32(buf
, "hello", 1);
115 blobmsg_add_string(buf
, "world", "2");
116 blobmsg_close_table(buf
, tbl
);
118 tbl
= blobmsg_open_array(buf
, "list");
119 blobmsg_add_u32(buf
, NULL
, 0);
120 blobmsg_add_u32(buf
, NULL
, 1);
121 blobmsg_add_u32(buf
, NULL
, 2);
122 blobmsg_close_table(buf
, tbl
);
125 int main(int argc
, char **argv
)
127 static struct blob_buf buf
;
129 blobmsg_buf_init(&buf
);
132 fprintf(stderr
, "json: %s\n", blobmsg_format_json(buf
.head
, false));