-
Notifications
You must be signed in to change notification settings - Fork 136
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
JSON can be encoded to multi-line string with configurable indentation #410
Comments
API is a good starting point. I'm diggin' the opt-in nature of this signature @tylerschultz suggested: json.encode(json_struct, indent=4) "pretty" can be in the eye of the formatter, so being more specific is also appealing to me. As is the ability to specify the actual indent size. Would |
This issue points to a known need, extends the |
I'm interested in working on a PR. LMK if there's anything to discuss. |
Fantastic! Thanks for raising your hand, @tylerschultz. "Acceptance Criteria" that come to mind:
|
Above criteria sets a reasonably defined scope ==> "ready for dev" |
@tylerschultz Regarding PR modes:
|
Some longwinded support for your proposed acceptance criteria:
👍 fwiw, this is what in python's,
I don't see any effective limit in Out of curiosity, how did you identify that CWE when thinking about the acceptance criteria here? |
@pivotaljohn Please consider this PR picked up. |
- allow json.encode to receive an indent named argument: ``` json.encode('{"a":"b"}', indent=2) ``` - indent greater than 0 indicates the json shall be rendered as a multiline string indented with the specified number of spaces - indent=0 and omitting indent both result in a single line json string - errors are returned if indent > 4 or if arguments other than indent are included Issue carvel-dev#410 Co-authored-by: Tyler Schultz <[email protected]>
That's a perfect source of inspiration, here, given Starlark's lineage. Thank you. And that read is on the money.
Yup, I was thinking along the same lines. Like, what exactly are you doing if you need more than 4 spaces for indentation? I don't mean to sound snarky, if it's legit, I'd love to know!
I can tell you exactly... lemme look that up in my zettelkasten.... right... So, about a month ago I became aware of this bug: kubernetes/kubernetes#89535. Which had attached to it this CVE: https://nvd.nist.gov/vuln/detail/CVE-2019-11254 in which you'll see a "Weakness Enumeration" section. Thar be rabbit-holes. |
@tylerschultz + @mcwumbly, re: documentation. Yes, please:
|
- allow json.encode to receive an indent named argument: ``` json.encode('{"a":"b"}', indent=2) ``` - indent greater than 0 indicates the json shall be rendered as a multiline string indented with the specified number of spaces - indent=0 and omitting indent both result in a single line json string - errors are returned if indent > 4 or if arguments other than indent are included Issue carvel-dev#410 Co-authored-by: Tyler Schultz <[email protected]>
Created requested PR into docs. 👍🏻 |
I believe that this issue can be closed out now. |
I believe that this issue can be closed out now. |
Describe the problem/challenge you have
I have a yaml value that is a a pretty print, multi-line, JSON string. When I decode that JSON value, modify the json struct, then encode it back to a string, it loses it's multi-line pretty print and is one lined. This makes it visually difficult to compare to the original JSON string.
Describe the solution you'd like
I'd like the JSON module to include a way to pretty print. For example it could be
json.encode_pretty(json_struct)
or add a named argument to encode, like this:json.encode(json_struct, indent=4)
. The resulting encoded yaml and JSON would look something like:(jsr) edit: included link to originating Slack convo
Vote on this request
This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.
👍 "I would like to see this addressed as soon as possible"
👎 "There are other more important things to focus on right now"
We are also happy to receive and review Pull Requests if you want to help working on this issue.
The text was updated successfully, but these errors were encountered: