summaryrefslogtreecommitdiff
path: root/json-glib/json-parser.c
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@openedhand.com>2007-09-20 17:33:28 +0100
committerEmmanuele Bassi <ebassi@openedhand.com>2007-09-20 20:35:49 +0100
commitc5bfb22f964b8f1feecdc8fb29d8a74b36861d32 (patch)
tree15f05b52f97eaf01d11ea0046f8b12b982060209 /json-glib/json-parser.c
downloadjson-glib-c5bfb22f964b8f1feecdc8fb29d8a74b36861d32.tar.gz
Initial import of JSON-GLib
JSON-GLib is a JSON parser library written with GLib and GObject. JSON is the JavaScript Object Notation, and it's used to define objects and object hierarchies in a human-readable way.
Diffstat (limited to 'json-glib/json-parser.c')
-rw-r--r--json-glib/json-parser.c152
1 files changed, 152 insertions, 0 deletions
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
new file mode 100644
index 0000000..f44451f
--- /dev/null
+++ b/json-glib/json-parser.c
@@ -0,0 +1,152 @@
+#include "config.h"
+
+#include <string.h>
+
+#include "json-parser.h"
+#include "json-private.h"
+
+GQuark
+json_parser_error_quark (void)
+{
+ return g_quark_from_static_string ("json-parser-error");
+}
+
+#define JSON_PARSER_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE ((obj), JSON_TYPE_PARSER, JsonParserPrivate))
+
+struct _JsonParserPrivate
+{
+ GList *top_levels;
+};
+
+G_DEFINE_TYPE (JsonParser, json_parser, G_TYPE_OBJECT);
+
+static void
+json_parser_dispose (GObject *gobject)
+{
+ G_OBJECT_CLASS (json_parser_parent_class)->dispose (gobject);
+}
+
+static void
+json_parser_class_init (JsonParserClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (JsonParserPrivate));
+
+ gobject_class->dispose = json_parser_dispose;
+}
+
+static void
+json_parser_init (JsonParser *parser)
+{
+ JsonParserPrivate *priv;
+
+ parser->priv = priv = JSON_PARSER_GET_PRIVATE (parser);
+
+ priv->top_levels = NULL;
+}
+
+/**
+ * json_parser_new:
+ *
+ * Creates a new #JsonParser instance. You can use the #JsonParser to
+ * load a JSON stream from either a file or a buffer and then walk the
+ * hierarchy using the data types API.
+ *
+ * Return value: the newly created #JsonParser. Use g_object_unref()
+ * to release all the memory it allocates.
+ */
+JsonParser *
+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.
+ *
+ * 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)
+{
+ 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.
+ *
+ * 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,
+ gsize length,
+ GError **error)
+{
+ g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
+ g_return_val_if_fail (data != NULL, FALSE);
+
+ if (length < 0)
+ length = strlen (data);
+
+ return TRUE;
+}
+
+/**
+ * json_parser_get_toplevels:
+ * @parser: a #JsonParser
+ *
+ * Retrieves the top level entities from the parsed JSON stream.
+ *
+ * Return value: a list of pointers to the top-level entites. The
+ * returned list is owned by the #JsonParser and should never be
+ * modified or freed.
+ */
+GList *
+json_parser_get_toplevels (JsonParser *parser)
+{
+ g_return_val_if_fail (JSON_IS_PARSER (parser), NULL);
+
+ return parser->priv->top_levels;
+}
+