Rework _json_node_deep_copy in Vala
This commit is contained in:
parent
19ef7de1f0
commit
a99ec3ba61
@ -93,7 +93,6 @@ libmatrix_glib_0_0_la_SOURCES = \
|
|||||||
matrix-version.c \
|
matrix-version.c \
|
||||||
matrix-types.c \
|
matrix-types.c \
|
||||||
matrix-enumtypes.c \
|
matrix-enumtypes.c \
|
||||||
utils.c \
|
|
||||||
$(INST_H_SRC_FILES) \
|
$(INST_H_SRC_FILES) \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
|
@ -91,8 +91,4 @@ namespace Matrix {
|
|||||||
public class StateEvent {
|
public class StateEvent {
|
||||||
public Json.Node? get_json_node();
|
public Json.Node? get_json_node();
|
||||||
}
|
}
|
||||||
|
|
||||||
[CCode (cheader_filename = "utils.h", cname = "_json_node_deep_copy")]
|
|
||||||
public Json.Node?
|
|
||||||
_json_node_deep_copy(Json.Node? node);
|
|
||||||
}
|
}
|
||||||
|
@ -651,4 +651,53 @@ namespace Matrix {
|
|||||||
return builder.get_root();
|
return builder.get_root();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Json.Node?
|
||||||
|
_json_node_deep_copy(Json.Node? node)
|
||||||
|
{
|
||||||
|
Json.Node ret;
|
||||||
|
|
||||||
|
if (node == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = new Json.Node(node.get_node_type());
|
||||||
|
|
||||||
|
switch (node.get_node_type()) {
|
||||||
|
case Json.NodeType.OBJECT:
|
||||||
|
var new_obj = new Json.Object();
|
||||||
|
|
||||||
|
node.get_object().foreach_member(
|
||||||
|
(old_obj, member_name, member_node) => {
|
||||||
|
new_obj.set_member(
|
||||||
|
member_name,
|
||||||
|
_json_node_deep_copy(member_node));
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.set_object(new_obj);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Json.NodeType.ARRAY:
|
||||||
|
var new_ary = new Json.Array();
|
||||||
|
|
||||||
|
node.get_array().foreach_element(
|
||||||
|
(old_ary, idx, element_node) => {
|
||||||
|
new_ary.add_element(
|
||||||
|
_json_node_deep_copy(element_node));
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Json.NodeType.VALUE:
|
||||||
|
ret.set_value(node.get_value());
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Json.NodeType.NULL:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
#include "matrix-types.h"
|
#include "matrix-types.h"
|
||||||
#include "matrix-enumtypes.h"
|
#include "matrix-enumtypes.h"
|
||||||
#include "matrix-glib.h"
|
#include "matrix-glib.h"
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SECTION:matrix-types
|
* SECTION:matrix-types
|
||||||
|
98
src/utils.c
98
src/utils.c
@ -1,98 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of matrix-glib-sdk
|
|
||||||
*
|
|
||||||
* matrix-glib-sdk is free software: you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
|
||||||
* License as published by the Free Software Foundation, either
|
|
||||||
* version 3 of the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* matrix-glib-sdk is distributed in the hope that it will be
|
|
||||||
* useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
||||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
* See the GNU Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
|
||||||
* License along with matrix-glib-sdk. If not, see
|
|
||||||
* <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "utils.h"
|
|
||||||
#include "matrix-types.h"
|
|
||||||
|
|
||||||
static void
|
|
||||||
deep_copy_object(JsonObject *object,
|
|
||||||
const gchar *member_name,
|
|
||||||
JsonNode *member_node,
|
|
||||||
JsonObject *new_obj)
|
|
||||||
{
|
|
||||||
json_object_set_member(new_obj,
|
|
||||||
member_name,
|
|
||||||
_json_node_deep_copy((const JsonNode *)member_node));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
deep_copy_array(JsonArray *array,
|
|
||||||
guint idx,
|
|
||||||
JsonNode *element_node,
|
|
||||||
JsonArray *new_array)
|
|
||||||
{
|
|
||||||
json_array_add_element(new_array,
|
|
||||||
_json_node_deep_copy((const JsonNode *)element_node));
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *
|
|
||||||
_json_node_deep_copy(const JsonNode *node)
|
|
||||||
{
|
|
||||||
JsonNode *ret;
|
|
||||||
|
|
||||||
if (node == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = json_node_new(JSON_NODE_TYPE((JsonNode *)node));
|
|
||||||
|
|
||||||
switch (JSON_NODE_TYPE((JsonNode *)node)) {
|
|
||||||
case JSON_NODE_OBJECT:
|
|
||||||
{
|
|
||||||
JsonObject *new_obj = json_object_new();
|
|
||||||
|
|
||||||
json_object_foreach_member(json_node_get_object((JsonNode *)node),
|
|
||||||
(JsonObjectForeach)deep_copy_object,
|
|
||||||
new_obj);
|
|
||||||
|
|
||||||
json_node_set_object(ret, new_obj);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case JSON_NODE_ARRAY:
|
|
||||||
{
|
|
||||||
JsonArray *new_ary = json_array_new();
|
|
||||||
|
|
||||||
json_array_foreach_element(json_node_get_array((JsonNode *)node),
|
|
||||||
(JsonArrayForeach)deep_copy_array,
|
|
||||||
new_ary);
|
|
||||||
|
|
||||||
json_node_set_array(ret, new_ary);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case JSON_NODE_VALUE:
|
|
||||||
{
|
|
||||||
GValue val = G_VALUE_INIT;
|
|
||||||
|
|
||||||
json_node_get_value((JsonNode *)node, &val);
|
|
||||||
json_node_set_value(ret, &val);
|
|
||||||
|
|
||||||
g_value_unset(&val);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case JSON_NODE_NULL:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
28
src/utils.h
28
src/utils.h
@ -1,28 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of matrix-glib-sdk
|
|
||||||
*
|
|
||||||
* matrix-glib-sdk is free software: you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
|
||||||
* License as published by the Free Software Foundation, either
|
|
||||||
* version 3 of the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* matrix-glib-sdk is distributed in the hope that it will be
|
|
||||||
* useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
||||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
* See the GNU Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
|
||||||
* License along with matrix-glib-sdk. If not, see
|
|
||||||
* <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __MATRIX_UTILS_H__
|
|
||||||
#define __MATRIX_UTILS_H__
|
|
||||||
|
|
||||||
#include <glib.h>
|
|
||||||
#include <glib-object.h>
|
|
||||||
#include <json-glib/json-glib.h>
|
|
||||||
|
|
||||||
JsonNode *_json_node_deep_copy(const JsonNode *node);
|
|
||||||
|
|
||||||
#endif /* __MATRIX_UTILS_H__ */
|
|
Loading…
Reference in New Issue
Block a user