6330fea33095fdf9dfcbd87eee6dbd8e78fa6485
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(struct blob_attr
*data
, int indent
, int next_indent
);
17 dump_table(struct blob_attr
*head
, int len
, int indent
, bool array
)
19 struct blob_attr
*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(attr
, 0, indent
+ 1);
29 indent_printf(indent
, "}\n");
32 static void dump_attr_data(struct blob_attr
*data
, int indent
, int next_indent
)
34 int type
= blobmsg_type(data
);
36 case BLOBMSG_TYPE_STRING
:
37 indent_printf(indent
, "%s\n", (char *) blobmsg_data(data
));
39 case BLOBMSG_TYPE_INT8
:
40 indent_printf(indent
, "%d\n", blobmsg_get_u8(data
));
42 case BLOBMSG_TYPE_INT16
:
43 indent_printf(indent
, "%d\n", blobmsg_get_u16(data
));
45 case BLOBMSG_TYPE_INT32
:
46 indent_printf(indent
, "%d\n", blobmsg_get_u32(data
));
48 case BLOBMSG_TYPE_INT64
:
49 indent_printf(indent
, "%"PRIu64
"\n", blobmsg_get_u64(data
));
51 case BLOBMSG_TYPE_TABLE
:
52 case BLOBMSG_TYPE_ARRAY
:
54 indent_printf(indent
, "\n");
55 dump_table(blobmsg_data(data
), blobmsg_data_len(data
),
56 next_indent
, type
== BLOBMSG_TYPE_ARRAY
);
67 static const struct blobmsg_policy pol
[] = {
70 .type
= BLOBMSG_TYPE_STRING
,
74 .type
= BLOBMSG_TYPE_ARRAY
,
78 .type
= BLOBMSG_TYPE_TABLE
,
83 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
86 static void dump_message(struct blob_buf
*buf
)
88 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
90 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
91 fprintf(stderr
, "Parse failed\n");
95 fprintf(stderr
, "Message: %s\n", (char *) blobmsg_data(tb
[FOO_MESSAGE
]));
98 fprintf(stderr
, "List: ");
99 dump_table(blobmsg_data(tb
[FOO_LIST
]), blobmsg_data_len(tb
[FOO_LIST
]), 0, true);
101 if (tb
[FOO_TESTDATA
]) {
102 fprintf(stderr
, "Testdata: ");
103 dump_table(blobmsg_data(tb
[FOO_TESTDATA
]), blobmsg_data_len(tb
[FOO_TESTDATA
]), 0, false);
108 fill_message(struct blob_buf
*buf
)
112 blobmsg_add_string(buf
, "message", "Hello, world!");
114 tbl
= blobmsg_open_table(buf
, "testdata");
115 blobmsg_add_u32(buf
, "hello", 1);
116 blobmsg_add_string(buf
, "world", "2");
117 blobmsg_close_table(buf
, tbl
);
119 tbl
= blobmsg_open_array(buf
, "list");
120 blobmsg_add_u32(buf
, NULL
, 0);
121 blobmsg_add_u32(buf
, NULL
, 1);
122 blobmsg_add_u32(buf
, NULL
, 2);
123 blobmsg_close_table(buf
, tbl
);
126 int main(int argc
, char **argv
)
128 static struct blob_buf buf
;
130 blobmsg_buf_init(&buf
);
133 fprintf(stderr
, "json: %s\n", blobmsg_format_json(buf
.head
, true));