From: Andrea Zagli Date: Sat, 29 Aug 2020 16:19:20 +0000 (+0200) Subject: Added functions Json:to_hashtable*. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=a1cba6cb81c054c4aabbab963f45d299c071ae4e;p=libzakutilsjsonxml Added functions Json:to_hashtable*. --- diff --git a/configure.ac b/configure.ac index 64172d8..1d20e93 100644 --- a/configure.ac +++ b/configure.ac @@ -46,7 +46,8 @@ PKG_CHECK_MODULES(ZAKUTILSJX, [glib-2.0 >= 2.36 gobject-2.0 >= 2.36 gio-2.0 >= 2.36 json-glib-1.0 - libxml-2.0]) + libxml-2.0 + libzakutils]) AC_SUBST(ZAKUTILSJX_CFLAGS) AC_SUBST(ZAKUTILSJX_LIBS) diff --git a/src/json.c b/src/json.c index c681b78..4678d0c 100644 --- a/src/json.c +++ b/src/json.c @@ -20,6 +20,8 @@ #include #endif +#include + #include "json.h" JsonNode @@ -163,3 +165,64 @@ zak_utils_json_to_xml (JsonReader *reader, xmlNode *xnode) g_strfreev (members); } } + +static GHashTable +*_zak_utils_json_to_hashtable (JsonNode *jnode, gboolean gvalue) +{ + GHashTable *ret; + + JsonObject *obj; + GList *membs; + + const gchar *value; + GValue *gval; + + if (jnode != NULL + && JSON_NODE_HOLDS_OBJECT (jnode)) + { + ret = g_hash_table_new (g_str_hash, g_str_equal); + + obj = json_node_get_object (jnode); + membs = json_object_get_members (obj); + + while (membs) + { + if (gvalue) + { + gval = zak_utils_gvalue_new_string (json_object_get_string_member (obj, (gchar *)membs->data)); + g_hash_table_insert (ret, + g_strdup ((gchar *)membs->data), + gval); + } + else + { + value = json_object_get_string_member (obj, (gchar *)membs->data); + g_hash_table_insert (ret, + g_strdup ((gchar *)membs->data), + g_strdup (value)); + } + + membs = g_list_next (membs); + } + + g_list_free (membs); + } + else + { + ret = NULL; + } + + return ret; +} + +GHashTable +*zak_utils_json_to_hashtable (JsonNode *jnode) +{ + return _zak_utils_json_to_hashtable (jnode, FALSE); +} + +GHashTable +*zak_utils_json_to_hashtable_gvalue (JsonNode *jnode) +{ + return _zak_utils_json_to_hashtable (jnode, TRUE); +} diff --git a/src/json.h b/src/json.h index 2c5cefa..5eb6986 100644 --- a/src/json.h +++ b/src/json.h @@ -46,6 +46,9 @@ void zak_utils_json_set_boolean (JsonBuilder *builder, const gchar *name, gboole void zak_utils_json_to_xml (JsonReader *reader, xmlNode *xnode); +GHashTable *zak_utils_json_to_hashtable (JsonNode *jnode); +GHashTable *zak_utils_json_to_hashtable_gvalue (JsonNode *jnode); + G_END_DECLS