Skip to content

Commit 80f46ad

Browse files
committed
Add documentation for new JSON pretty printing feature
1 parent e87e419 commit 80f46ad

File tree

2 files changed

+89
-29
lines changed

2 files changed

+89
-29
lines changed

Diff for: site/src/main/paradox/borer-core/JSON-specifics.md

+41-29
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ low-level data items one would use this approach:
2222
But on a slightly higher level the `Writer` also gives you a way to write arrays (and maps) without having distinguish
2323
between [CBOR] and [JSON] yourself:
2424

25-
@@snip [-]($test$/JsonSpecificsSpec.scala) { #writeArrayOpen-close }
25+
@@snip [-]($test$/JsonSpecificsSpec.scala) { #writeArrayOpen-close }
2626

2727
As long as you rely on the somewhat higher-level parts of the `Reader` and `Writer` APIs or construct your
28-
(de)serialization purely logic from _borer_'s built-in or (and/or @ref:[derived](../borer-derivation/index.md)) Encoders and
28+
(de)serialization purely logic from _borer_'s built-in or (and/or @ref:[derived](../borer-derivation/index.md)) Encoders
29+
and
2930
Decoders, your application will support both [CBOR] and [JSON] at the same time without any special casing whatsoever.
3031

3132

@@ -38,7 +39,7 @@ This is typically worked around by mapping binary data to a "Base Encoding", e.g
3839
In order to give your application an easy and flexible way to integrate with other systems _borer_ supports a number
3940
of base encodings out of the box, specifically:
4041

41-
- [base16](https://tools.ietf.org/html/rfc4648#section-8)
42+
- [base16](https://tools.ietf.org/html/rfc4648#section-8)
4243
- [base32](https://tools.ietf.org/html/rfc4648#section-6)
4344
- [base32hex](https://tools.ietf.org/html/rfc4648#section-7)
4445
- [base32crockford](https://en.wikipedia.org/wiki/Base32#Crockford's_Base32)
@@ -53,6 +54,15 @@ In order to switch to a different base encoding in a particular scope define the
5354
@@snip [-]($test$/JsonSpecificsSpec.scala) { #alternative-base-encoding }
5455

5556

57+
JSON Pretty Printing
58+
--------------------
59+
60+
Normally _borer_ will output JSON in the most compact form, with no whitespace padding anywhere.
61+
However, you can enable "pretty" JSON rendering as shown here:
62+
63+
@@snip [-]($test$/JsonSpecificsSpec.scala) { #json-pretty-printing }
64+
65+
5666
When (not) to use _borer_ for JSON
5767
----------------------------------
5868

@@ -67,15 +77,6 @@ However, if all you need is an efficient way to convert raw network- or disk-byt
6777
from your data model types, with no (or few) dependencies and maybe even with the option to target [CBOR] with no
6878
additional work required from your side, then _borer_ should be a good choice.
6979

70-
@@@ note
71-
72-
Since _borer_ doesn't really work with `String` and `Char`, but rather raw bytes only, it also doesn't support
73-
encoding to a "pretty", i.e. nicely formatted, JSON representation.<br>
74-
_borer_ always outputs [JSON] in the most compact form.
75-
(It can, of course, read "pretty" [JSON] documents without any issue.)
76-
77-
@@@
78-
7980

8081
Comparison with other Scala JSON Libraries
8182
------------------------------------------
@@ -88,7 +89,7 @@ Comparison with other Scala JSON Libraries
8889
: AST/DOM- and type-class-based design
8990

9091
@@@ div { .pros }
91-
92+
9293
- very mature
9394
- allows for extensive DOM-manipulation
9495
- many integration option already available
@@ -111,7 +112,7 @@ Comparison with other Scala JSON Libraries
111112
: AST/DOM- and type-class-based design
112113

113114
@@@ div { .pros }
114-
115+
115116
- zero dependencies
116117

117118
@@@
@@ -137,7 +138,7 @@ Comparison with other Scala JSON Libraries
137138
@@@ div { .pros }
138139

139140
- zero dependencies
140-
- optional DOM
141+
- optional DOM
141142
- also supports [MessagePack]
142143
- compatible with [scala.js]
143144

@@ -150,7 +151,7 @@ Comparison with other Scala JSON Libraries
150151
- _borer_ decodes [JSON] more than 4 times as fast
151152
- no [Scala Native] support
152153
- no [CBOR] support
153-
154+
154155
@@@
155156

156157
---
@@ -166,7 +167,7 @@ Comparison with other Scala JSON Libraries
166167
@@@
167168

168169
@@@ div { .cons }
169-
170+
170171
- no type class-based API
171172
- several non-Scala dependencies
172173
- not compatible with [scala.js] and [Scala Native]
@@ -200,15 +201,26 @@ Comparison with other Scala JSON Libraries
200201

201202
---
202203

203-
[CBOR]: https://cbor.io/
204-
[JSON]: https://json.org/
205-
[RFC 4648]: https://tools.ietf.org/html/rfc4648
206-
[base64]: https://tools.ietf.org/html/rfc4648#section-4
207-
[Circe]: https://circe.github.io/circe/
208-
[spray-json]: https://github.com/spray/spray-json/
209-
[json-benchmark-files]: https://github.com/sirthias/borer/tree/master/benchmarks/src/main/resources
210-
[Jackson Scala]: https://github.com/FasterXML/jackson-module-scala
211-
[µPickle]: https://com-lihaoyi.github.io/upickle/
212-
[Jsoniter Scala]: https://github.com/plokhotnyuk/jsoniter-scala
213-
[MessagePack]: https://msgpack.org/
214-
[scala.js]: https://www.scala-js.org/
204+
[CBOR]: https://cbor.io/
205+
206+
[JSON]: https://json.org/
207+
208+
[RFC 4648]: https://tools.ietf.org/html/rfc4648
209+
210+
[base64]: https://tools.ietf.org/html/rfc4648#section-4
211+
212+
[Circe]: https://circe.github.io/circe/
213+
214+
[spray-json]: https://github.com/spray/spray-json/
215+
216+
[json-benchmark-files]: https://github.com/sirthias/borer/tree/master/benchmarks/src/main/resources
217+
218+
[Jackson Scala]: https://github.com/FasterXML/jackson-module-scala
219+
220+
[µPickle]: https://com-lihaoyi.github.io/upickle/
221+
222+
[Jsoniter Scala]: https://github.com/plokhotnyuk/jsoniter-scala
223+
224+
[MessagePack]: https://msgpack.org/
225+
226+
[scala.js]: https://www.scala-js.org/

Diff for: site/src/test/scala/io/bullet/borer/site/JsonSpecificsSpec.scala

+48
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,52 @@ class JsonSpecificsSpec extends BorerSuite {
7272
}
7373
// #alternative-base-encoding
7474
}
75+
76+
test("json pretty printing") {
77+
// #json-pretty-printing
78+
import io.bullet.borer.{Codec, Json}
79+
import io.bullet.borer.derivation.MapBasedCodecs.*
80+
81+
sealed trait Animal derives Codec.All
82+
case class Dog(age: Int, name: String) extends Animal
83+
case class Cat(weight: Double, color: String, home: String) extends Animal
84+
case class Mouse(colorRGB: List[Int], tail: Boolean) extends Animal
85+
86+
val animals = List(
87+
Dog(5, "Rufus"),
88+
Cat(4.7, "red", "next door"),
89+
Mouse(colorRGB = List(73, 42, 64), tail = false),
90+
)
91+
92+
Json
93+
.encode(animals)
94+
.withPrettyRendering(indent = 2) // enables pretty printing
95+
.toUtf8String ==>
96+
"""|[
97+
| {
98+
| "Dog": {
99+
| "age": 5,
100+
| "name": "Rufus"
101+
| }
102+
| },
103+
| {
104+
| "Cat": {
105+
| "weight": 4.7,
106+
| "color": "red",
107+
| "home": "next door"
108+
| }
109+
| },
110+
| {
111+
| "Mouse": {
112+
| "colorRGB": [
113+
| 73,
114+
| 42,
115+
| 64
116+
| ],
117+
| "tail": false
118+
| }
119+
| }
120+
|]""".stripMargin
121+
// #json-pretty-printing
122+
}
75123
}

0 commit comments

Comments
 (0)