Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
simplepad opened this issue Jul 5, 2022 · 2 comments

Comments

@simplepad
Copy link

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
@hhj1988
Copy link

hhj1988 commented Jul 26, 2022

yum -y install gcc-c++

@hawicz
Copy link
Member

hawicz commented Jul 30, 2022

Sounds like a good idea. 👍

hawicz added a commit that referenced this issue Jul 30, 2022
…nd closing curly or square braces on same line for empty objects or arrays. Issue #778.
@hawicz hawicz closed this as completed Jul 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants