summaryrefslogtreecommitdiff
path: root/json-glib/json-parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'json-glib/json-parser.c')
-rw-r--r--json-glib/json-parser.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
index a90bbd8..3d77975 100644
--- a/json-glib/json-parser.c
+++ b/json-glib/json-parser.c
@@ -1089,6 +1089,9 @@ json_parser_load (JsonParser *parser,
* Loads a JSON stream from the content of @filename and parses it. See
* json_parser_load_from_data().
*
+ * If the file is large or shared between processes,
+ * json_parser_load_from_mapped_file() may be a more efficient way to load it.
+ *
* Return value: %TRUE if the file was successfully loaded and parsed.
* In case of error, @error is set accordingly and %FALSE is returned
*/
@@ -1132,6 +1135,61 @@ json_parser_load_from_file (JsonParser *parser,
}
/**
+ * json_parser_load_from_mapped_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. Unlike
+ * json_parser_load_from_file(), @filename will be memory mapped as read-only
+ * and parsed. @filename will be unmapped before this function returns.
+ *
+ * If mapping or reading the file fails, a %G_FILE_ERROR will be returned.
+ *
+ * Return value: %TRUE if the file was successfully loaded and parsed.
+ * In case of error, @error is set accordingly and %FALSE is returned
+ * Since: 1.6
+ */
+gboolean
+json_parser_load_from_mapped_file (JsonParser *parser,
+ const gchar *filename,
+ GError **error)
+{
+ JsonParserPrivate *priv;
+ GError *internal_error = NULL;
+ gboolean retval = TRUE;
+ GMappedFile *mapped_file = NULL;
+
+ g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
+ g_return_val_if_fail (filename != NULL, FALSE);
+
+ priv = parser->priv;
+
+ mapped_file = g_mapped_file_new (filename, FALSE, &internal_error);
+ if (mapped_file == NULL)
+ {
+ 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, g_mapped_file_get_contents (mapped_file),
+ g_mapped_file_get_length (mapped_file), &internal_error))
+ {
+ g_propagate_error (error, internal_error);
+ retval = FALSE;
+ }
+
+ g_clear_pointer (&mapped_file, g_mapped_file_unref);
+
+ return retval;
+}
+
+/**
* json_parser_load_from_data:
* @parser: a #JsonParser
* @data: the buffer to parse