implement TreeNode to JSON conversion

This commit is contained in:
Akos Horvath 2023-09-28 20:21:10 +02:00
parent 0b878dd9d7
commit 9e2070500a
2 changed files with 32 additions and 9 deletions

View File

@ -1,5 +1,7 @@
#include "util.h" #include "util.h"
#include "wm.h" #include "wm.h"
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.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(node);
assert(buf);
char type[128] = {0}; char type[128] = {0};
wm_node_type_to_str(node->type, type, sizeof(type)); wm_node_type_to_str(node->type, type, sizeof(type));
snprintf(buf, bufsize, "{ type: %s, parent: %p, children: { nodes: %p, " json_object *jobj = json_object_new_object();
"size: %ld, capacity: %ld }, pos: { x: %d, y: %d, w: %d, h: %d }, "
"client: %p, id: %d }\n", type, node->parent, json_object_object_add(jobj, "type", json_object_new_string(type));
node->children.nodes, node->children.size, node->children.capacity, json_object_object_add(jobj, "parent", json_object_new_uint64((uint64_t)node->parent));
node->pos.x, node->pos.y, node->pos.w, node->pos.h, json_object_object_add(jobj, "children", json_object_new_array());
node->client, node->id);
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) void wm_tree_to_DOT(TreeNode *root, const char *filename)

View File

@ -4,6 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <json-c/json.h>
#include "client.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); int wm_get_node_index(TreeNode *parent, unsigned int node_id);
void wm_tree_to_DOT(TreeNode *root, const char *filename); 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_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(); NodeArray wm_nodearray_new();
void wm_nodearray_push(NodeArray *arr, TreeNode *node); void wm_nodearray_push(NodeArray *arr, TreeNode *node);