Repository navigation
Add :pretty option to JSON encoding - #16041
gilbertwong96 wants to merge 1 commit into
Conversation
0fa05a4 to
f2cc249
Compare
Assisted-by: pi:deepseek-v4.1-flash
f2cc249 to
7820203
Compare
|
Closing this: the measurements changed my mind. To match the layout Jason and Torque produce, this has to be a text pass over the encoded output, which costs about 4.5x the compact encode (68 ms vs 47 ms for encode -> decode -> That cost is hard to justify in the standard library for a convenience API, so I would rather settle the direction first. Findings and the remaining options are on the mailing list: https://groups.google.com/g/elixir-lang-core/c/Y39qc6zcQto The branch stays available if the conclusion is that this is worth carrying. |
Adds a
:prettyoption toJSON.encode!/2andJSON.encode_to_iodata!/2, matching the shapeJasonandTorquealready ship:trueindents by two spaces, a binary of spaces and tabs is used as the indent unit. Empty containers stay on one line,:gains a trailing space, and there is no trailing newline. Unknown options and non-keyword arguments raise.The whitespace is written in a single pass over the iodata the encoder already produced, so
JSON.Encoderis untouched: derived implementations, calendar types,Durationandnilformat correctly without a decode/re-encode step. The pass is skipped unless:prettyis given.The second argument now accepts an encoder function or a keyword list, which is additive since
encode!(term, [pretty: true])raisedBadFunctionErrorbefore.Proposed on the mailing list first: https://groups.google.com/g/elixir-lang-core/c/Y39qc6zcQto (no replies yet).
make test_stdlibpasses.Assisted-by: pi:deepseek-v4.1-flash