[json-glib/wip/ordered-iter: 15/15] Gracefully handle empty arrays and objects
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib/wip/ordered-iter: 15/15] Gracefully handle empty arrays and objects
- Date: Mon, 24 Aug 2020 16:44:20 +0000 (UTC)
commit 3350e7808aada83149ddb36e9c85d22733f28af3
Author: Emmanuele Bassi <ebassi gnome org>
Date: Mon Aug 24 16:19:59 2020 +0100
Gracefully handle empty arrays and objects
Don't pretty print newlines. This requires a change in the generator
test, but the pretty printing format is not stable anyway, so we can get
away with it.
json-glib/json-generator.c | 14 +++++++++-----
json-glib/tests/generator.c | 11 +++++------
2 files changed, 14 insertions(+), 11 deletions(-)
---
diff --git a/json-glib/json-generator.c b/json-glib/json-generator.c
index 346be65..4ccf320 100644
--- a/json-glib/json-generator.c
+++ b/json-glib/json-generator.c
@@ -394,13 +394,16 @@ dump_array (JsonGenerator *generator,
g_string_append_c (buffer, '[');
- if (pretty)
- g_string_append_c (buffer, '\n');
+ if (array_len == 0)
+ goto out;
for (i = 0; i < array_len; i++)
{
JsonNode *cur = json_array_get_element (array, i);
+ if (i == 0 && pretty)
+ g_string_append_c (buffer, '\n');
+
dump_node (generator, buffer, level + 1, NULL, cur);
if ((i + 1) != array_len)
@@ -416,6 +419,7 @@ dump_array (JsonGenerator *generator,
g_string_append_c (buffer, priv->indent_char);
}
+out:
g_string_append_c (buffer, ']');
}
@@ -434,9 +438,6 @@ dump_object (JsonGenerator *generator,
g_string_append_c (buffer, '{');
- if (pretty)
- g_string_append_c (buffer, '\n');
-
members = json_object_get_members_internal (object);
for (l = members->head; l != NULL; l = l->next)
@@ -444,6 +445,9 @@ dump_object (JsonGenerator *generator,
const gchar *member_name = l->data;
JsonNode *cur = json_object_get_member (object, member_name);
+ if (l->prev == NULL && pretty)
+ g_string_append_c (buffer, '\n');
+
dump_node (generator, buffer, level + 1, member_name, cur);
if (l->next != NULL)
diff --git a/json-glib/tests/generator.c b/json-glib/tests/generator.c
index e7dabff..84995f8 100644
--- a/json-glib/tests/generator.c
+++ b/json-glib/tests/generator.c
@@ -34,9 +34,9 @@ static const gchar *nested_object =
"}";
static const char *pretty_examples[] = {
- "[\n]",
+ "[]",
- "{\n}",
+ "{}",
"[\n"
"\ttrue,\n"
@@ -382,10 +382,9 @@ test_pretty (void)
data = json_generator_to_data (generator, &len);
- if (g_test_verbose ())
- g_print ("** checking pretty printing:\n%s\n** expected:\n%s\n",
- data,
- pretty_examples[i]);
+ g_test_message ("checking pretty printing: %s\texpected: %s",
+ data,
+ pretty_examples[i]);
g_assert_cmpint (len, ==, strlen (pretty_examples[i]));
g_assert_cmpstr (data, ==, pretty_examples[i]);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]