Skip to content

Do not insert newlines when converting empty arrays to json string and JSON_C_TO_STRING_PRETTY is used #778

@simplepad

Description

@simplepad

Describe the bug
When json-c converts a json array to a json string and the flag JSON_C_TO_STRING_PRETTY is used, it inserts a newline even if the array is empty.

json-c/json_object.c

Lines 1377 to 1378 in d28ac67

if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");

Here in the if condition it should check if the array is not empty, otherwise it looks like this:

{
  "mystring": "hello",
  "my_empty_array": [
  ]
}

I think it would be better if it looked like this:

{
  "mystring": "hello",
  "my_empty_array": []
}

Pretty insignificant, I know 😄

Steps To Reproduce

struct json_object * object = json_object_new_object();
json_object_object_add(object, "my_empty_array", json_object_new_array());
puts(json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));

Version and Platform

  • json-c version: json-c-0.16
  • OS: Arch Linux
  • Custom cmake/build flags, if any: none

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions