Closed danielthegray closed 4 years ago
I would prefer not to build this into serde_json, but the Formatter that is used for layout by the serializer is plugable (https://docs.rs/serde_json/1.0.56/serde_json/struct.Serializer.html#method.with_formatter) and you could write one which generates the layout that you showed, either via a heuristic that applies to numeric arrays or maps with one entry, or based on key path such that the caller would indicate they want keys.daniel
placed on one line.
There are some cases where I believe it would be useful to pretty-print an entire object, but forego the pretty-printing for a field of that object where it is more desirable for the JSON object under that field to be in one line.
Case in point, the JSON files encoded for my Vaultfile project. The basic idea is a file where people can store shared secrets using public-key cryptography.
Since vaultfiles would be meant to be added to source control, it would be useful for them to have secret values and the different public keys on separate lines. However, pretty printing the RSA public key as well makes it a bit more of a pain since it adds ~70 new lines to the file (instead of just one, as I would prefer).
Example:
instead of the terser: