com-lihaoyi / upickle

uPickle: a simple, fast, dependency-free JSON & Binary (MessagePack) serialization library for Scala
https://com-lihaoyi.github.io/upickle
MIT License
706 stars 158 forks source link

Make empty arrays and dictionaries render compactly even when indent > 0 #501

Closed lihaoyi closed 12 months ago

lihaoyi commented 12 months ago

Fixes https://github.com/com-lihaoyi/upickle/issues/499

We take a similar strategy as we do for adding commas: rather than calling renderIndent immediately after opening a new array/dict and at visitEnd, we instead set an indentBuffered flag and defer the renderIndent until later. Then each flushBuffer call can render the indent where necessary, and visitEnd can perform some additional checks that the collection is not empty before rendering the final indent

Added some basic tests to ExampleTests.scala, since some of this behavior is probably worth showing to users in the documentation