[geocode-glib] lib: Move json parsers to the objects



commit a63db4fb411e0606c0f0575b7002bad4942c870f
Author: Bastien Nocera <hadess hadess net>
Date:   Sat Nov 24 21:30:26 2012 +0100

    lib: Move json parsers to the objects

 geocode-glib/geocode-forward.c |   12 +-
 geocode-glib/geocode-glib.c    |  260 ----------------------------------------
 geocode-glib/geocode-reverse.c |    8 +-
 3 files changed, 9 insertions(+), 271 deletions(-)
---
diff --git a/geocode-glib/geocode-forward.c b/geocode-glib/geocode-forward.c
index 9150e73..fb454a9 100644
--- a/geocode-glib/geocode-forward.c
+++ b/geocode-glib/geocode-forward.c
@@ -51,8 +51,6 @@ G_DEFINE_TYPE (GeocodeForward, geocode_forward, G_TYPE_OBJECT)
 static void geocode_forward_add (GeocodeForward *forward,
 				 const char     *key,
 				 const char     *value);
-static GList *__geocode_parse_search_json (const char *contents,
-					  GError    **error);
 
 static void
 geocode_forward_finalize (GObject *gforward)
@@ -316,7 +314,7 @@ on_query_data_loaded (GObject      *source_forward,
 		return;
 	}
 
-	ret = __geocode_parse_search_json (contents, &error);
+	ret = _geocode_parse_search_json (contents, &error);
 
 	if (ret == NULL) {
 		g_simple_async_result_set_from_error (simple, error);
@@ -366,7 +364,7 @@ on_cache_data_loaded (GObject      *source_forward,
 		return;
 	}
 
-	ret = __geocode_parse_search_json (contents, &error);
+	ret = _geocode_parse_search_json (contents, &error);
 	g_free (contents);
 
 	if (ret == NULL) {
@@ -606,8 +604,8 @@ new_location_from_result (GHashTable *ht)
 	return loc;
 }
 
-static GList *
-__geocode_parse_search_json (const char *contents,
+GList *
+_geocode_parse_search_json (const char *contents,
 			     GError    **error)
 {
 	GList *ret;
@@ -717,7 +715,7 @@ geocode_forward_search (GeocodeForward      *forward,
 		to_cache = TRUE;
 	}
 
-	ret = __geocode_parse_search_json (contents, error);
+	ret = _geocode_parse_search_json (contents, error);
 	if (to_cache && ret != NULL)
 		_geocode_glib_cache_save (query, contents);
 
diff --git a/geocode-glib/geocode-glib.c b/geocode-glib/geocode-glib.c
index 729d5c3..a1cf07c 100644
--- a/geocode-glib/geocode-glib.c
+++ b/geocode-glib/geocode-glib.c
@@ -422,148 +422,6 @@ geocode_object_add (GeocodeObject *object,
 			     g_strdup (value));
 }
 
-GHashTable *
-_geocode_parse_resolve_json (const char *contents,
-			     GError    **error)
-{
-	GHashTable *ret;
-	JsonParser *parser;
-	JsonNode *root;
-	JsonReader *reader;
-	gint64 err_code, found;
-	guint i;
-	const GError *err = NULL;
-	char **members;
-
-	ret = NULL;
-
-	parser = json_parser_new ();
-	if (json_parser_load_from_data (parser, contents, -1, error) == FALSE) {
-		g_object_unref (parser);
-		return ret;
-	}
-
-	root = json_parser_get_root (parser);
-	reader = json_reader_new (root);
-
-	if (json_reader_read_member (reader, "ResultSet") == FALSE)
-		goto parse;
-
-	if (json_reader_read_member (reader, "Error") == FALSE)
-		goto parse;
-
-	err_code = json_reader_get_int_value (reader);
-	json_reader_end_member (reader);
-
-	if (err_code != 0) {
-		const char *msg;
-
-		json_reader_read_member (reader, "ErrorMessage");
-		msg = json_reader_get_string_value (reader);
-		json_reader_end_member (reader);
-		if (msg && *msg == '\0')
-			msg = NULL;
-
-		switch (err_code) {
-		case 1:
-			g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NOT_SUPPORTED, msg ? msg : "Query not supported");
-			break;
-		case 100:
-			g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NOT_SUPPORTED, msg ? msg : "No input parameters");
-			break;
-		case 102:
-			g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NOT_SUPPORTED, msg ? msg : "Address data not recognized as valid UTF-8");
-			break;
-		case 103:
-			g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NOT_SUPPORTED, msg ? msg : "Insufficient address data");
-			break;
-		case 104:
-			g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NOT_SUPPORTED, msg ? msg : "Unknown language");
-			break;
-		case 105:
-			g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NOT_SUPPORTED, msg ? msg : "No country detected");
-			break;
-		case 106:
-			g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NOT_SUPPORTED, msg ? msg : "Country not supported");
-			break;
-		default:
-			if (msg == NULL)
-				g_set_error (error, GEOCODE_ERROR, GEOCODE_ERROR_PARSE, "Unknown error code %"G_GINT64_FORMAT, err_code);
-			else
-				g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_PARSE, msg);
-			break;
-		}
-		g_object_unref (parser);
-		g_object_unref (reader);
-		return NULL;
-	}
-
-	/* Check for the number of results */
-	if (json_reader_read_member (reader, "Found") == FALSE)
-		goto parse;
-
-	found = json_reader_get_int_value (reader);
-	json_reader_end_member (reader);
-
-	if (!found) {
-		g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NO_MATCHES, "No matches found for request");
-		g_object_unref (parser);
-		g_object_unref (reader);
-		return NULL;
-	}
-
-	if (json_reader_read_member (reader, "Results") == FALSE)
-		goto parse;
-
-	if (json_reader_read_element (reader, 0) == FALSE)
-		goto parse;
-
-	members = json_reader_list_members (reader);
-
-	/* Yay, start adding data */
-	ret = g_hash_table_new_full (g_str_hash, g_str_equal,
-				     g_free, g_free);
-
-	for (i = 0; members[i] != NULL; i++) {
-		const char *value;
-
-		json_reader_read_member (reader, members[i]);
-
-		if (g_str_equal (members[i], "radius") ||
-		    g_str_equal (members[i], "quality")) {
-			gint64 num;
-
-			num = json_reader_get_int_value (reader);
-			g_hash_table_insert (ret, g_strdup (members[i]), g_strdup_printf ("%"G_GINT64_FORMAT, num));
-			json_reader_end_member (reader);
-			continue;
-		}
-
-		value = json_reader_get_string_value (reader);
-		if (value && *value == '\0')
-			value = NULL;
-
-		if (value != NULL)
-			g_hash_table_insert (ret, g_strdup (members[i]), g_strdup (value));
-		json_reader_end_member (reader);
-	}
-	g_strfreev (members);
-
-	g_object_unref (parser);
-	g_object_unref (reader);
-
-	return ret;
-
-parse:
-	if (ret != NULL)
-		g_hash_table_destroy (ret);
-	err = json_reader_get_error (reader);
-	g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_PARSE, err->message);
-	g_object_unref (parser);
-	g_object_unref (reader);
-	return NULL;
-}
-
 static gboolean
 query_is_search (GFile *query)
 {
@@ -1025,124 +883,6 @@ out:
 	return ret;
 }
 
-#define IS_EL(x) (g_str_equal (element_name, x))
-
-static void
-insert_place_attr (GHashTable *ht,
-		   JsonReader *reader,
-		   const char *element_name)
-{
-	char *value;
-
-	if (json_reader_read_member (reader, element_name) == FALSE) {
-		json_reader_end_member (reader);
-		return;
-	}
-
-	/* FIXME: check all the member names against what Place Finder outputs */
-
-	if (IS_EL("woeid") ||
-	    IS_EL("popRank") ||
-	    IS_EL("areaRank")) {
-		value = g_strdup_printf ("%"G_GINT64_FORMAT, json_reader_get_int_value (reader));
-	} else if (IS_EL("centroid")) {
-		json_reader_read_member (reader, "longitude");
-		g_hash_table_insert (ht, g_strdup ("longitude"),
-				     g_strdup_printf ("%lf", json_reader_get_double_value (reader)));
-		json_reader_end_member (reader);
-		json_reader_read_member (reader, "latitude");
-		g_hash_table_insert (ht, g_strdup ("latitude"),
-				     g_strdup_printf ("%lf", json_reader_get_double_value (reader)));
-		json_reader_end_member (reader);
-		goto end;
-	} else if (g_str_has_suffix (element_name, " attrs")) {
-		g_debug ("Ignoring attributes element '%s'", element_name);
-		value = g_strdup (""); /* So that they're ignored */
-	} else if (IS_EL("boundingBox")) {
-		g_debug ("Ignoring element '%s'", element_name);
-		value = g_strdup (""); /* So that they're ignored */
-	} else {
-		value = g_strdup (json_reader_get_string_value (reader));
-	}
-
-	if (value != NULL && *value == '\0') {
-		g_clear_pointer (&value, g_free);
-		goto end;
-	}
-
-	if (value != NULL)
-		g_hash_table_insert (ht, g_strdup (element_name), value);
-	else
-		g_warning ("Ignoring element %s, don't know how to parse it", element_name);
-
-end:
-	json_reader_end_member (reader);
-}
-
-GList *
-_geocode_parse_search_json (const char *contents,
-			    GError    **error)
-{
-	GList *ret;
-	JsonParser *parser;
-	JsonNode *root;
-	JsonReader *reader;
-	const GError *err = NULL;
-	int num_places, i;
-
-	ret = NULL;
-
-	parser = json_parser_new ();
-	if (json_parser_load_from_data (parser, contents, -1, error) == FALSE) {
-		g_object_unref (parser);
-		return ret;
-	}
-
-	root = json_parser_get_root (parser);
-	reader = json_reader_new (root);
-
-	if (json_reader_read_member (reader, "places") == FALSE)
-		goto parse;
-	if (json_reader_read_member (reader, "place") == FALSE)
-		goto parse;
-
-	num_places = json_reader_count_elements (reader);
-	if (num_places < 0)
-		goto parse;
-
-	for (i = 0; i < num_places; i++) {
-		GHashTable *ht;
-		char **members;
-		int j;
-
-		json_reader_read_element (reader, i);
-
-		ht = g_hash_table_new_full (g_str_hash, g_str_equal,
-					    g_free, g_free);
-
-		members = json_reader_list_members (reader);
-		for (j = 0; members != NULL && members[j] != NULL; j++)
-			insert_place_attr (ht, reader, members[j]);
-		g_strfreev (members);
-
-		json_reader_end_element (reader);
-
-		ret = g_list_prepend (ret, ht);
-	}
-
-	g_object_unref (parser);
-	g_object_unref (reader);
-	ret = g_list_reverse (ret);
-
-	return ret;
-parse:
-	err = json_reader_get_error (reader);
-	g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_PARSE, err->message);
-	g_object_unref (parser);
-	g_object_unref (reader);
-	return NULL;
-}
-
 /**
  * geocode_object_search:
  * @object: a #GeocodeObject representing a query
diff --git a/geocode-glib/geocode-reverse.c b/geocode-glib/geocode-reverse.c
index 9f936e1..257166b 100644
--- a/geocode-glib/geocode-reverse.c
+++ b/geocode-glib/geocode-reverse.c
@@ -191,8 +191,8 @@ geocode_reverse_new_for_location (GeocodeLocation *location)
 	return object;
 }
 
-static GHashTable *
-__geocode_parse_resolve_json (const char *contents,
+GHashTable *
+_geocode_parse_resolve_json (const char *contents,
 			     GError    **error)
 {
 	GHashTable *ret;
@@ -358,7 +358,7 @@ on_query_data_loaded (GObject      *source_object,
 		return;
 	}
 
-	ret = __geocode_parse_resolve_json (contents, &error);
+	ret = _geocode_parse_resolve_json (contents, &error);
 
 	if (ret == NULL) {
 		g_simple_async_result_set_from_error (simple, error);
@@ -408,7 +408,7 @@ on_cache_data_loaded (GObject      *source_object,
 		return;
 	}
 
-	ret = __geocode_parse_resolve_json (contents, &error);
+	ret = _geocode_parse_resolve_json (contents, &error);
 	g_free (contents);
 
 	if (ret == NULL) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]