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

Conflict of interest between JSON_C_TO_STRING_SPACED and JSON_C_TO_STRING_PRETTY #429

Closed
lhunath opened this issue Jul 8, 2018 · 6 comments

Comments

@lhunath
Copy link

lhunath commented Jul 8, 2018

The goal of JSON_C_TO_STRING_SPACED is to include minimal spacing in the output.

The goal of JSON_C_TO_STRING_PRETTY is to indent the output.

JSON_C_TO_STRING_SPACED's minimal spacing includes spacing inside objects and spacing after colons.

JSON_C_TO_STRING_PRETTY's indenting includes spacing inside objects and newlines.

If you want output which has both minimal spacing and is indented (eg. indenting + space after colon), you need to enable both flags, but this currently causes bad indenting.

Therefore, I propose that if JSON_C_TO_STRING_PRETTY is enabled, JSON_C_TO_STRING_SPACED's object-spacing is ignored (because pretty already provides its own object spacing). Alternatively, JSON_C_TO_STRING_PRETTY could be changed to be a complete subset of JSON_C_TO_STRING_SPACED's minimal spacing, which means it would also have to add a space after the colon.

As implemented right now, there is a strange conflict where pretty is not spacing + indenting, and to get all spacing features, your indenting cannot be done right.

@lhunath
Copy link
Author

lhunath commented Jul 8, 2018

First proposal, "if JSON_C_TO_STRING_PRETTY is enabled, JSON_C_TO_STRING_SPACED's object-spacing is ignored (because pretty already provides its own object spacing)":

diff --git json_object.c json_object.c
index 8a86bc6..c5bca7c 100644
--- json_object.c
+++ json_object.c
@@ -395,7 +395,7 @@ static int json_object_object_to_json_string(struct json_object* jso,
                                printbuf_strappend(pb, "\n");
                }
                had_children = 1;
-               if (flags & JSON_C_TO_STRING_SPACED)
+               if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
                        printbuf_strappend(pb, " ");
                indent(pb, level+1, flags);
                printbuf_strappend(pb, "\"");
@@ -416,7 +416,7 @@ static int json_object_object_to_json_string(struct json_object* jso,
                        printbuf_strappend(pb, "\n");
                indent(pb,level,flags);
        }
-       if (flags & JSON_C_TO_STRING_SPACED)
+       if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
                return printbuf_strappend(pb, /*{*/ " }");
        else
                return printbuf_strappend(pb, /*{*/ "}");
@@ -1127,7 +1127,7 @@ static int json_object_array_to_json_string(struct json_object* jso,
                                printbuf_strappend(pb, "\n");
                }
                had_children = 1;
-               if (flags & JSON_C_TO_STRING_SPACED)
+               if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
                        printbuf_strappend(pb, " ");
                indent(pb, level + 1, flags);
                val = json_object_array_get_idx(jso, ii);
@@ -1144,7 +1144,7 @@ static int json_object_array_to_json_string(struct json_object* jso,
                indent(pb,level,flags);
        }

-       if (flags & JSON_C_TO_STRING_SPACED)
+       if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
                return printbuf_strappend(pb, " ]");
        return printbuf_strappend(pb, "]");
 }

@hawicz
Copy link
Member

hawicz commented Jul 10, 2018

A simpler change would be to just clear the JSON_C_TO_STRING_PRETTY bit at the start of the function.

@lhunath
Copy link
Author

lhunath commented Jul 10, 2018

Not entirely sure I understand what you're suggesting. JSON_C_TO_STRING_PRETTY and JSON_C_TO_STRING_SPACED definitely have a distinct effect and likely shouldn't just be ignored.

@hawicz
Copy link
Member

hawicz commented Jul 11, 2018

er.. yeah, never mind my previous comment.

@dota17
Copy link
Member

dota17 commented Jan 13, 2020

Nice issue and it could be closed @hawicz

@hawicz
Copy link
Member

hawicz commented Jul 4, 2020

Long since fixed, closing.

@hawicz hawicz closed this as completed Jul 4, 2020
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