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

The spaces_around_comma options doesn't work #490

Closed
YintongMa opened this issue Feb 16, 2024 · 5 comments · Fixed by microsoft/vcpkg#38532
Closed

The spaces_around_comma options doesn't work #490

YintongMa opened this issue Feb 16, 2024 · 5 comments · Fixed by microsoft/vcpkg#38532

Comments

@YintongMa
Copy link

YintongMa commented Feb 16, 2024

I want print out the json array by adding a space after comma. But the spaces_around_comma option doesn't work in the following simple case.
`
Code:

jsoncons::json j_arr(jsoncons::json_array_arg, {"1", "2", 3, 4});
jsoncons::json_options options;
options.spaces_around_comma(jsoncons::spaces_option::space_after);
std::string buffer;
jsoncons::encode_json(j_arr, buffer, options);
std::cout << buffer << std::endl;

`

The output is:
["1","2",3,4]

What I want is:
["1", "2", 3, 4]

@YintongMa YintongMa added the Bug label Feb 16, 2024
@danielaparker
Copy link
Owner

Thanks for reporting this, I'll have a look.

@danielaparker
Copy link
Owner

danielaparker commented Feb 16, 2024

It's been a while since I've looked at the json options! I had to re-familiarize myself with them.

The function encode_json with indenting::no_indent as the (default) fourth argument doesn't perform any prettify operations on the output json, it produces compact single-line output only, and ignores prettify options (by design.) To prettify the output, with spaces and so on, you need to call it with indenting::indent.

If your input JSON looked like

[["1", "2"], [3, 4]]

you could use

   jsoncons::json_options options;
   options.spaces_around_comma(jsoncons::spaces_option::space_after);
   options.array_array_line_splits(jsoncons::line_split_kind::same_line);
   std::string buffer;
   jsoncons::encode_json(j_arr, buffer, options, jsoncons::indenting::indent);
   std::cout << buffer << std::endl;

and your output would be

[
    ["1", "2"],
    [3, 4]
]

It seems we don't currently have an option for prettifying the output but leaving it on a single line.

The name of the fourth parameter type indenting is a little misleading, it really means "prettify yes no", that usually does include line indenting, but not in your case.

@YintongMa
Copy link
Author

Thank you so much for the help! I think I'll iterate the values and format the output by myself then

@danielaparker
Copy link
Owner

danielaparker commented Feb 28, 2024

This is now supported on the branch master, with the new option line_splits,

    jsoncons::json j_arr = jsoncons::json::parse(R"(["1", "2", 3, 4])");
    jsoncons::json_options options;
    options.spaces_around_comma(jsoncons::spaces_option::space_after) // same as default when using pretty printing 
           .line_splits(jsoncons::line_split_kind::same_line);        // default is multi_line 
    std::string buffer;
    jsoncons::encode_json(j_arr, buffer, options, jsoncons::indenting::indent);
    std::cout << buffer << std::endl;

Output:

["1", "2", 3, 4]

The line_splits value now becomes the default value for object_object_line_splits, object_array_line_splits, array_object_line_splits, and array_array_line_splits, so, for example,

    jsoncons::json j_arr = jsoncons::json::parse(R"(["1", "2", [3, 4]])");
    jsoncons::json_options options;
    options.line_splits(jsoncons::line_split_kind::same_line);
    std::string buffer;
    jsoncons::encode_json(j_arr, buffer, options, jsoncons::indenting::indent);
    std::cout << buffer << std::endl;

produces

["1", "2", [3, 4]]

@YintongMa
Copy link
Author

That's awsome! Appreciated it!

data-queue pushed a commit to microsoft/vcpkg that referenced this issue May 3, 2024
<!-- If your PR fixes issues, please note that here by adding "Fixes
#NNNNNN." for each fixed issue on separate lines. -->

Fixes danielaparker/jsoncons#502
Fixes danielaparker/jsoncons#501
Fixes danielaparker/jsoncons#499
Fixes danielaparker/jsoncons#496
Fixes danielaparker/jsoncons#493
Fixes danielaparker/jsoncons#490

<!-- If you are still working on the PR, open it as a Draft:
https://github.blog/2019-02-14-introducing-draft-pull-requests/. -->

<!-- If this PR updates an existing port, please uncomment and fill out
this checklist:

- [x] Changes comply with the [maintainer
guide](https://github.com/microsoft/vcpkg-docs/blob/main/vcpkg/contributing/maintainer-guide.md).
- [x] SHA512s are updated for each updated download.
- [x] The "supports" clause reflects platforms that may be fixed by this
new version.
- [x] Any fixed [CI
baseline](https://github.com/microsoft/vcpkg/blob/master/scripts/ci.baseline.txt)
entries are removed from that file.
- [x] Any patches that are no longer applied are deleted from the port's
directory.
- [x] The version database is fixed by rerunning `./vcpkg x-add-version
--all` and committing the result.
- [x] Only one version is added to each modified port's versions file.
yurybura pushed a commit to yurybura/vcpkg that referenced this issue May 8, 2024
<!-- If your PR fixes issues, please note that here by adding "Fixes
#NNNNNN." for each fixed issue on separate lines. -->

Fixes danielaparker/jsoncons#502
Fixes danielaparker/jsoncons#501
Fixes danielaparker/jsoncons#499
Fixes danielaparker/jsoncons#496
Fixes danielaparker/jsoncons#493
Fixes danielaparker/jsoncons#490

<!-- If you are still working on the PR, open it as a Draft:
https://github.blog/2019-02-14-introducing-draft-pull-requests/. -->

<!-- If this PR updates an existing port, please uncomment and fill out
this checklist:

- [x] Changes comply with the [maintainer
guide](https://github.com/microsoft/vcpkg-docs/blob/main/vcpkg/contributing/maintainer-guide.md).
- [x] SHA512s are updated for each updated download.
- [x] The "supports" clause reflects platforms that may be fixed by this
new version.
- [x] Any fixed [CI
baseline](https://github.com/microsoft/vcpkg/blob/master/scripts/ci.baseline.txt)
entries are removed from that file.
- [x] Any patches that are no longer applied are deleted from the port's
directory.
- [x] The version database is fixed by rerunning `./vcpkg x-add-version
--all` and committing the result.
- [x] Only one version is added to each modified port's versions file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants