[json-glib] Use typed initializers for JsonNode



commit f50f32dcfdecd552ca353731faa0fde099569c28
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Oct 27 12:06:28 2012 +0100

    Use typed initializers for JsonNode

 json-glib/json-gobject.c |  115 +++++++++++++++++++++-------------------------
 json-glib/json-parser.c  |   37 ++++++++------
 2 files changed, 73 insertions(+), 79 deletions(-)
---
diff --git a/json-glib/json-gobject.c b/json-glib/json-gobject.c
index 97b0338..b2c86d6 100644
--- a/json-glib/json-gobject.c
+++ b/json-glib/json-gobject.c
@@ -663,44 +663,70 @@ json_serialize_pspec (const GValue *real_value,
                       GParamSpec   *pspec)
 {
   JsonNode *retval = NULL;
-  GValue value = { 0, };
   JsonNodeType node_type;
 
   switch (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (real_value)))
     {
+    /* JSON native types */
     case G_TYPE_INT64:
+      retval = json_node_init_int (json_node_alloc (), g_value_get_int64 (real_value));
+      break;
+
     case G_TYPE_BOOLEAN:
+      retval = json_node_init_boolean (json_node_alloc (), g_value_get_boolean (real_value));
+      break;
+
     case G_TYPE_DOUBLE:
-      /* JSON native types */
-      retval = json_node_new (JSON_NODE_VALUE);
-      g_value_init (&value, G_VALUE_TYPE (real_value));
-      g_value_copy (real_value, &value);
-      json_node_set_value (retval, &value);
-      g_value_unset (&value);
+      retval = json_node_init_double (json_node_alloc (), g_value_get_double (real_value));
       break;
 
     case G_TYPE_STRING:
-      /* strings might be NULL, so we handle it differently */
-      if (!g_value_get_string (real_value))
-        retval = json_node_new (JSON_NODE_NULL);
-      else
-        {
-          retval = json_node_new (JSON_NODE_VALUE);
-          json_node_set_string (retval, g_value_get_string (real_value));
-          break;
-        }
+      retval = json_node_init_string (json_node_alloc (), g_value_get_string (real_value));
       break;
 
+    /* auto-promoted types */
     case G_TYPE_INT:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_int (retval, g_value_get_int (real_value));
+      retval = json_node_init_int (json_node_alloc (), g_value_get_int (real_value));
+      break;
+
+    case G_TYPE_UINT:
+      retval = json_node_init_int (json_node_alloc (), g_value_get_uint (real_value));
+      break;
+
+    case G_TYPE_LONG:
+      retval = json_node_init_int (json_node_alloc (), g_value_get_long (real_value));
+      break;
+
+    case G_TYPE_ULONG:
+      retval = json_node_init_int (json_node_alloc (), g_value_get_ulong (real_value));
       break;
 
     case G_TYPE_FLOAT:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_double (retval, g_value_get_float (real_value));
+      retval = json_node_init_double (json_node_alloc (), g_value_get_float (real_value));
+      break;
+
+    case G_TYPE_CHAR:
+      retval = json_node_alloc ();
+#if GLIB_CHECK_VERSION (2, 31, 0)
+      json_node_init_int (retval, g_value_get_schar (real_value));
+#else
+      json_node_init_int (retval, g_value_get_char (real_value));
+#endif
+      break;
+
+    case G_TYPE_UCHAR:
+      retval = json_node_init_int (json_node_alloc (), g_value_get_uchar (real_value));
+      break;
+
+    case G_TYPE_ENUM:
+      retval = json_node_init_int (json_node_alloc (), g_value_get_enum (real_value));
+      break;
+
+    case G_TYPE_FLAGS:
+      retval = json_node_init_int (json_node_alloc (), g_value_get_flags (real_value));
       break;
 
+    /* complex types */
     case G_TYPE_BOXED:
       if (G_VALUE_HOLDS (real_value, G_TYPE_STRV))
         {
@@ -719,8 +745,8 @@ json_serialize_pspec (const GValue *real_value,
               json_array_add_element (array, str);
             }
 
-          retval = json_node_new (JSON_NODE_ARRAY);
-          json_node_take_array (retval, array);
+          retval = json_node_init_array (json_node_alloc (), array);
+          json_array_unref (array);
         }
       else if (json_boxed_can_serialize (G_VALUE_TYPE (real_value), &node_type))
         {
@@ -733,56 +759,19 @@ json_serialize_pspec (const GValue *real_value,
                    g_type_name (G_VALUE_TYPE (real_value)));
       break;
 
-    case G_TYPE_UINT:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_int (retval, g_value_get_uint (real_value));
-      break;
-
-    case G_TYPE_LONG:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_int (retval, g_value_get_long (real_value));
-      break;
-
-    case G_TYPE_ULONG:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_int (retval, g_value_get_long (real_value));
-      break;
-
-    case G_TYPE_CHAR:
-      retval = json_node_new (JSON_NODE_VALUE);
-#if GLIB_CHECK_VERSION (2, 31, 0)
-      json_node_set_int (retval, g_value_get_schar (real_value));
-#else
-      json_node_set_int (retval, g_value_get_char (real_value));
-#endif
-      break;
-
-    case G_TYPE_UCHAR:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_int (retval, g_value_get_uchar (real_value));
-      break;
-
-    case G_TYPE_ENUM:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_int (retval, g_value_get_enum (real_value));
-      break;
-
-    case G_TYPE_FLAGS:
-      retval = json_node_new (JSON_NODE_VALUE);
-      json_node_set_int (retval, g_value_get_flags (real_value));
-      break;
-
     case G_TYPE_OBJECT:
       {
         GObject *object = g_value_get_object (real_value);
 
+        retval = json_node_alloc ();
+
         if (object != NULL)
           {
-            retval = json_node_new (JSON_NODE_OBJECT);
+            json_node_init (retval, JSON_NODE_OBJECT);
             json_node_take_object (retval, json_gobject_dump (object));
           }
         else
-          retval = json_node_new (JSON_NODE_NULL);
+          json_node_init_null (retval);
       }
       break;
 
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
index 5053530..f9722bd 100644
--- a/json-glib/json-parser.c
+++ b/json-glib/json-parser.c
@@ -365,47 +365,45 @@ json_parse_value (JsonParser   *parser,
   switch (token)
     {
     case G_TOKEN_INT:
-      *node = json_node_new (JSON_NODE_VALUE);
       JSON_NOTE (PARSER, "abs(node): %" G_GINT64_FORMAT " (sign: %s)",
                  scanner->value.v_int64,
                  is_negative ? "negative" : "positive");
-      json_node_set_int (*node, is_negative ? scanner->value.v_int64 * -1
-                                            : scanner->value.v_int64);
+      *node = json_node_init_int (json_node_alloc (),
+                                  is_negative ? scanner->value.v_int64 * -1
+                                              : scanner->value.v_int64);
       break;
 
     case G_TOKEN_FLOAT:
-      *node = json_node_new (JSON_NODE_VALUE);
       JSON_NOTE (PARSER, "abs(node): %.6f (sign: %s)",
                  scanner->value.v_float,
                  is_negative ? "negative" : "positive");
-      json_node_set_double (*node, is_negative ? scanner->value.v_float * -1.0
-                                               : scanner->value.v_float);
+      *node = json_node_init_double (json_node_alloc (),
+                                     is_negative ? scanner->value.v_float * -1.0
+                                                 : scanner->value.v_float);
       break;
 
     case G_TOKEN_STRING:
-      *node = json_node_new (JSON_NODE_VALUE);
       JSON_NOTE (PARSER, "node: '%s'",
                  scanner->value.v_string);
-      json_node_set_string (*node, scanner->value.v_string);
+      *node = json_node_init_string (json_node_alloc (), scanner->value.v_string);
       break;
 
     case JSON_TOKEN_TRUE:
     case JSON_TOKEN_FALSE:
-      *node = json_node_new (JSON_NODE_VALUE);
       JSON_NOTE (PARSER, "node: '%s'",
                  JSON_TOKEN_TRUE ? "<true>" : "<false>");
-      json_node_set_boolean (*node, token == JSON_TOKEN_TRUE ? TRUE : FALSE);
+      *node = json_node_init_boolean (json_node_alloc (), token == JSON_TOKEN_TRUE ? TRUE : FALSE);
       break;
 
     case JSON_TOKEN_NULL:
-      *node = json_node_new (JSON_NODE_NULL);
       JSON_NOTE (PARSER, "node: <null>");
+      *node = json_node_init_null (json_node_alloc ());
       break;
 
     case G_TOKEN_IDENTIFIER:
-      *node = NULL;
       JSON_NOTE (PARSER, "node: identifier '%s'", scanner->value.v_identifier);
       priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
+      *node = NULL;
       return G_TOKEN_SYMBOL;
 
     default:
@@ -418,15 +416,22 @@ json_parse_value (JsonParser   *parser,
 
         cur_type = json_node_get_node_type (current_node);
         if (cur_type == JSON_NODE_ARRAY)
-          return G_TOKEN_RIGHT_BRACE;
+          {
+            priv->error_code = JSON_PARSER_ERROR_PARSE;
+            return G_TOKEN_RIGHT_BRACE;
+          }
         else if (cur_type == JSON_NODE_OBJECT)
-          return G_TOKEN_RIGHT_CURLY;
+          {
+            priv->error_code = JSON_PARSER_ERROR_PARSE;
+            return G_TOKEN_RIGHT_CURLY;
+          }
         else
           {
             priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
             return G_TOKEN_SYMBOL;
           }
       }
+      break;
     }
 
   return G_TOKEN_NONE;
@@ -444,7 +449,7 @@ json_parse_array (JsonParser   *parser,
   gint idx;
 
   old_current = priv->current_node;
-  priv->current_node = json_node_new (JSON_NODE_ARRAY);
+  priv->current_node = json_node_init_array (json_node_alloc (), NULL);
 
   array = json_array_new ();
 
@@ -550,7 +555,7 @@ json_parse_object (JsonParser   *parser,
   guint token;
 
   old_current = priv->current_node;
-  priv->current_node = json_node_new (JSON_NODE_OBJECT);
+  priv->current_node = json_node_init_object (json_node_alloc (), NULL);
 
   object = json_object_new ();
 



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