[json-glib] generator: Clean up and add accessors
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib] generator: Clean up and add accessors
- Date: Tue, 14 Dec 2010 16:36:22 +0000 (UTC)
commit 9aed6f5a455109186ae56ec1c3e558505644d57f
Author: Emmanuele Bassi <ebassi linux intel com>
Date: Tue Dec 14 16:26:10 2010 +0000
generator: Clean up and add accessors
Instead of asking everyone to use g_object_set() to set up a
JsonGenerator the class should provide a decent API for its properties.
While we're at it, we should also use modern API for installing and
notifying of property changes.
doc/reference/json-glib-sections.txt | 11 ++-
json-glib/json-generator.c | 256 ++++++++++++++++++++++++++++------
json-glib/json-generator.h | 37 +++--
3 files changed, 247 insertions(+), 57 deletions(-)
---
diff --git a/doc/reference/json-glib-sections.txt b/doc/reference/json-glib-sections.txt
index acb86b3..9a93e05 100644
--- a/doc/reference/json-glib-sections.txt
+++ b/doc/reference/json-glib-sections.txt
@@ -178,10 +178,19 @@ json_parser_error_quark
JsonGenerator
JsonGeneratorClass
json_generator_new
+json_generator_set_root
+json_generator_get_root
+json_generator_set_pretty
+json_generator_get_pretty
+json_generator_set_indent
+json_generator_get_indent
+json_generator_set_indent_char
+json_generator_get_indent_char
+
+<SUBSECTION>
json_generator_to_file
json_generator_to_data
json_generator_to_stream
-json_generator_set_root
<SUBSECTION Standard>
JSON_TYPE_GENERATOR
diff --git a/json-glib/json-generator.c b/json-glib/json-generator.c
index 7b41c40..22fc6b3 100644
--- a/json-glib/json-generator.c
+++ b/json-glib/json-generator.c
@@ -61,7 +61,9 @@ enum
PROP_PRETTY,
PROP_INDENT,
PROP_ROOT,
- PROP_INDENT_CHAR
+ PROP_INDENT_CHAR,
+
+ PROP_LAST
};
static gchar *dump_value (JsonGenerator *generator,
@@ -105,6 +107,8 @@ static const char json_exceptions[] = {
'\0' /* g_strescape() expects a NUL-terminated string */
};
+static GParamSpec *generator_props[PROP_LAST] = { NULL, };
+
G_DEFINE_TYPE (JsonGenerator, json_generator, G_TYPE_OBJECT);
static gchar *
@@ -130,23 +134,26 @@ json_generator_set_property (GObject *gobject,
const GValue *value,
GParamSpec *pspec)
{
- JsonGeneratorPrivate *priv = JSON_GENERATOR_GET_PRIVATE (gobject);
+ JsonGenerator *generator = JSON_GENERATOR (gobject);
switch (prop_id)
{
case PROP_PRETTY:
- priv->pretty = g_value_get_boolean (value);
+ json_generator_set_pretty (generator, g_value_get_boolean (value));
break;
+
case PROP_INDENT:
- priv->indent = g_value_get_uint (value);
+ json_generator_set_indent (generator, g_value_get_uint (value));
break;
+
case PROP_INDENT_CHAR:
- priv->indent_char = g_value_get_uint (value);
+ json_generator_set_indent_char (generator, g_value_get_uint (value));
break;
+
case PROP_ROOT:
- json_generator_set_root (JSON_GENERATOR (gobject),
- g_value_get_boxed (value));
+ json_generator_set_root (generator, g_value_get_boxed (value));
break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
@@ -159,7 +166,7 @@ json_generator_get_property (GObject *gobject,
GValue *value,
GParamSpec *pspec)
{
- JsonGeneratorPrivate *priv = JSON_GENERATOR_GET_PRIVATE (gobject);
+ JsonGeneratorPrivate *priv = JSON_GENERATOR (gobject)->priv;
switch (prop_id)
{
@@ -188,10 +195,6 @@ json_generator_class_init (JsonGeneratorClass *klass)
g_type_class_add_private (klass, sizeof (JsonGeneratorPrivate));
- gobject_class->set_property = json_generator_set_property;
- gobject_class->get_property = json_generator_get_property;
- gobject_class->finalize = json_generator_finalize;
-
/**
* JsonGenerator:pretty:
*
@@ -199,26 +202,26 @@ json_generator_class_init (JsonGeneratorClass *klass)
* newlines. The indentation level can be controlled by using the
* JsonGenerator:indent property
*/
- g_object_class_install_property (gobject_class,
- PROP_PRETTY,
- g_param_spec_boolean ("pretty",
- "Pretty",
- "Pretty-print the output",
- FALSE,
- G_PARAM_READWRITE));
+ generator_props[PROP_PRETTY] =
+ g_param_spec_boolean ("pretty",
+ "Pretty",
+ "Pretty-print the output",
+ FALSE,
+ G_PARAM_READWRITE);
+
/**
* JsonGenerator:indent:
*
* Number of spaces to be used to indent when pretty printing.
*/
- g_object_class_install_property (gobject_class,
- PROP_INDENT,
- g_param_spec_uint ("indent",
- "Indent",
- "Number of indentation spaces",
- 0, G_MAXUINT,
- 2,
- G_PARAM_READWRITE));
+ generator_props[PROP_INDENT] =
+ g_param_spec_uint ("indent",
+ "Indent",
+ "Number of indentation spaces",
+ 0, G_MAXUINT,
+ 2,
+ G_PARAM_READWRITE);
+
/**
* JsonGenerator:root:
*
@@ -227,13 +230,13 @@ json_generator_class_init (JsonGeneratorClass *klass)
*
* Since: 0.4
*/
- g_object_class_install_property (gobject_class,
- PROP_ROOT,
- g_param_spec_boxed ("root",
- "Root",
- "Root of the JSON data tree",
- JSON_TYPE_NODE,
- G_PARAM_READWRITE));
+ generator_props[PROP_ROOT] =
+ g_param_spec_boxed ("root",
+ "Root",
+ "Root of the JSON data tree",
+ JSON_TYPE_NODE,
+ G_PARAM_READWRITE);
+
/**
* JsonGenerator:indent-char:
*
@@ -241,13 +244,17 @@ json_generator_class_init (JsonGeneratorClass *klass)
*
* Since: 0.6
*/
- g_object_class_install_property (gobject_class,
- PROP_INDENT_CHAR,
- g_param_spec_unichar ("indent-char",
- "Indent Char",
- "Character that should be used when indenting",
- ' ',
- G_PARAM_READWRITE));
+ generator_props[PROP_INDENT_CHAR] =
+ g_param_spec_unichar ("indent-char",
+ "Indent Char",
+ "Character that should be used when indenting",
+ ' ',
+ G_PARAM_READWRITE);
+
+ gobject_class->set_property = json_generator_set_property;
+ gobject_class->get_property = json_generator_get_property;
+ gobject_class->finalize = json_generator_finalize;
+ g_object_class_install_properties (gobject_class, PROP_LAST, generator_props);
}
static void
@@ -677,12 +684,175 @@ json_generator_set_root (JsonGenerator *generator,
{
g_return_if_fail (JSON_IS_GENERATOR (generator));
- if (generator->priv->root)
+ if (generator->priv->root != NULL)
{
json_node_free (generator->priv->root);
generator->priv->root = NULL;
}
- if (node)
+ if (node != NULL)
generator->priv->root = json_node_copy (node);
+
+ g_object_notify_by_pspec (G_OBJECT (generator), generator_props[PROP_ROOT]);
+}
+
+/**
+ * json_generator_get_root:
+ * @generator: a #JsonGenerator
+ *
+ * Retrieves a pointer to the root #JsonNode set using
+ * json_generator_set_root().
+ *
+ * Return value: (transfer none): a #JsonNode, or %NULL. The returned node
+ * is owned by the #JsonGenerator and it should not be freed
+ *
+ * Since: 0.14
+ */
+JsonNode *
+json_generator_get_root (JsonGenerator *generator)
+{
+ g_return_val_if_fail (JSON_IS_GENERATOR (generator), NULL);
+
+ return generator->priv->root;
+}
+
+/**
+ * json_generator_set_pretty:
+ * @generator: a #JsonGenerator
+ * @is_pretty: whether the generated string should be pretty printed
+ *
+ * Sets whether the generated JSON should be pretty printed, using the
+ * indentation character specified in the #JsonGenerator:indent-char
+ * property and the spacing specified in #JsonGenerator:indent property.
+ *
+ * Since: 0.14
+ */
+void
+json_generator_set_pretty (JsonGenerator *generator,
+ gboolean is_pretty)
+{
+ JsonGeneratorPrivate *priv;
+
+ g_return_if_fail (JSON_IS_GENERATOR (generator));
+
+ priv = generator->priv;
+
+ is_pretty = !!is_pretty;
+
+ if (priv->pretty != is_pretty)
+ {
+ priv->pretty = is_pretty;
+
+ g_object_notify_by_pspec (G_OBJECT (generator), generator_props[PROP_PRETTY]);
+ }
+}
+
+/**
+ * json_generator_get_pretty:
+ * @generator: a #JsonGenerator
+ *
+ * Retrieves the value set using json_generator_set_pretty().
+ *
+ * Return value: %TRUE if the generated JSON should be pretty-printed, and
+ * %FALSE otherwise
+ *
+ * Since: 0.14
+ */
+gboolean
+json_generator_get_pretty (JsonGenerator *generator)
+{
+ g_return_val_if_fail (JSON_IS_GENERATOR (generator), FALSE);
+
+ return generator->priv->pretty;
+}
+
+/**
+ * json_generator_set_indent:
+ * @generator: a #JsonGenerator
+ * @indent_level: the number of repetitions of the indentation character
+ * that should be applied when pretty printing
+ *
+ * Sets the number of repetitions for each indentation level.
+ *
+ * Since: 0.14
+ */
+void
+json_generator_set_indent (JsonGenerator *generator,
+ guint indent_level)
+{
+ JsonGeneratorPrivate *priv;
+
+ g_return_if_fail (JSON_IS_GENERATOR (generator));
+
+ priv = generator->priv;
+
+ if (priv->indent != indent_level)
+ {
+ priv->indent = indent_level;
+
+ g_object_notify_by_pspec (G_OBJECT (generator), generator_props[PROP_INDENT]);
+ }
+}
+
+/**
+ * json_generator_get_indent:
+ * @generator: a #JsonGenerator
+ *
+ * Retrieves the value set using json_generator_set_indent().
+ *
+ * Return value: the number of repetitions per indentation level
+ *
+ * Since: 0.14
+ */
+guint
+json_generator_get_indent (JsonGenerator *generator)
+{
+ g_return_val_if_fail (JSON_IS_GENERATOR (generator), FALSE);
+
+ return generator->priv->indent;
+}
+
+/**
+ * json_generator_set_indent_char:
+ * @generator: a #JsonGenerator
+ * @indent_char: a Unicode character to be used when indenting
+ *
+ * Sets the character to be used when indenting
+ *
+ * Since: 0.14
+ */
+void
+json_generator_set_indent_char (JsonGenerator *generator,
+ gunichar indent_char)
+{
+ JsonGeneratorPrivate *priv;
+
+ g_return_if_fail (JSON_IS_GENERATOR (generator));
+
+ priv = generator->priv;
+
+ if (priv->indent_char != indent_char)
+ {
+ priv->indent_char = indent_char;
+
+ g_object_notify_by_pspec (G_OBJECT (generator), generator_props[PROP_INDENT_CHAR]);
+ }
+}
+
+/**
+ * json_generator_get_indent_char:
+ * @generator: a #JsonGenerator
+ *
+ * Retrieves the value set using json_generator_set_indent_char().
+ *
+ * Return value: the character to be used when indenting
+ *
+ * Since: 0.14
+ */
+gunichar
+json_generator_get_indent_char (JsonGenerator *generator)
+{
+ g_return_val_if_fail (JSON_IS_GENERATOR (generator), FALSE);
+
+ return generator->priv->indent_char;
}
diff --git a/json-glib/json-generator.h b/json-glib/json-generator.h
index 858bbe8..eb7cdba 100644
--- a/json-glib/json-generator.h
+++ b/json-glib/json-generator.h
@@ -77,19 +77,30 @@ struct _JsonGeneratorClass
GType json_generator_get_type (void) G_GNUC_CONST;
-JsonGenerator *json_generator_new (void);
-gchar * json_generator_to_data (JsonGenerator *generator,
- gsize *length);
-gboolean json_generator_to_file (JsonGenerator *generator,
- const gchar *filename,
- GError **error);
-gboolean json_generator_to_stream (JsonGenerator *generator,
- GOutputStream *stream,
- GCancellable *cancellable,
- GError **error);
-
-void json_generator_set_root (JsonGenerator *generator,
- JsonNode *node);
+JsonGenerator * json_generator_new (void);
+
+void json_generator_set_pretty (JsonGenerator *generator,
+ gboolean is_pretty);
+gboolean json_generator_get_pretty (JsonGenerator *generator);
+void json_generator_set_indent (JsonGenerator *generator,
+ guint indent_level);
+guint json_generator_get_indent (JsonGenerator *generator);
+void json_generator_set_indent_char (JsonGenerator *generator,
+ gunichar indent_char);
+gunichar json_generator_get_indent_char (JsonGenerator *generator);
+void json_generator_set_root (JsonGenerator *generator,
+ JsonNode *node);
+JsonNode * json_generator_get_root (JsonGenerator *generator);
+
+gchar * json_generator_to_data (JsonGenerator *generator,
+ gsize *length);
+gboolean json_generator_to_file (JsonGenerator *generator,
+ const gchar *filename,
+ GError **error);
+gboolean json_generator_to_stream (JsonGenerator *generator,
+ GOutputStream *stream,
+ GCancellable *cancellable,
+ GError **error);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]