static void test_json_build(void) { ACL_JSON* json = acl_json_alloc(); ACL_JSON_NODE* root, *node1, *node2, *node3; root = acl_json_create_obj(json); acl_json_node_append_child(json->root, root); node1 = acl_json_create_leaf(json, "name1", "value1"); acl_json_node_append_child(root, node1); node1 = acl_json_create_leaf(json, "name2", "value2"); acl_json_node_append_child(root, node1); node1 = acl_json_create_obj(json); node2 = acl_json_create_leaf(json, "name3", "value3"); acl_json_node_append_child(node1, node2); node2 = acl_json_create_node(json, "name4", node1); acl_json_node_append_child(root, node2); ////////////////////////////////////////////////////////////////////////// node1 = acl_json_create_array(json); node2 = acl_json_create_node(json, "name5", node1); acl_json_node_append_child(root, node2); node3 = acl_json_create_leaf(json, "name6", "value6"); acl_json_node_append_child(node1, node3); node3 = acl_json_create_leaf(json, "name7", "value7"); acl_json_node_append_child(node1, node3); node3 = acl_json_create_obj(json); acl_json_node_append_child(node1, node3); node2 = acl_json_create_leaf(json, "name8", "value8"); acl_json_node_append_child(node3, node2); node2 = acl_json_create_leaf(json, "name9", "value9"); acl_json_node_append_child(node3, node2); ////////////////////////////////////////////////////////////////////////// ACL_VSTRING *buf1 = acl_vstring_alloc(128); ACL_VSTRING *buf2 = acl_vstring_alloc(128); acl_json_build(json, buf1); acl_json_building(json, 1, build_callback, buf2); printf("%s\r\n", acl_vstring_str(buf1)); printf("%s\r\n", acl_vstring_str(buf2)); if (strcmp(STR(buf1), STR(buf2)) != 0) printf("BUILD ERROR\r\n"); else printf("BUILD OK\r\n"); acl_vstring_free(buf1); acl_vstring_free(buf2); acl_json_free(json); }
json::~json(void) { clear(); if (json_) acl_json_free(json_); delete root_; delete buf_; if (iter_) acl_myfree(iter_); }
static void test_json(void) { ACL_JSON *json = acl_json_alloc(); const char* ptr = default_data; ptr = acl_json_update(json, ptr); printf("finish: %s, left char: %s\r\n", acl_json_finish(json) ? "yes" : "no", ptr); acl_json_free(json); }
static void parse_json(const char *data) { ACL_JSON *json = acl_json_alloc(); ACL_VSTRING *buf1 = acl_vstring_alloc(128); ACL_VSTRING *buf2 = acl_vstring_alloc(128); ACL_ARRAY *nodes; const char *tag = "header"; printf("buf src: %s\r\n", data); printf("------------------------------------------------\r\n"); acl_json_update(json, data); acl_json_build(json, buf1); printf("result src: %s\r\n", STR(buf1)); printf("------------------------------------------------\r\n"); nodes = acl_json_getElementsByTagName(json, tag); if (nodes == NULL) { printf("not found tag: %s\r\n", tag); acl_vstring_free(buf1); acl_vstring_free(buf2); acl_json_free(json); return; } printf(">>>tag: %s, len: %d\r\n", tag, acl_array_size(nodes)); ACL_ITER iter; #define STR acl_vstring_str #define LEN ACL_VSTRING_LEN acl_foreach(iter, nodes) { ACL_JSON_NODE *node = (ACL_JSON_NODE*) iter.data; ACL_JSON_NODE *tag_node = node->tag_node; if (tag_node == NULL) continue; printf(">>>tag: %s\r\n", STR(node->ltag)); ACL_ITER iter2; acl_foreach(iter2, tag_node) { ACL_JSON_NODE *node1 = (ACL_JSON_NODE*) iter2.data; if (node1->ltag == NULL || LEN(node1->ltag) == 0) continue; printf(">>>child tag: %s, txt: %s\r\n", STR(node1->ltag), node1->text ? STR(node1->text) : "null"); }
json::~json(void) { clear(); if (json_) acl_json_free(json_); if (root_) delete root_; if (node_tmp_) delete node_tmp_; if (buf_) delete buf_; if (iter_) acl_myfree(iter_); }
static void test_json_benchmark(int max) { ACL_JSON *json = acl_json_alloc(); ACL_METER_TIME("-------------bat begin--------------"); for (int i = 0; i < max; i++) { const char* ptr = default_data; acl_json_update(json, ptr); acl_json_reset(json); } ACL_METER_TIME("-------------bat end--------------"); acl_json_free(json); }