5 #include "blobmsg_json.h"
7 static const char *indent_str
= "\t\t\t\t\t\t\t\t\t\t\t\t\t";
9 #define indent_printf(indent, ...) do { \
11 fwrite(indent_str, indent, 1, stderr); \
12 fprintf(stderr, __VA_ARGS__); \
15 static void dump_attr_data(struct blob_attr
*data
, int indent
, int next_indent
);
18 dump_table(struct blob_attr
*head
, size_t len
, int indent
, bool array
)
20 struct blob_attr
*attr
;
21 struct blobmsg_hdr
*hdr
;
23 indent_printf(indent
, "{\n");
24 __blob_for_each_attr(attr
, head
, len
) {
25 hdr
= blob_data(attr
);
27 indent_printf(indent
+ 1, "%s : ", hdr
->name
);
28 dump_attr_data(attr
, 0, indent
+ 1);
30 indent_printf(indent
, "}\n");
33 static void dump_attr_data(struct blob_attr
*data
, int indent
, int next_indent
)
35 int type
= blobmsg_type(data
);
37 case BLOBMSG_TYPE_STRING
:
38 indent_printf(indent
, "%s\n", blobmsg_get_string(data
));
40 case BLOBMSG_TYPE_INT8
:
41 indent_printf(indent
, "%d\n", blobmsg_get_u8(data
));
43 case BLOBMSG_TYPE_INT16
:
44 indent_printf(indent
, "%d\n", blobmsg_get_u16(data
));
46 case BLOBMSG_TYPE_INT32
:
47 indent_printf(indent
, "%d\n", blobmsg_get_u32(data
));
49 case BLOBMSG_TYPE_INT64
:
50 indent_printf(indent
, "%"PRIu64
"\n", blobmsg_get_u64(data
));
52 case BLOBMSG_TYPE_DOUBLE
:
53 indent_printf(indent
, "%lf\n", blobmsg_get_double(data
));
55 case BLOBMSG_TYPE_TABLE
:
56 case BLOBMSG_TYPE_ARRAY
:
58 indent_printf(indent
, "\n");
59 dump_table(blobmsg_data(data
), blobmsg_data_len(data
),
60 next_indent
, type
== BLOBMSG_TYPE_ARRAY
);
71 static const struct blobmsg_policy pol
[] = {
74 .type
= BLOBMSG_TYPE_STRING
,
78 .type
= BLOBMSG_TYPE_ARRAY
,
82 .type
= BLOBMSG_TYPE_TABLE
,
87 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
90 static void dump_message(struct blob_buf
*buf
)
92 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
94 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
95 fprintf(stderr
, "Parse failed\n");
99 fprintf(stderr
, "Message: %s\n", (char *) blobmsg_data(tb
[FOO_MESSAGE
]));
102 fprintf(stderr
, "List: ");
103 dump_table(blobmsg_data(tb
[FOO_LIST
]), blobmsg_data_len(tb
[FOO_LIST
]), 0, true);
105 if (tb
[FOO_TESTDATA
]) {
106 fprintf(stderr
, "Testdata: ");
107 dump_table(blobmsg_data(tb
[FOO_TESTDATA
]), blobmsg_data_len(tb
[FOO_TESTDATA
]), 0, false);
112 fill_message(struct blob_buf
*buf
)
116 blobmsg_add_string(buf
, "message", "Hello, world!");
118 tbl
= blobmsg_open_table(buf
, "testdata");
119 blobmsg_add_double(buf
, "double", 1.337e2
);
120 blobmsg_add_u32(buf
, "hello", 1);
121 blobmsg_add_string(buf
, "world", "2");
122 blobmsg_close_table(buf
, tbl
);
124 tbl
= blobmsg_open_array(buf
, "list");
125 blobmsg_add_u32(buf
, NULL
, 0);
126 blobmsg_add_u32(buf
, NULL
, 1);
127 blobmsg_add_u32(buf
, NULL
, 2);
128 blobmsg_add_double(buf
, "double", 1.337e2
);
129 blobmsg_close_table(buf
, tbl
);
132 int main(int argc
, char **argv
)
134 static struct blob_buf buf
;
136 blobmsg_buf_init(&buf
);
139 fprintf(stderr
, "json: %s\n", blobmsg_format_json(buf
.head
, true));