I want to customize the output of a JSONEncoder.
The prettyPrinted
formatting option does what I want for containers with multiple values. That is, arrays with at least 2 elements, dictionaries with at least 2 keys, objects with at least 2 fields, and so forth.
But when an array, dictionary, or object has only one entry (or none), I want the encoded json to stay on a single line (provided the entry itself encodes to a single-line result).
For example:
// [Int]
[] // Encode to []
[1] // Encode to [1]
[1, 2] // Encode to:
// [
// 1,
// 2
// ]
// [String: [Int]]
["a": []] // Encode to { "a" : [] }
["a": [1]] // Encode to { "a" : [1] }
["a": [1], "b": [2]] // Encode to:
// {
// "a" : [1],
// "b" : [2]
// }
["a": [1, 2]] // Encode to:
// {
// "a" : [
// 1,
// 2
// ]
// }
Is something like this possible?
What about taking it further by letting multi-element arrays encode to a single line (with a space after each comma), unless that would result in a length exceeding some threshold?
Basically, I’m looking for a “compactPrettyPrinted” format, which uses whitespace and newlines freely, but not excessively. Does such a thing exist, or is there a way to create one?