aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTed Gould <ted@gould.cx>2010-11-23 14:40:11 -0600
committerTed Gould <ted@gould.cx>2010-11-23 14:40:11 -0600
commit784522930b3102d044000ef1dd6af1854548e3be (patch)
tree76f10574bb45c8fdcde315720ec7319484dff50c /tests
parente811c4f900545d37aecdf18cbce911ff23e684cd (diff)
downloadlibdbusmenu-784522930b3102d044000ef1dd6af1854548e3be.tar.gz
libdbusmenu-784522930b3102d044000ef1dd6af1854548e3be.tar.bz2
libdbusmenu-784522930b3102d044000ef1dd6af1854548e3be.zip
Convert json loader to using variants
Diffstat (limited to 'tests')
-rw-r--r--tests/json-loader.c107
1 files changed, 57 insertions, 50 deletions
diff --git a/tests/json-loader.c b/tests/json-loader.c
index 9e67666..4d3e6aa 100644
--- a/tests/json-loader.c
+++ b/tests/json-loader.c
@@ -21,67 +21,75 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#include "json-loader.h"
-static GValue *
-node2value (JsonNode * node)
+static GVariant * node2variant (JsonNode * node);
+
+static void
+array_foreach (JsonArray * array, guint index, JsonNode * node, gpointer user_data)
+{
+ GVariantBuilder * builder = (GVariantBuilder *)user_data;
+ GVariant * variant = node2variant(node);
+ if (variant != NULL) {
+ g_variant_builder_add_value(builder, variant);
+ }
+ return;
+}
+
+static void
+object_foreach (JsonObject * array, const gchar * member, JsonNode * node, gpointer user_data)
+{
+ GVariantBuilder * builder = (GVariantBuilder *)user_data;
+ GVariant * variant = node2variant(node);
+ if (variant != NULL) {
+ g_variant_builder_add(builder, "{sv}", member, variant);
+ }
+ return;
+}
+
+static GVariant *
+node2variant (JsonNode * node)
{
if (node == NULL) {
return NULL;
}
- GValue * value = g_new0(GValue, 1);
-
if (JSON_NODE_TYPE(node) == JSON_NODE_VALUE) {
- json_node_get_value(node, value);
- return value;
+ switch (json_node_get_value_type(node)) {
+ case G_TYPE_INT:
+ case G_TYPE_INT64:
+ return g_variant_new_int64(json_node_get_int(node));
+ case G_TYPE_DOUBLE:
+ case G_TYPE_FLOAT:
+ return g_variant_new_double(json_node_get_double(node));
+ case G_TYPE_BOOLEAN:
+ return g_variant_new_boolean(json_node_get_boolean(node));
+ case G_TYPE_STRING:
+ return g_variant_new_string(json_node_get_string(node));
+ default:
+ g_assert_not_reached();
+ }
}
if (JSON_NODE_TYPE(node) == JSON_NODE_ARRAY) {
+ GVariantBuilder builder;
+ g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
+
JsonArray * array = json_node_get_array(node);
- JsonNode * first = json_array_get_element(array, 0);
-
- if (JSON_NODE_TYPE(first) == JSON_NODE_VALUE) {
- GValue subvalue = {0};
- json_node_get_value(first, &subvalue);
-
- if (G_VALUE_TYPE(&subvalue) == G_TYPE_STRING) {
- GArray * garray = g_array_sized_new(TRUE, TRUE, sizeof(gchar *), json_array_get_length(array));
- g_value_init(value, G_TYPE_STRV);
- g_value_take_boxed(value, garray->data);
-
- int i;
- for (i = 0; i < json_array_get_length(array); i++) {
- const gchar * str = json_node_get_string(json_array_get_element(array, i));
- gchar * dupstr = g_strdup(str);
- g_array_append_val(garray, dupstr);
- }
-
- g_array_free(garray, FALSE);
- } else {
- GValueArray * varray = g_value_array_new(json_array_get_length(array));
- g_value_init(value, G_TYPE_VALUE_ARRAY);
- g_value_take_boxed(value, varray);
-
- g_value_array_append(varray, &subvalue);
- g_value_unset(&subvalue);
-
- int i;
- for (i = 1; i < json_array_get_length(array); i++) {
- json_node_get_value(first, &subvalue);
- g_value_array_append(varray, &subvalue);
- g_value_unset(&subvalue);
- }
- }
+ json_array_foreach_element(array, array_foreach, &builder);
- } else {
- g_warning("Complex array not supported");
- }
+ return g_variant_builder_end(&builder);
}
if (JSON_NODE_TYPE(node) == JSON_NODE_OBJECT) {
- g_warning("Object nodes are a problem");
+ GVariantBuilder builder;
+ g_variant_builder_init(&builder, G_VARIANT_TYPE_DICTIONARY);
+
+ JsonObject * array = json_node_get_object(node);
+ json_object_foreach_member(array, object_foreach, &builder);
+
+ return g_variant_builder_end(&builder);
}
- return value;
+ return NULL;
}
static void
@@ -97,12 +105,11 @@ set_props (DbusmenuMenuitem * mi, JsonObject * node)
if (!g_strcmp0(member, "submenu")) { continue; }
JsonNode * lnode = json_object_get_member(node, member);
- GValue * value = node2value(lnode);
+ GVariant * variant = node2variant(lnode);
- if (value != NULL) {
- dbusmenu_menuitem_property_set_value(mi, member, value);
- g_value_unset(value);
- g_free(value);
+ if (variant != NULL) {
+ dbusmenu_menuitem_property_set_variant(mi, member, variant);
+ g_variant_unref(variant);
}
}