implement TreeNode to JSON conversion
This commit is contained in:
parent
0b878dd9d7
commit
9e2070500a
38
src/util.c
38
src/util.c
@ -1,5 +1,7 @@
|
||||
#include "util.h"
|
||||
#include "wm.h"
|
||||
#include <json-c/json_object.h>
|
||||
#include <json-c/json_tokener.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@ -119,19 +121,39 @@ void wm_node_type_to_str(NodeType type, char *buf, size_t bufsize)
|
||||
}
|
||||
}
|
||||
|
||||
void wm_treenode_to_str(TreeNode *node, char *buf, size_t bufsize)
|
||||
char* wm_treenode_to_str(TreeNode *node)
|
||||
{
|
||||
assert(node);
|
||||
assert(buf);
|
||||
char type[128] = {0};
|
||||
wm_node_type_to_str(node->type, type, sizeof(type));
|
||||
|
||||
snprintf(buf, bufsize, "{ type: %s, parent: %p, children: { nodes: %p, "
|
||||
"size: %ld, capacity: %ld }, pos: { x: %d, y: %d, w: %d, h: %d }, "
|
||||
"client: %p, id: %d }\n", type, node->parent,
|
||||
node->children.nodes, node->children.size, node->children.capacity,
|
||||
node->pos.x, node->pos.y, node->pos.w, node->pos.h,
|
||||
node->client, node->id);
|
||||
json_object *jobj = json_object_new_object();
|
||||
|
||||
json_object_object_add(jobj, "type", json_object_new_string(type));
|
||||
json_object_object_add(jobj, "parent", json_object_new_uint64((uint64_t)node->parent));
|
||||
json_object_object_add(jobj, "children", json_object_new_array());
|
||||
|
||||
for (size_t i = 0; i < node->children.size; i++) {
|
||||
char *child_str = wm_treenode_to_str(node->children.nodes[i]);
|
||||
json_object_array_add(json_object_object_get(jobj, "children"),
|
||||
json_tokener_parse(child_str));
|
||||
free(child_str);
|
||||
}
|
||||
|
||||
json_object *pos = json_object_new_object();
|
||||
json_object_object_add(pos, "x", json_object_new_int(node->pos.x));
|
||||
json_object_object_add(pos, "y", json_object_new_int(node->pos.y));
|
||||
json_object_object_add(pos, "w", json_object_new_int(node->pos.w));
|
||||
json_object_object_add(pos, "h", json_object_new_int(node->pos.h));
|
||||
json_object_object_add(jobj, "pos", pos);
|
||||
json_object_object_add(jobj, "client", json_object_new_uint64((uint64_t)node->client));
|
||||
json_object_object_add(jobj, "id", json_object_new_uint64((uint64_t)node->id));
|
||||
|
||||
|
||||
char *ret = strdup(json_object_to_json_string(jobj));
|
||||
json_object_put(jobj);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void wm_tree_to_DOT(TreeNode *root, const char *filename)
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <json-c/json.h>
|
||||
|
||||
#include "client.h"
|
||||
|
||||
@ -71,7 +72,7 @@ void wm_treenode_remove_node(TreeNode *root, unsigned int node_id);
|
||||
int wm_get_node_index(TreeNode *parent, unsigned int node_id);
|
||||
void wm_tree_to_DOT(TreeNode *root, const char *filename);
|
||||
void wm_node_type_to_str(NodeType type, char *buf, size_t bufsize);
|
||||
void wm_treenode_to_str(TreeNode *node, char *buf, size_t bufsize);
|
||||
char* wm_treenode_to_str(TreeNode *node);
|
||||
|
||||
NodeArray wm_nodearray_new();
|
||||
void wm_nodearray_push(NodeArray *arr, TreeNode *node);
|
||||
|
Loading…
x
Reference in New Issue
Block a user