[json-glib/wip/g-property] Use GProperty



commit 3e6fc54b662d55c4748a4c63669fef4f2ef020a0
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Thu Jun 2 14:46:35 2011 +0100

    Use GProperty
    
    Instead of the GParamSpec API.

 configure.ac                     |    2 +-
 json-glib/json-generator.c       |  235 ++++++++------------------------------
 json-glib/json-reader.c          |  107 +++++------------
 json-glib/json-reader.h          |    1 +
 json-glib/tests/generator-test.c |   40 +++++++
 5 files changed, 121 insertions(+), 264 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 00e1aa9..92413d6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -20,7 +20,7 @@ m4_define([lt_current], [m4_eval(100 * json_minor_version + json_micro_version -
 m4_define([lt_revision], [json_interface_age])
 m4_define([lt_age], [m4_eval(json_binary_age - json_interface_age)])
 
-m4_define([glib_req_version], [2.26])
+m4_define([glib_req_version], [2.29.5])
 
 AC_PREREQ([2.63])
 
diff --git a/json-glib/json-generator.c b/json-glib/json-generator.c
index 6a36bcf..72c6cf9 100644
--- a/json-glib/json-generator.c
+++ b/json-glib/json-generator.c
@@ -51,19 +51,19 @@ struct _JsonGeneratorPrivate
   guint indent;
   gunichar indent_char;
 
-  guint pretty : 1;
+  gboolean pretty;
 };
 
 enum
 {
-  PROP_0,
+  PROP_INVALID,
 
-  PROP_PRETTY,
-  PROP_INDENT,
-  PROP_ROOT,
-  PROP_INDENT_CHAR,
+  PRETTY,
+  INDENT,
+  ROOT,
+  INDENT_CHAR,
 
-  PROP_LAST
+  LAST_PROP
 };
 
 static gchar *dump_value  (JsonGenerator *generator,
@@ -107,7 +107,7 @@ static const char json_exceptions[] = {
   '\0'   /* g_strescape() expects a NUL-terminated string */
 };
 
-static GParamSpec *generator_props[PROP_LAST] = { NULL, };
+static GParamSpec *generator_props[LAST_PROP] = { NULL, };
 
 G_DEFINE_TYPE (JsonGenerator, json_generator, G_TYPE_OBJECT);
 
@@ -129,66 +129,6 @@ json_generator_finalize (GObject *gobject)
 }
 
 static void
-json_generator_set_property (GObject      *gobject,
-                             guint         prop_id,
-                             const GValue *value,
-                             GParamSpec   *pspec)
-{
-  JsonGenerator *generator = JSON_GENERATOR (gobject);
-
-  switch (prop_id)
-    {
-    case PROP_PRETTY:
-      json_generator_set_pretty (generator, g_value_get_boolean (value));
-      break;
-
-    case PROP_INDENT:
-      json_generator_set_indent (generator, g_value_get_uint (value));
-      break;
-
-    case PROP_INDENT_CHAR:
-      json_generator_set_indent_char (generator, g_value_get_uint (value));
-      break;
-
-    case PROP_ROOT:
-      json_generator_set_root (generator, g_value_get_boxed (value));
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
-}
-
-static void
-json_generator_get_property (GObject    *gobject,
-                             guint       prop_id,
-                             GValue     *value,
-                             GParamSpec *pspec)
-{
-  JsonGeneratorPrivate *priv = JSON_GENERATOR (gobject)->priv;
-
-  switch (prop_id)
-    {
-    case PROP_PRETTY:
-      g_value_set_boolean (value, priv->pretty);
-      break;
-    case PROP_INDENT:
-      g_value_set_uint (value, priv->indent);
-      break;
-    case PROP_INDENT_CHAR:
-      g_value_set_uint (value, priv->indent_char);
-      break;
-    case PROP_ROOT:
-      g_value_set_boxed (value, priv->root);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
-}
-
-static void
 json_generator_class_init (JsonGeneratorClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
@@ -202,25 +142,21 @@ json_generator_class_init (JsonGeneratorClass *klass)
    * newlines. The indentation level can be controlled by using the
    * JsonGenerator:indent property
    */
-  generator_props[PROP_PRETTY] =
-    g_param_spec_boolean ("pretty",
-                          "Pretty",
-                          "Pretty-print the output",
-                          FALSE,
-                          G_PARAM_READWRITE);
+  generator_props[PRETTY] =
+    g_boolean_property_new ("pretty", G_PROPERTY_READWRITE,
+                            G_STRUCT_OFFSET (JsonGeneratorPrivate, pretty),
+                            NULL, NULL);
 
   /**
    * JsonGenerator:indent:
    *
    * Number of spaces to be used to indent when pretty printing.
    */
-  generator_props[PROP_INDENT] =
-    g_param_spec_uint ("indent",
-                       "Indent",
-                       "Number of indentation spaces",
-                       0, G_MAXUINT,
-                       2,
-                       G_PARAM_READWRITE);
+  generator_props[INDENT] =
+    g_uint_property_new ("indent", G_PROPERTY_READWRITE,
+                         G_STRUCT_OFFSET (JsonGeneratorPrivate, indent),
+                         NULL, NULL);
+  g_property_set_default (G_PROPERTY (generator_props[INDENT]), 2);
 
   /**
    * JsonGenerator:root:
@@ -230,12 +166,12 @@ json_generator_class_init (JsonGeneratorClass *klass)
    *
    * Since: 0.4
    */
-  generator_props[PROP_ROOT] =
-    g_param_spec_boxed ("root",
-                        "Root",
-                        "Root of the JSON data tree",
-                        JSON_TYPE_NODE,
-                        G_PARAM_READWRITE);
+  generator_props[ROOT] =
+    g_boxed_property_new ("root", G_PROPERTY_READWRITE,
+                          G_STRUCT_OFFSET (JsonGeneratorPrivate, root),
+                          NULL, NULL);
+  g_property_set_prerequisite (G_PROPERTY (generator_props[ROOT]),
+                               JSON_TYPE_NODE);
 
   /**
    * JsonGenerator:indent-char:
@@ -244,17 +180,15 @@ json_generator_class_init (JsonGeneratorClass *klass)
    *
    * Since: 0.6
    */
-  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;
+  generator_props[INDENT_CHAR] =
+    g_unichar_property_new ("indent-char", G_PROPERTY_READWRITE,
+                            G_STRUCT_OFFSET (JsonGeneratorPrivate, indent_char),
+                            NULL, NULL);
+  g_property_set_default (G_PROPERTY (generator_props[INDENT_CHAR]), ' ');
+
   gobject_class->finalize = json_generator_finalize;
-  g_object_class_install_properties (gobject_class, PROP_LAST, generator_props);
+
+  g_object_class_install_properties (gobject_class, LAST_PROP, generator_props);
 }
 
 static void
@@ -696,16 +630,7 @@ json_generator_set_root (JsonGenerator *generator,
 {
   g_return_if_fail (JSON_IS_GENERATOR (generator));
 
-  if (generator->priv->root != NULL)
-    {
-      json_node_free (generator->priv->root);
-      generator->priv->root = NULL;
-    }
-
-  if (node != NULL)
-    generator->priv->root = json_node_copy (node);
-
-  g_object_notify_by_pspec (G_OBJECT (generator), generator_props[PROP_ROOT]);
+  g_property_set (G_PROPERTY (generator_props[ROOT]), generator, node);
 }
 
 /**
@@ -723,15 +648,19 @@ json_generator_set_root (JsonGenerator *generator,
 JsonNode *
 json_generator_get_root (JsonGenerator *generator)
 {
+  JsonNode *retval = NULL;
+
   g_return_val_if_fail (JSON_IS_GENERATOR (generator), NULL);
 
-  return generator->priv->root;
+  g_property_get (G_PROPERTY (generator_props[ROOT]), generator, &retval);
+
+  return retval;
 }
 
 /**
  * json_generator_set_pretty:
- * @generator: a #JsonGenerator
- * @is_pretty: whether the generated string should be pretty printed
+ * @self: a #JsonGenerator
+ * @value: 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
@@ -739,29 +668,10 @@ json_generator_get_root (JsonGenerator *generator)
  *
  * 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
+ * @self: a #JsonGenerator
  *
  * Retrieves the value set using json_generator_set_pretty().
  *
@@ -770,41 +680,19 @@ json_generator_set_pretty (JsonGenerator *generator,
  *
  * Since: 0.14
  */
-gboolean
-json_generator_get_pretty (JsonGenerator *generator)
-{
-  g_return_val_if_fail (JSON_IS_GENERATOR (generator), FALSE);
 
-  return generator->priv->pretty;
-}
+G_DEFINE_PROPERTY_GET_SET (JsonGenerator, json_generator, gboolean, pretty)
 
 /**
  * json_generator_set_indent:
- * @generator: a #JsonGenerator
- * @indent_level: the number of repetitions of the indentation character
+ * @self: a #JsonGenerator
+ * @value: 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:
@@ -816,40 +704,17 @@ json_generator_set_indent (JsonGenerator *generator,
  *
  * Since: 0.14
  */
-guint
-json_generator_get_indent (JsonGenerator *generator)
-{
-  g_return_val_if_fail (JSON_IS_GENERATOR (generator), FALSE);
-
-  return generator->priv->indent;
-}
+G_DEFINE_PROPERTY_GET_SET (JsonGenerator, json_generator, guint, indent)
 
 /**
  * json_generator_set_indent_char:
- * @generator: a #JsonGenerator
- * @indent_char: a Unicode character to be used when indenting
+ * @self: a #JsonGenerator
+ * @value: 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:
@@ -861,10 +726,4 @@ json_generator_set_indent_char (JsonGenerator *generator,
  *
  * 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;
-}
+G_DEFINE_PROPERTY_GET_SET (JsonGenerator, json_generator, gunichar, indent_char)
diff --git a/json-glib/json-reader.c b/json-glib/json-reader.c
index 8da7e5e..4549554 100644
--- a/json-glib/json-reader.c
+++ b/json-glib/json-reader.c
@@ -100,14 +100,14 @@ struct _JsonReaderPrivate
 
 enum
 {
-  PROP_0,
+  PROP_INVALID,
 
-  PROP_ROOT,
+  ROOT,
 
-  PROP_LAST
+  LAST_PROP
 };
 
-static GParamSpec *reader_properties[PROP_LAST] = { NULL, };
+static GParamSpec *reader_properties[LAST_PROP] = { NULL, };
 
 G_DEFINE_TYPE (JsonReader, json_reader, G_TYPE_OBJECT);
 
@@ -128,42 +128,6 @@ json_reader_finalize (GObject *gobject)
 }
 
 static void
-json_reader_set_property (GObject      *gobject,
-                          guint         prop_id,
-                          const GValue *value,
-                          GParamSpec   *pspec)
-{
-  switch (prop_id)
-    {
-    case PROP_ROOT:
-      json_reader_set_root (JSON_READER (gobject), g_value_get_boxed (value));
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
-}
-
-static void
-json_reader_get_property (GObject    *gobject,
-                          guint       prop_id,
-                          GValue     *value,
-                          GParamSpec *pspec)
-{
-  switch (prop_id)
-    {
-    case PROP_ROOT:
-      g_value_set_boxed (value, JSON_READER (gobject)->priv->root);
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
-}
-
-static void
 json_reader_class_init (JsonReaderClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
@@ -177,19 +141,14 @@ json_reader_class_init (JsonReaderClass *klass)
    *
    * Since: 0.12
    */
-  reader_properties[PROP_ROOT] =
-    g_param_spec_boxed ("root",
-                        "Root Node",
-                        "The root of the tree to read",
-                        JSON_TYPE_NODE,
-                        G_PARAM_READWRITE |
-                        G_PARAM_CONSTRUCT |
-                        G_PARAM_STATIC_STRINGS);
+  reader_properties[ROOT] =
+    g_boxed_property_new ("root", G_PROPERTY_READWRITE,
+                          G_STRUCT_OFFSET (JsonReaderPrivate, root),
+                          NULL, NULL);
+  g_property_set_prerequisite (G_PROPERTY (reader_properties[ROOT]), JSON_TYPE_NODE);
 
   gobject_class->finalize = json_reader_finalize;
-  gobject_class->set_property = json_reader_set_property;
-  gobject_class->get_property = json_reader_get_property;
-  g_object_class_install_properties (gobject_class, PROP_LAST, reader_properties);
+  g_object_class_install_properties (gobject_class, LAST_PROP, reader_properties);
 }
 
 static void
@@ -238,49 +197,47 @@ json_reader_unset_error (JsonReader *reader)
 
 /**
  * json_reader_set_root:
- * @reader: a #JsonReader
- * @root: (allow-none): a #JsonNode
+ * @self: a #JsonReader
+ * @value: (allow-none): a #JsonNode
  *
- * Sets the root #JsonNode to be read by @reader. The @reader will take
- * a copy of @root
+ * Sets the root #JsonNode to be read by a #JsonReader instance. The
+ * #JsonReader will take a copy of the #JsonNode.
  *
  * If another #JsonNode is currently set as root, it will be replaced.
  *
  * Since: 0.12
  */
 void
-json_reader_set_root (JsonReader *reader,
-                      JsonNode   *root)
+json_reader_set_root (JsonReader *self,
+                      JsonNode   *value)
 {
   JsonReaderPrivate *priv;
 
-  g_return_if_fail (JSON_IS_READER (reader));
-
-  priv = reader->priv;
+  g_return_if_fail (JSON_IS_READER (self));
 
-  if (priv->root == root)
-    return;
-
-  if (priv->root != NULL)
-    {
-      json_node_free (priv->root);
-      priv->root = NULL;
-      priv->current_node = NULL;
-      priv->previous_node = NULL;
-    }
+  priv = self->priv;
 
-  if (root != NULL)
+  if (g_property_set (G_PROPERTY (reader_properties[ROOT]), self, value))
     {
-      priv->root = json_node_copy (root);
       priv->current_node = priv->root;
       priv->previous_node = NULL;
     }
-
-  g_object_notify_by_pspec (G_OBJECT (reader), reader_properties[PROP_ROOT]);
 }
 
+/**
+ * json_reader_get_root:
+ * @self: a #JsonReader
+ *
+ * Retrieves the root #JsonNode used by the #JsonReader.
+ *
+ * Return value: (transfer none): a pointer to the root #JsonNode or %NULL
+ *
+ * Since: 0.14
+ */
+G_DEFINE_PROPERTY_GET (JsonReader, json_reader, JsonNode*, root)
+
 /*
- * json_reader_ser_error:
+ * json_reader_set_error:
  * @reader: a #JsonReader
  * @error_code: the #JsonReaderError code for the error
  * @error_fmt: format string
diff --git a/json-glib/json-reader.h b/json-glib/json-reader.h
index 6cea36b..fdf20bf 100644
--- a/json-glib/json-reader.h
+++ b/json-glib/json-reader.h
@@ -112,6 +112,7 @@ JsonReader *           json_reader_new               (JsonNode     *node);
 
 void                   json_reader_set_root          (JsonReader   *reader,
                                                       JsonNode     *root);
+JsonNode *             json_reader_get_root          (JsonReader   *reader);
 
 G_CONST_RETURN GError *json_reader_get_error         (JsonReader   *reader);
 
diff --git a/json-glib/tests/generator-test.c b/json-glib/tests/generator-test.c
index 5cc8b92..b6e0237 100644
--- a/json-glib/tests/generator-test.c
+++ b/json-glib/tests/generator-test.c
@@ -18,6 +18,12 @@ static const gchar *empty_object = "{}";
 static const gchar *simple_array = "[true,false,null,42,\"foo\"]";
 static const gchar *nested_array = "[true,[false,null],42]";
 
+static const char *pretty_object =
+"{\n"
+"  \"hello\" : \"world\",\n"
+"  \"answer\" : 42\n"
+"}";
+
 static const gchar *simple_object = "{\"Bool1\":true,\"Bool2\":false,\"Null\":null,\"Int\":42,\"String\":\"foo\"}";
 /* taken from the RFC 4627, Examples section */
 static const gchar *nested_object =
@@ -268,6 +274,39 @@ test_nested_object (void)
 }
 
 static void
+test_pretty_object (void)
+{
+  JsonNode *node = json_node_new (JSON_NODE_OBJECT);
+  JsonObject *object = json_object_new ();
+  JsonGenerator *generator = json_generator_new ();
+  gchar *data;
+  gsize len;
+
+  json_object_set_string_member (object, "hello", "world");
+  json_object_set_int_member (object, "answer", 42);
+
+  json_node_take_object (node, object);
+
+  generator = json_generator_new ();
+  json_generator_set_pretty (generator, TRUE);
+  json_generator_set_root (generator, node);
+
+  data = json_generator_to_data (generator, &len);
+
+  if (g_test_verbose ())
+    g_print ("checking pretty printed object `%s' (expected: %s)\n",
+             data,
+             pretty_object);
+
+  g_assert_cmpint (len, ==, strlen (pretty_object));
+  g_assert_cmpstr (data, ==, pretty_object);
+
+  g_free (data);
+  g_object_unref (generator);
+  json_node_free (node);
+}
+
+static void
 test_decimal_separator (void)
 {
   JsonNode *node = json_node_new (JSON_NODE_VALUE);
@@ -324,6 +363,7 @@ main (int   argc,
   g_test_add_func ("/generator/nested-array", test_nested_array);
   g_test_add_func ("/generator/simple-object", test_simple_object);
   g_test_add_func ("/generator/nested-object", test_nested_object);
+  g_test_add_func ("/generator/pretty-object", test_pretty_object);
   g_test_add_func ("/generator/decimal-separator", test_decimal_separator);
 
   return g_test_run ();



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