Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "common.h"
int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req)
{
struct blob_buf bb;
memset(&bb, 0, sizeof(bb));
blob_buf_init(&bb, 0);
bb = json_to_blob(parsed_response, bb);
ubus_send_reply(ctx, req, bb.head);
blob_buf_free(&bb);
json_object_put(parsed_response);
return 0;
}
struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb)
{
void *arr, *obj;
int i;
json_object_object_foreach(response, key, val) {
int val_type = json_object_get_type(val);
switch (val_type) {
case json_type_int:
blobmsg_add_u32(&bb, key, json_object_get_int(val));
break;
case json_type_double:
blobmsg_add_double(&bb, key, json_object_get_double(val));
break;
case json_type_string:
blobmsg_add_string(&bb, key, json_object_get_string(val));
break;
case json_type_array:
arr = blobmsg_open_array(&bb, key);
for (i = 0; i < json_object_array_length(val); i++) {
// add open table
bb = json_to_blob(json_object_array_get_idx(val, i), bb);
// add close table
}
blobmsg_close_array(&bb, arr);
break;
case json_type_object:
obj = blobmsg_open_table(&bb, key);
bb = json_to_blob(val, bb);
blobmsg_close_table(&bb, obj);
break;
}
}
return bb;
}
//need to free tag content..
char *xml_parser(struct write_result *result, char *tag)
{
char *tag_content;
tag_content = malloc(TAG_CONTENT_SIZE);
if (!tag_content)
goto leave;
xmlDocPtr doc_ptr = NULL;
xmlXPathContextPtr xpathctx_ptr;
xmlXPathObjectPtr xpathobj_ptr;
doc_ptr = xmlParseMemory(result->data, result->pos);
xpathctx_ptr = doc_ptr ? xmlXPathNewContext(doc_ptr) : NULL;
xpathobj_ptr = xpathctx_ptr ? xmlXPathEvalExpression((xmlChar *)tag, xpathctx_ptr) : NULL;
int n;
xmlNodeSetPtr nodeset_ptr = xpathobj_ptr->nodesetval;
for (n = 0; nodeset_ptr && n < xmlXPathNodeSetGetLength(nodeset_ptr); n++)
{
xmlNodePtr node_ptr = nodeset_ptr->nodeTab[n];
if (node_ptr->type != XML_ATTRIBUTE_NODE && node_ptr->type != XML_ELEMENT_NODE && node_ptr->type != XML_CDATA_SECTION_NODE)
continue;
//tag_content = (char*) node_ptr->children->content;
strcpy(tag_content, (char *)node_ptr->children->content);
break;
}
}
}
if (xpathobj_ptr)
xmlXPathFreeObject(xpathobj_ptr);
if (xpathctx_ptr)
xmlXPathFreeContext(xpathctx_ptr);
if (doc_ptr)
xmlFreeDoc(doc_ptr);
return tag_content;
leave:
return NULL;
}
void xml_to_json(xmlNode *anode, json_object *jobj)
{
xmlNodePtr cur_node = NULL;
json_object *cur_jstr = NULL;
for (cur_node = anode; cur_node; cur_node = cur_node->next)
{
debug_print("child address: %p\n", cur_node);
debug_print("next addres: %p\n", cur_node->next);
debug_print("root child content: %s\n", (uint8_t *)xmlNodeGetContent(cur_node));
if (cur_node->type == XML_ELEMENT_NODE)
{
if (xmlChildElementCount(cur_node) == 0)
{
cur_jstr = json_object_new_string((char *)xmlNodeGetContent(cur_node));
debug_print("xmlNodeGet name: %sn", (char *)cur_node->name);
debug_print("xmlNodeGetContent: %sn", (char *)xmlNodeGetContent(cur_node));
json_object_object_add(jobj, (char *)cur_node->name, cur_jstr);
}
}
xml_to_json(cur_node->children, jobj);
}
}
void xml_to_json_converter(struct write_result *result)
{
json_object *jobj;
xmlDocPtr doc_ptr = NULL;
xmlNode *origin_node = NULL;
jobj = json_object_new_object();
doc_ptr = xmlParseMemory(result->data, result->pos);
origin_node = doc_ptr ? xmlDocGetRootElement(doc_ptr) : NULL;
jobj = json_object_new_object();
xml_to_json(origin_node, jobj);
debug_print("The json object created: %s\n", json_object_to_json_string(jobj));