summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2008-11-28 17:24:15 +0000
committerEmmanuele Bassi <ebassi@gnome.org>2008-11-28 17:24:15 +0000
commit1d92c73bc05423872581d513f355783d4864edd5 (patch)
tree34d869ab24fd4a4deafcc9d24aa276037ed392bb
parent6e7958785096806b3ffa60a4937642d713a051f2 (diff)
downloadjson-glib-error-location.tar.gz
Abstract the loading code into its own functionerror-location
The load_from_file() method must set the is_filename/filename fields of the JsonParserPrivate structure, so that the error handler can use them to print out the file, as well as the line in case of error. Since load_from_data() needs to unset those two fields, to avoid printing invalid/stale information, we need to have a generic "load" function that can be invoked by both load_from_data() and load_from_file(), and leave the JsonParser object set up to those two methods. Hence, a private json_parser_load() has been added, moving most of the code out of json_parser_load_from_data(). This function does not perform type checks and requires that the length of the memory buffer containing the JSON data stream is already a positive integer.
-rw-r--r--json-glib/json-parser.c175
1 files changed, 103 insertions, 72 deletions
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
index bd1d2e9..bb82938 100644
--- a/json-glib/json-parser.c
+++ b/json-glib/json-parser.c
@@ -901,89 +901,24 @@ json_parser_new (void)
return g_object_new (JSON_TYPE_PARSER, NULL);
}
-/**
- * json_parser_load_from_file:
- * @parser: a #JsonParser
- * @filename: the path for the file to parse
- * @error: return location for a #GError, or %NULL
- *
- * Loads a JSON stream from the content of @filename and parses it. See
- * json_parser_load_from_data().
- *
- * Return value: %TRUE if the file was successfully loaded and parsed.
- * In case of error, @error is set accordingly and %FALSE is returned
- */
-gboolean
-json_parser_load_from_file (JsonParser *parser,
- const gchar *filename,
- GError **error)
+static gboolean
+json_parser_load (JsonParser *parser,
+ const gchar *data,
+ gsize length,
+ GError **error)
{
- GError *internal_error;
- gchar *data;
- gsize length;
- gboolean retval = TRUE;
-
- g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
- g_return_val_if_fail (filename != NULL, FALSE);
-
- internal_error = NULL;
- if (!g_file_get_contents (filename, &data, &length, &internal_error))
- {
- g_propagate_error (error, internal_error);
- return FALSE;
- }
-
- if (!json_parser_load_from_data (parser, data, length, &internal_error))
- {
- g_propagate_error (error, internal_error);
- retval = FALSE;
- }
-
- g_free (data);
-
- return retval;
-}
-
-/**
- * json_parser_load_from_data:
- * @parser: a #JsonParser
- * @data: the buffer to parse
- * @length: the length of the buffer, or -1
- * @error: return location for a #GError, or %NULL
- *
- * Loads a JSON stream from a buffer and parses it. You can call this function
- * multiple times with the same #JsonParser object, but the contents of the
- * parser will be destroyed each time.
- *
- * Return value: %TRUE if the buffer was succesfully parser. In case
- * of error, @error is set accordingly and %FALSE is returned
- */
-gboolean
-json_parser_load_from_data (JsonParser *parser,
- const gchar *data,
- gssize length,
- GError **error)
-{
- JsonParserPrivate *priv;
+ JsonParserPrivate *priv = parser->priv;
JsonScanner *scanner;
gboolean done;
gboolean retval = TRUE;
gint i;
- g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
- g_return_val_if_fail (data != NULL, FALSE);
-
json_parser_clear (parser);
- if (length < 0)
- length = strlen (data);
-
- priv = parser->priv;
-
scanner = json_scanner_create (parser);
json_scanner_input_text (scanner, data, length);
- parser->priv->scanner = scanner;
+ priv->scanner = scanner;
g_signal_emit (parser, parser_signals[PARSE_START], 0);
@@ -1065,6 +1000,102 @@ json_parser_load_from_data (JsonParser *parser,
}
/**
+ * json_parser_load_from_file:
+ * @parser: a #JsonParser
+ * @filename: the path for the file to parse
+ * @error: return location for a #GError, or %NULL
+ *
+ * Loads a JSON stream from the content of @filename and parses it. See
+ * json_parser_load_from_data().
+ *
+ * Return value: %TRUE if the file was successfully loaded and parsed.
+ * In case of error, @error is set accordingly and %FALSE is returned
+ */
+gboolean
+json_parser_load_from_file (JsonParser *parser,
+ const gchar *filename,
+ GError **error)
+{
+ JsonParserPrivate *priv;
+ GError *internal_error;
+ gchar *data;
+ gsize length;
+ gboolean retval = TRUE;
+
+ g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
+ g_return_val_if_fail (filename != NULL, FALSE);
+
+ priv = parser->priv;
+
+ internal_error = NULL;
+ if (!g_file_get_contents (filename, &data, &length, &internal_error))
+ {
+ g_propagate_error (error, internal_error);
+ return FALSE;
+ }
+
+ g_free (priv->filename);
+
+ priv->is_filename = TRUE;
+ priv->filename = g_strdup (filename);
+
+ if (!json_parser_load (parser, data, length, &internal_error))
+ {
+ g_propagate_error (error, internal_error);
+ retval = FALSE;
+ }
+
+ g_free (data);
+
+ return retval;
+}
+
+/**
+ * json_parser_load_from_data:
+ * @parser: a #JsonParser
+ * @data: the buffer to parse
+ * @length: the length of the buffer, or -1
+ * @error: return location for a #GError, or %NULL
+ *
+ * Loads a JSON stream from a buffer and parses it. You can call this function
+ * multiple times with the same #JsonParser object, but the contents of the
+ * parser will be destroyed each time.
+ *
+ * Return value: %TRUE if the buffer was succesfully parser. In case
+ * of error, @error is set accordingly and %FALSE is returned
+ */
+gboolean
+json_parser_load_from_data (JsonParser *parser,
+ const gchar *data,
+ gssize length,
+ GError **error)
+{
+ JsonParserPrivate *priv;
+ GError *internal_error;
+ gboolean retval = TRUE;
+
+ g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
+ g_return_val_if_fail (data != NULL, FALSE);
+
+ priv = parser->priv;
+
+ if (length < 0)
+ length = strlen (data);
+
+ priv->is_filename = FALSE;
+ g_free (priv->filename);
+
+ internal_error = NULL;
+ if (!json_parser_load (parser, data, length, &internal_error))
+ {
+ g_propagate_error (error, internal_error);
+ retval = FALSE;
+ }
+
+ return retval;
+}
+
+/**
* json_parser_get_root:
* @parser: a #JsonParser
*