open-source-parsers / open-source-parsers/jsoncpp

Braces/brackets on the same line

Open
#1,336 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C++
Stars
8.9k
Forks
2.7k
Avg merge
31m
Merged PRs (30d)
1

Description

Is your feature request related to a problem? Please describe.
When printing JSON (std::cout << json, StreamWriterBuilder+BuiltStyledStreamWriter), if an object contains another object or an array, it looks like this:

{
	"hello" : 
	{
		"world" : "!"
	},
	"world" : 
	[
		"!"
	]
}

while I want it to look like this:

{
	"hello" : {
		"world" : "!"
	},
	"world" : [
		"!"
	]
}

Also, currently there's trailing whitespace after the colons which is annoying.

Describe the solution you'd like
To solve both problems at once, whenever the opening { or [ of an object or array is printed after a colon of an object key:value pair, I want it to not move to a new line and indent and instead print it in the same line.

Describe alternatives you've considered
I've considered solving the trailing whitespace problem while keeping the brace/bracket the way it is, but it's easier this way and I prefer it.

Additional context
I think a setting should be added to StreamWriterBuilder called something like whitespaceStyle, and currently have two options: colonBracesOnNewLine and colonBracesOnSameLine.

This way instead of a boolean setting, in the future maybe one can add whitespaceStyle: leftComma for example, for this:

{
	"foo": "bar"
	, "hello": "world"
	, "this is": "weird"
}

I'm wondering whether combining values should be possible or not, for example using both colonBracesOnSameLine and leftComma at the same time, so maybe whitespaceStyle can be an array. Or separate boolean flags. Not sure.

More future options for whitespaceStyle:
minimal to output the JSON as compactly as possible without any spaces.
commaBracesOnSameLine/bracketBraceOnSameLine for having { be on the same line as a preceding ,/[ respectively.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading StreamWriterBuilder, BuiltStyledStreamWriter, and the std::cout JSON-writing path mentioned in the issue to locate how styled output places braces and whitespace. Define the formatting option and verify that nested objects and arrays after object-key colons stay on the same line without trailing whitespace; the issue does not name a test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.