From: Andrea Zagli Date: Sun, 30 Aug 2020 08:17:22 +0000 (+0200) Subject: Added functions ::json_parse_*. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=fe4f205278e814678163c1391bf37903dbabcfd7;p=libzakutilsjsonxml Added functions ::json_parse_*. --- diff --git a/src/json.c b/src/json.c index 4678d0c..3ce409f 100644 --- a/src/json.c +++ b/src/json.c @@ -24,6 +24,74 @@ #include "json.h" +JsonNode +*zak_utils_json_parse_file (const gchar *filename) +{ + JsonParser *parser; + + GError *error; + + JsonNode *ret; + + JsonNode *root; + + parser = json_parser_new (); + + error = NULL; + if (json_parser_load_from_file (parser, filename, &error)) + { + root = json_parser_get_root (parser); + if (root != NULL) + { + ret = json_node_copy (root); + } + } + else + { + g_warning ("Error on parsing json file: %s", + error != NULL && error->message != NULL ? error->message : "no details"); + ret = NULL; + } + + g_object_unref (parser); + + return ret; +} + +JsonNode +*zak_utils_json_parse_data (const gchar *data) +{ + JsonParser *parser; + + GError *error; + + JsonNode *ret; + + JsonNode *root; + + parser = json_parser_new (); + + error = NULL; + if (json_parser_load_from_data (parser, data, -1, &error)) + { + root = json_parser_get_root (parser); + if (root != NULL) + { + ret = json_node_copy (root); + } + } + else + { + g_warning ("Error on parsing json data: %s", + error != NULL && error->message != NULL ? error->message : "no details"); + ret = NULL; + } + + g_object_unref (parser); + + return ret; +} + JsonNode *zak_utils_json_node_new_array () { diff --git a/src/json.h b/src/json.h index 5eb6986..90dbdd2 100644 --- a/src/json.h +++ b/src/json.h @@ -31,6 +31,9 @@ G_BEGIN_DECLS +JsonNode *zak_utils_json_parse_file (const gchar *filename); +JsonNode *zak_utils_json_parse_data (const gchar *data); + JsonNode *zak_utils_json_node_new_array (void); JsonNode *zak_utils_json_node_new_object (void);