[json-glib] generator: Removes blank spaces from generated JSON when not pretty-print



commit af70f34b3fc46a4c464418ebd6a16d9a1f18af8a
Author: Eduardo Lima Mitev <elima igalia com>
Date:   Thu Jan 27 16:58:48 2011 +0100

    generator: Removes blank spaces from generated JSON when not pretty-print
    
    https://bugzilla.gnome.org/show_bug.cgi?id=640729

 json-glib/json-generator.c       |   35 +++++++++++------
 json-glib/tests/builder-test.c   |    6 +-
 json-glib/tests/generator-test.c |   34 ++++++++--------
 json-glib/tests/gvariant-test.c  |   78 +++++++++++++++++++-------------------
 4 files changed, 82 insertions(+), 71 deletions(-)
---
diff --git a/json-glib/json-generator.c b/json-glib/json-generator.c
index c3bdc9a..6a36bcf 100644
--- a/json-glib/json-generator.c
+++ b/json-glib/json-generator.c
@@ -293,7 +293,12 @@ dump_value (JsonGenerator *generator,
     }
 
   if (name && name[0] != '\0')
-    g_string_append_printf (buffer, "\"%s\" : ", name);
+    {
+      if (pretty)
+        g_string_append_printf (buffer, "\"%s\" : ", name);
+      else
+        g_string_append_printf (buffer, "\"%s\":", name);
+    }
 
   json_node_get_value (node, &value);
 
@@ -364,14 +369,17 @@ dump_array (JsonGenerator *generator,
     }
 
   if (name && name[0] != '\0')
-    g_string_append_printf (buffer, "\"%s\" : ", name);
+    {
+      if (pretty)
+        g_string_append_printf (buffer, "\"%s\" : ", name);
+      else
+        g_string_append_printf (buffer, "\"%s\":", name);
+    }
 
   g_string_append_c (buffer, '[');
 
   if (pretty)
     g_string_append_c (buffer, '\n');
-  else
-    g_string_append_c (buffer, ' ');
 
   for (i = 0; i < array_len; i++)
     {
@@ -415,8 +423,6 @@ dump_array (JsonGenerator *generator,
 
       if (pretty)
         g_string_append_c (buffer, '\n');
-      else
-        g_string_append_c (buffer, ' ');
     }
 
   if (pretty)
@@ -456,14 +462,17 @@ dump_object (JsonGenerator *generator,
     }
 
   if (name && name[0] != '\0')
-    g_string_append_printf (buffer, "\"%s\" : ", name);
+    {
+      if (pretty)
+        g_string_append_printf (buffer, "\"%s\" : ", name);
+      else
+        g_string_append_printf (buffer, "\"%s\":", name);
+    }
 
   g_string_append_c (buffer, '{');
 
   if (pretty)
     g_string_append_c (buffer, '\n');
-  else
-    g_string_append_c (buffer, ' ');
 
   members = json_object_get_members (object);
 
@@ -482,8 +491,12 @@ 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);
+            }
+          else
+            {
+              g_string_append_printf (buffer, "\"%s\":null", member_name);
             }
-          g_string_append_printf (buffer, "\"%s\" : null", member_name);
           break;
 
         case JSON_NODE_VALUE:
@@ -512,8 +525,6 @@ dump_object (JsonGenerator *generator,
 
       if (pretty)
         g_string_append_c (buffer, '\n');
-      else
-        g_string_append_c (buffer, ' ');
     }
 
   g_list_free (members);
diff --git a/json-glib/tests/builder-test.c b/json-glib/tests/builder-test.c
index 6812434..63382a7 100644
--- a/json-glib/tests/builder-test.c
+++ b/json-glib/tests/builder-test.c
@@ -6,10 +6,10 @@
 
 #include <json-glib/json-glib.h>
 
-static const gchar *complex_object = "{ \"depth1\" : [ 1, { \"depth2\" : [ 3, [ null ], \"after array\" ], \"value2\" : true } ], \"object1\" : { } }\") == 0)";
+static const gchar *complex_object = "{\"depth1\":[1,{\"depth2\":[3,[null],\"after array\"],\"value2\":true}],\"object1\":{}}\") == 0)";
 
-static const gchar *reset_object = "{ \"test\" : \"reset\" }";
-static const gchar *reset_array = "[ \"reset\" ]";
+static const gchar *reset_object = "{\"test\":\"reset\"}";
+static const gchar *reset_array = "[\"reset\"]";
 
 static void
 test_builder_complex (void)
diff --git a/json-glib/tests/generator-test.c b/json-glib/tests/generator-test.c
index 4de218d..5cc8b92 100644
--- a/json-glib/tests/generator-test.c
+++ b/json-glib/tests/generator-test.c
@@ -12,27 +12,27 @@
 
 #include <locale.h>
 
-static const gchar *empty_array  = "[ ]";
-static const gchar *empty_object = "{ }";
+static const gchar *empty_array  = "[]";
+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_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,\"String\":\"foo\"}";
 /* taken from the RFC 4627, Examples section */
 static const gchar *nested_object =
-"{ "
-  "\"Image\" : { "
-    "\"Width\" : 800, "
-    "\"Height\" : 600, "
-    "\"Title\" : \"View from 15th Floor\", "
-    "\"Thumbnail\" : { "
-      "\"Url\" : \"http://www.example.com/image/481989943\";, "
-      "\"Height\" : 125, "
-      "\"Width\" : \"100\" "
-    "}, "
-    "\"IDs\" : [ 116, 943, 234, 38793 ] "
-  "} "
+"{"
+  "\"Image\":{"
+    "\"Width\":800,"
+    "\"Height\":600,"
+    "\"Title\":\"View from 15th Floor\","
+    "\"Thumbnail\":{"
+      "\"Url\":\"http://www.example.com/image/481989943\",";
+      "\"Height\":125,"
+      "\"Width\":\"100\""
+    "},"
+    "\"IDs\":[116,943,234,38793]"
+  "}"
 "}";
 
 static const struct {
diff --git a/json-glib/tests/gvariant-test.c b/json-glib/tests/gvariant-test.c
index e3678cc..0dbccdf 100644
--- a/json-glib/tests/gvariant-test.c
+++ b/json-glib/tests/gvariant-test.c
@@ -15,134 +15,134 @@ typedef struct
 const TestCase test_cases[] =
   {
     /* boolean */
-    { "/boolean", "(b)", "(true,)", "[ true ]" },
+    { "/boolean", "(b)", "(true,)", "[true]" },
 
     /* byte */
-    { "/byte", "(y)", "(byte 0xff,)", "[ 255 ]" },
+    { "/byte", "(y)", "(byte 0xff,)", "[255]" },
 
     /* int16 */
-    { "/int16", "(n)", "(int16 -12345,)", "[ -12345 ]" },
+    { "/int16", "(n)", "(int16 -12345,)", "[-12345]" },
 
     /* uint16 */
-    { "/uint16", "(q)", "(uint16 40001,)", "[ 40001 ]" },
+    { "/uint16", "(q)", "(uint16 40001,)", "[40001]" },
 
     /* int32 */
-    { "/int32", "(i)", "(-7654321,)", "[ -7654321 ]" },
+    { "/int32", "(i)", "(-7654321,)", "[-7654321]" },
 
     /* uint32 */
-    { "/uint32", "(u)", "(uint32 12345678,)", "[ 12345678 ]" },
+    { "/uint32", "(u)", "(uint32 12345678,)", "[12345678]" },
 
     /* int64 */
-    { "/int64", "(x)", "(int64 -666999666999,)", "[ -666999666999 ]" },
+    { "/int64", "(x)", "(int64 -666999666999,)", "[-666999666999]" },
 
     /* uint64 */
-    { "/uint64", "(t)", "(uint64 1999999999999999,)", "[ 1999999999999999 ]" },
+    { "/uint64", "(t)", "(uint64 1999999999999999,)", "[1999999999999999]" },
 
     /* handle */
-    { "/handle", "(h)", "(handle 1,)", "[ 1 ]" },
+    { "/handle", "(h)", "(handle 1,)", "[1]" },
 
     /* double */
-    { "/double", "(d)", "(1.23,)", "[ 1.23 ]" },
+    { "/double", "(d)", "(1.23,)", "[1.23]" },
 
     /* string */
-    { "/string", "(s)", "('hello world!',)", "[ \"hello world!\" ]" },
+    { "/string", "(s)", "('hello world!',)", "[\"hello world!\"]" },
 
     /* object-path */
-    { "/object-path", "(o)", "(objectpath '/org/gtk/json_glib',)", "[ \"/org/gtk/json_glib\" ]" },
+    { "/object-path", "(o)", "(objectpath '/org/gtk/json_glib',)", "[\"/org/gtk/json_glib\"]" },
 
     /* signature */
-    { "/signature", "(g)", "(signature '(asna{sv}i)',)", "[ \"(asna{sv}i)\" ]" },
+    { "/signature", "(g)", "(signature '(asna{sv}i)',)", "[\"(asna{sv}i)\"]" },
 
     /* maybe - null string */
-    { "/maybe/simple/null", "(ms)", "(@ms nothing,)", "[ null ]" },
+    { "/maybe/simple/null", "(ms)", "(@ms nothing,)", "[null]" },
 
     /* maybe - simple string */
-    { "/maybe/simple/string", "(ms)", "(@ms 'maybe string',)", "[ \"maybe string\" ]" },
+    { "/maybe/simple/string", "(ms)", "(@ms 'maybe string',)", "[\"maybe string\"]" },
 
     /* maybe - null container */
-    { "/maybe/container/null", "(m(sn))", "(@m(sn) nothing,)", "[ null ]" },
+    { "/maybe/container/null", "(m(sn))", "(@m(sn) nothing,)", "[null]" },
 
     /* maybe - tuple container */
-    { "/maybe/container/tuple", "(m(sn))", "(@m(sn) ('foo', 0),)", "[ [ \"foo\", 0 ] ]" },
+    { "/maybe/container/tuple", "(m(sn))", "(@m(sn) ('foo', 0),)", "[[\"foo\",0]]" },
 
     /* maybe - variant boolean */
-    { "/maybe/variant/boolean", "(mv)", "(@mv <true>,)", "[ true ]" },
+    { "/maybe/variant/boolean", "(mv)", "(@mv <true>,)", "[true]" },
 
     /* empty array */
-    { "/array/empty", "as", "@as []", "[ ]" },
+    { "/array/empty", "as", "@as []", "[]" },
 
     /* array of bytes */
-    { "/array/byte", "ay", "[byte 0x01, 0x0a, 0x03, 0xff]", "[ 1, 10, 3, 255 ]" },
+    { "/array/byte", "ay", "[byte 0x01, 0x0a, 0x03, 0xff]", "[1,10,3,255]" },
 
     /* array of strings */
-    { "/array/string", "as", "['a', 'b', 'ab', 'ba']", "[ \"a\", \"b\", \"ab\", \"ba\" ]" },
+    { "/array/string", "as", "['a', 'b', 'ab', 'ba']", "[\"a\",\"b\",\"ab\",\"ba\"]" },
 
     /* array of array of int32 */
-    { "/array/array/int32", "aai", "[[1, 2], [3, 4], [5, 6]]", "[ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]" },
+    { "/array/array/int32", "aai", "[[1, 2], [3, 4], [5, 6]]", "[[1,2],[3,4],[5,6]]" },
 
     /* array of variants */
-    { "/array/variant", "av", "[<true>, <int64 1>, <'oops'>, <int64 -2>, <0.5>]", "[ true, 1, \"oops\", -2, 0.5 ]" },
+    { "/array/variant", "av", "[<true>, <int64 1>, <'oops'>, <int64 -2>, <0.5>]", "[true,1,\"oops\",-2,0.5]" },
 
     /* tuple */
     { "/tuple", "(bynqiuxthds)",
       "(false, byte 0x00, int16 -1, uint16 1, -2, uint32 2, int64 429496729, uint64 3, handle 16, 2.48, 'end')",
-      "[ false, 0, -1, 1, -2, 2, 429496729, 3, 16, 2.48, \"end\" ]" },
+      "[false,0,-1,1,-2,2,429496729,3,16,2.48,\"end\"]" },
 
     /* empty dictionary */
-    { "/dictionary/empty", "a{sv}", "@a{sv} {}", "{ }" },
+    { "/dictionary/empty", "a{sv}", "@a{sv} {}", "{}" },
 
     /* single dictionary entry */
-    { "/dictionary/single-entry", "{ss}", "{'hello', 'world'}", "{ \"hello\" : \"world\" }" },
+    { "/dictionary/single-entry", "{ss}", "{'hello', 'world'}", "{\"hello\":\"world\"}" },
 
     /* dictionary - string : int32 */
-    { "/dictionary/string-int32", "a{si}", "{'foo': 1, 'bar': 2}", "{ \"foo\" : 1, \"bar\" : 2 }" },
+    { "/dictionary/string-int32", "a{si}", "{'foo': 1, 'bar': 2}", "{\"foo\":1,\"bar\":2}" },
 
     /* dictionary - string : variant */
-    { "/dictionary/string-variant", "a{sv}", "{'str': <'hi!'>, 'bool': <true>}", "{ \"str\" : \"hi!\", \"bool\" : true }" },
+    { "/dictionary/string-variant", "a{sv}", "{'str': <'hi!'>, 'bool': <true>}", "{\"str\":\"hi!\",\"bool\":true}" },
 
     /* dictionary - int64 : variant */
     { "/dictionary/int64-variant", "a{xv}",
-      "{int64 -5: <'minus five'>, 10: <'ten'>}", "{ \"-5\" : \"minus five\", \"10\" : \"ten\" }" },
+      "{int64 -5: <'minus five'>, 10: <'ten'>}", "{\"-5\":\"minus five\",\"10\":\"ten\"}" },
 
     /* dictionary - uint64 : variant */
     { "/dictionary/uint64-boolean", "a{tb}",
-      "{uint64 999888777666: true, 0: false}", "{ \"999888777666\" : true, \"0\" : false }" },
+      "{uint64 999888777666: true, 0: false}", "{\"999888777666\":true,\"0\":false}" },
 
     /* dictionary - boolean : variant */
-    { "/dictionary/boolean-variant", "a{by}", "{true: byte 0x01, false: 0x00}", "{ \"true\" : 1, \"false\" : 0 }" },
+    { "/dictionary/boolean-variant", "a{by}", "{true: byte 0x01, false: 0x00}", "{\"true\":1,\"false\":0}" },
 
     /* dictionary - double : string */
-    { "/dictionary/double-string", "a{ds}", "{1.0: 'one point zero'}", "{ \"1.000000\" : \"one point zero\" }" },
+    { "/dictionary/double-string", "a{ds}", "{1.0: 'one point zero'}", "{\"1.000000\":\"one point zero\"}" },
 
     /* variant - string */
-    { "/variant/string", "(v)", "(<'string within variant'>,)", "[ \"string within variant\" ]" },
+    { "/variant/string", "(v)", "(<'string within variant'>,)", "[\"string within variant\"]" },
 
     /* variant - maybe null  */
-    { "/variant/maybe/null", "(v)", "(<@mv nothing>,)", "[ null ]" },
+    { "/variant/maybe/null", "(v)", "(<@mv nothing>,)", "[null]" },
 
     /* variant - dictionary */
-    { "/variant/dictionary", "v", "<{'foo': <'bar'>, 'hi': <int64 1024>}>", "{ \"foo\" : \"bar\", \"hi\" : 1024 }" },
+    { "/variant/dictionary", "v", "<{'foo': <'bar'>, 'hi': <int64 1024>}>", "{\"foo\":\"bar\",\"hi\":1024}" },
 
     /* variant - variant - array */
-    { "/variant/variant/array", "v", "<[<'any'>, <'thing'>, <int64 0>, <int64 -1>]>", "[ \"any\", \"thing\", 0, -1 ]" },
+    { "/variant/variant/array", "v", "<[<'any'>, <'thing'>, <int64 0>, <int64 -1>]>", "[\"any\",\"thing\",0,-1]" },
 
     /* deep-nesting */
     { "/deep-nesting",
       "a(a(a(a(a(a(a(a(a(a(s))))))))))",
       "[([([([([([([([([([('sorprise',)],)],)],)],)],)],)],)],)],)]",
-      "[ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ \"sorprise\" ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ]" },
+      "[[[[[[[[[[[[[[[[[[[[\"sorprise\"]]]]]]]]]]]]]]]]]]]]" },
 
     /* mixed1 */
     { "/mixed1",
       "a{s(syba(od))}",
       "{'foo': ('bar', byte 0x64, true, [(objectpath '/baz', 1.3), ('/cat', -2.5)])}",
-      "{ \"foo\" : [ \"bar\", 100, true, [ [ \"/baz\", 1.3 ], [ \"/cat\", -2.5 ] ] ] }" },
+      "{\"foo\":[\"bar\",100,true,[[\"/baz\",1.3],[\"/cat\",-2.5]]]}" },
 
     /* mixed2 */
     { "/mixed2",
       "(a{by}amsvmaba{qm(sg)})",
       "({true: byte 0x01, false: 0x00}, [ ms 'do', nothing, 'did'], <@av []>, @mab nothing, {uint16 10000: @m(sg) ('yes', 'august'), 0: nothing})",
-      "[ { \"true\" : 1, \"false\" : 0 }, [ \"do\", null, \"did\" ], [ ], null, { \"10000\" : [ \"yes\", \"august\" ], \"0\" : null } ]" },
+      "[{\"true\":1,\"false\":0},[\"do\",null,\"did\"],[],null,{\"10000\":[\"yes\",\"august\"],\"0\":null}]" },
   };
 
 static void



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