[json-glib] Allow empty string as object member name



commit 799e1651b463bf127e6a99573e20331f6d7399b3
Author: Dr. David Alan Gilbert <dave treblig org>
Date:   Sun Mar 5 01:25:18 2017 +0000

    Allow empty string as object member name
    
    Commit 028e540 disallowed empty member names in objects, however
    they are unfortunately valid JSON.  This patch reenables an empty
    string as a member name.
    
    Tests are updated to allow the empty string case, and to test
    the use of an empty string in generation, iteration etc.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=747279

 json-glib/json-parser.c     |    2 +-
 json-glib/tests/builder.c   |    5 ++++-
 json-glib/tests/generator.c |    3 ++-
 json-glib/tests/invalid.c   |    2 +-
 json-glib/tests/object.c    |    8 +++++++-
 json-glib/tests/parser.c    |    3 ++-
 6 files changed, 17 insertions(+), 6 deletions(-)
---
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
index efd7e14..655f2e2 100644
--- a/json-glib/json-parser.c
+++ b/json-glib/json-parser.c
@@ -676,7 +676,7 @@ json_parse_object (JsonParser   *parser,
       /* member name */
       token = json_scanner_get_next_token (scanner);
       name = g_strdup (scanner->value.v_string);
-      if (name == NULL || *name == '\0')
+      if (name == NULL)
         {
           JSON_NOTE (PARSER, "Empty object member name");
 
diff --git a/json-glib/tests/builder.c b/json-glib/tests/builder.c
index eaabb0d..b3e07d6 100644
--- a/json-glib/tests/builder.c
+++ b/json-glib/tests/builder.c
@@ -6,7 +6,7 @@
 
 #include <json-glib/json-glib.h>
 
-static const gchar *complex_object = "{\"depth1\":[1,{\"depth2\":[3,[null,false],\"after 
array\"],\"value2\":true}],\"object1\":{},\"value3\":null,\"value4\":42}";
+static const gchar *complex_object = "{\"depth1\":[1,{\"depth2\":[3,[null,false],\"after 
array\"],\"value2\":true}],\"object1\":{},\"value3\":null,\"value4\":42,\"\":54}";
 
 static const gchar *empty_object = "{\"a\":{}}";
 
@@ -58,6 +58,9 @@ test_builder_complex (void)
   json_builder_set_member_name (builder, "value4");
   json_builder_add_int_value (builder, 42);
 
+  json_builder_set_member_name (builder, "");
+  json_builder_add_int_value (builder, 54);
+
   json_builder_end_object (builder);
 
   node = json_builder_get_root (builder);
diff --git a/json-glib/tests/generator.c b/json-glib/tests/generator.c
index f156fde..79b6887 100644
--- a/json-glib/tests/generator.c
+++ b/json-glib/tests/generator.c
@@ -16,7 +16,7 @@ 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 gchar *simple_object = 
"{\"Bool1\":true,\"Bool2\":false,\"Null\":null,\"Int\":42,\"String\":\"foo\"}";
+static const gchar *simple_object = 
"{\"Bool1\":true,\"Bool2\":false,\"Null\":null,\"Int\":42,\"\":54,\"String\":\"foo\"}";
 /* taken from the RFC 4627, Examples section */
 static const gchar *nested_object =
 "{"
@@ -209,6 +209,7 @@ test_simple_object (void)
   json_object_set_boolean_member (object, "Bool2", FALSE);
   json_object_set_null_member (object, "Null");
   json_object_set_int_member (object, "Int", 42);
+  json_object_set_int_member (object, "", 54);
   json_object_set_string_member (object, "String", "foo");
 
   json_node_take_object (root, object);
diff --git a/json-glib/tests/invalid.c b/json-glib/tests/invalid.c
index c5fad16..b782566 100644
--- a/json-glib/tests/invalid.c
+++ b/json-glib/tests/invalid.c
@@ -231,7 +231,7 @@ static const struct
   { "object-4", "{ \"foo\" : 42 ]", test_invalid_object },
   { "object-5", "{ \"blah\" }", test_invalid_object },
   { "object-6", "{ \"a\" : 0 \"b\" : 1 }", test_invalid_object },
-  { "object-7", "{ \"\" : false }", test_invalid_object },
+  { "object-7", "{ null: false }", test_invalid_object },
 
   /* missing commas */
   { "missing-comma-1", "[ true false ]", test_missing_comma },
diff --git a/json-glib/tests/object.c b/json-glib/tests/object.c
index 54b5934..03a478c 100644
--- a/json-glib/tests/object.c
+++ b/json-glib/tests/object.c
@@ -64,6 +64,9 @@ test_set_member (void)
   json_object_set_object_member (object, "Object", NULL);
   g_assert (json_object_get_null_member (object, "Object") == TRUE);
 
+  json_object_set_object_member (object, "", NULL);
+  g_assert (json_object_get_null_member (object, "") == TRUE);
+
   json_object_unref (object);
 }
 
@@ -95,7 +98,8 @@ static const struct {
   { "boolean", JSON_NODE_VALUE, G_TYPE_BOOLEAN },
   { "string", JSON_NODE_VALUE, G_TYPE_STRING },
   { "double", JSON_NODE_VALUE, G_TYPE_DOUBLE },
-  { "null", JSON_NODE_NULL, G_TYPE_INVALID }
+  { "null", JSON_NODE_NULL, G_TYPE_INVALID },
+  { "", JSON_NODE_VALUE, G_TYPE_INT64 }
 };
 
 static void
@@ -131,6 +135,7 @@ test_foreach_member (void)
   json_object_set_string_member (object, "string", "hello");
   json_object_set_double_member (object, "double", 3.14159);
   json_object_set_null_member (object, "null");
+  json_object_set_int_member (object, "", 0);
 
   json_object_foreach_member (object, verify_foreach, &fixture);
 
@@ -155,6 +160,7 @@ test_iter (void)
   json_object_set_string_member (object, "string", "hello");
   json_object_set_double_member (object, "double", 3.14159);
   json_object_set_null_member (object, "null");
+  json_object_set_int_member (object, "", 0);
 
   json_object_iter_init (&iter, object);
 
diff --git a/json-glib/tests/parser.c b/json-glib/tests/parser.c
index bdfbf13..f71584a 100644
--- a/json-glib/tests/parser.c
+++ b/json-glib/tests/parser.c
@@ -96,7 +96,8 @@ static const struct {
   { "{ \"name\" : \"\", \"state\" : 1 }", 2, "name", JSON_NODE_VALUE, G_TYPE_STRING },
   { "{ \"foo\" : \"bar\", \"baz\" : null }", 2, "baz", JSON_NODE_NULL, G_TYPE_INVALID },
   { "{ \"channel\" : \"/meta/connect\" }", 1, "channel", JSON_NODE_VALUE, G_TYPE_STRING },
-  { "{ \"halign\":0.5, \"valign\":0.5 }", 2, "valign", JSON_NODE_VALUE, G_TYPE_DOUBLE }
+  { "{ \"halign\":0.5, \"valign\":0.5 }", 2, "valign", JSON_NODE_VALUE, G_TYPE_DOUBLE },
+  { "{ \"\" : \"emptiness\" }", 1, "", JSON_NODE_VALUE, G_TYPE_STRING }
 };
 
 static const gchar *test_nested_objects[] = {


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