2 * blobmsg - library for generating/parsing structured blob messages
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include "blobmsg_json.h"
19 static bool blobmsg_add_object(struct blob_buf *b, json_object *obj)
21 json_object_object_foreach(obj, key, val) {
22 if (!blobmsg_add_json_element(b, key, val))
28 static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
32 for (i = 0, len = array_list_length(a); i < len; i++) {
33 if (!blobmsg_add_json_element(b, NULL, array_list_get_idx(a, i)))
40 bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
48 switch (json_object_get_type(obj)) {
49 case json_type_object:
50 c = blobmsg_open_table(b, name);
51 ret = blobmsg_add_object(b, obj);
52 blobmsg_close_table(b, c);
55 c = blobmsg_open_array(b, name);
56 ret = blobmsg_add_array(b, json_object_get_array(obj));
57 blobmsg_close_array(b, c);
59 case json_type_string:
60 blobmsg_add_string(b, name, json_object_get_string(obj));
62 case json_type_boolean:
63 blobmsg_add_u8(b, name, json_object_get_boolean(obj));
66 blobmsg_add_u32(b, name, json_object_get_int(obj));
74 bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
79 obj = json_tokener_parse(str);
83 if (json_object_get_type(obj) != json_type_object)
86 ret = blobmsg_add_object(b, obj);