[json-glib] generator: Escape Object key names correctly



commit 8f4816a2e05d0d3672d7723df2feaf466632c498
Author: Stef Walter <stefw redhat com>
Date:   Tue May 20 10:43:29 2014 +0200

    generator: Escape Object key names correctly
    
    Also fix empty keys, which are now encoded properly.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=727593

 json-glib/json-generator.c |   19 +++++++++++--------
 1 files changed, 11 insertions(+), 8 deletions(-)
---
diff --git a/json-glib/json-generator.c b/json-glib/json-generator.c
index 9868973..ff8af96 100644
--- a/json-glib/json-generator.c
+++ b/json-glib/json-generator.c
@@ -286,7 +286,7 @@ dump_value (JsonGenerator *generator,
         g_string_append_c (buffer, priv->indent_char);
     }
 
-  if (name && name[0] != '\0')
+  if (name)
     {
       if (pretty)
         g_string_append_printf (buffer, "\"%s\" : ", name);
@@ -365,7 +365,7 @@ dump_array (JsonGenerator *generator,
         g_string_append_c (buffer, priv->indent_char);
     }
 
-  if (name && name[0] != '\0')
+  if (name)
     {
       if (pretty)
         g_string_append_printf (buffer, "\"%s\" : ", name);
@@ -458,7 +458,7 @@ dump_object (JsonGenerator *generator,
         g_string_append_c (buffer, priv->indent_char);
     }
 
-  if (name && name[0] != '\0')
+  if (name)
     {
       if (pretty)
         g_string_append_printf (buffer, "\"%s\" : ", name);
@@ -476,6 +476,7 @@ dump_object (JsonGenerator *generator,
   for (l = members; l != NULL; l = l->next)
     {
       const gchar *member_name = l->data;
+      gchar *escaped_name = json_strescape (member_name);
       JsonNode *cur = json_object_get_member (object, member_name);
       guint sub_level = level + 1;
       guint j;
@@ -488,29 +489,29 @@ dump_object (JsonGenerator *generator,
             {
               for (j = 0; j < (sub_level * indent); j++)
                 g_string_append_c (buffer, priv->indent_char);
-              g_string_append_printf (buffer, "\"%s\" : null", member_name);
+              g_string_append_printf (buffer, "\"%s\" : null", escaped_name);
             }
           else
             {
-              g_string_append_printf (buffer, "\"%s\":null", member_name);
+              g_string_append_printf (buffer, "\"%s\":null", escaped_name);
             }
           break;
 
         case JSON_NODE_VALUE:
-          value = dump_value (generator, sub_level, member_name, cur, NULL);
+          value = dump_value (generator, sub_level, escaped_name, cur, NULL);
           g_string_append (buffer, value);
           g_free (value);
           break;
 
         case JSON_NODE_ARRAY:
-          value = dump_array (generator, sub_level, member_name,
+          value = dump_array (generator, sub_level, escaped_name,
                               json_node_get_array (cur), NULL);
           g_string_append (buffer, value);
           g_free (value);
           break;
 
         case JSON_NODE_OBJECT:
-          value = dump_object (generator, sub_level, member_name,
+          value = dump_object (generator, sub_level, escaped_name,
                                json_node_get_object (cur), NULL);
           g_string_append (buffer, value);
           g_free (value);
@@ -522,6 +523,8 @@ dump_object (JsonGenerator *generator,
 
       if (pretty)
         g_string_append_c (buffer, '\n');
+
+      g_free (escaped_name);
     }
 
   g_list_free (members);


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