Skip to content

Commit e87e419

Browse files
committed
Add support for pretty JSON rendering
1 parent 2416d41 commit e87e419

File tree

6 files changed

+249
-151
lines changed

6 files changed

+249
-151
lines changed

Diff for: core/src/main/scala/io/bullet/borer/Borer.scala

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
package io.bullet.borer
1010

11-
import io.bullet.borer.cbor._
11+
import io.bullet.borer.cbor.*
1212
import io.bullet.borer.internal.Util
13-
import io.bullet.borer.json._
13+
import io.bullet.borer.json.*
1414

1515
import scala.annotation.unchecked.uncheckedVariance
1616

@@ -52,7 +52,7 @@ case object Cbor extends Target:
5252
output: Output,
5353
config: EncodingConfig = EncodingConfig.default,
5454
receiverWrapper: Receiver.Transformer[EncodingConfig] = CborValidation.wrapper): Writer =
55-
new Writer(output, receiverWrapper(CborRenderer(output), config), this, config)
55+
new Writer(output, receiverWrapper(CborRenderer(output, config), config), this, config)
5656

5757
/**
5858
* Constructs a new [[Reader]] that reads CBOR from the given [[Input]].
@@ -211,7 +211,7 @@ case object Json extends Target:
211211
output: Output,
212212
config: EncodingConfig = EncodingConfig.default,
213213
receiverWrapper: Receiver.Transformer[EncodingConfig] = Receiver.nopTransformer): Writer =
214-
new Writer(output, receiverWrapper(JsonRenderer(output), config), null, config)
214+
new Writer(output, receiverWrapper(JsonRenderer(output, config), config), null, config)
215215

216216
/**
217217
* Constructs a new [[Reader]] that reads JSON from the given [[Input]].
@@ -227,12 +227,14 @@ case object Json extends Target:
227227

228228
final case class EncodingConfig(
229229
bufferSize: Int = 1024,
230-
allowBufferCaching: Boolean = true
230+
allowBufferCaching: Boolean = true,
231+
indent: Int = 0
231232
) extends Borer.EncodingConfig:
232233

233234
def compressFloatingPointValues = false
234235

235236
if (bufferSize < 8) throw new IllegalArgumentException(s"bufferSize must be >= 8, but was $bufferSize")
237+
if (indent < 0) throw new IllegalArgumentException(s"indent must be non-negative, but was $indent")
236238

237239
object EncodingConfig:
238240
val default = EncodingConfig()

Diff for: core/src/main/scala/io/bullet/borer/EncodingSetup.scala

+14-10
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
package io.bullet.borer
1010

11-
import java.nio.charset.StandardCharsets.UTF_8
12-
import java.nio.ByteBuffer
13-
1411
import io.bullet.borer.internal.Renderer
1512

13+
import java.nio.charset.StandardCharsets.UTF_8
14+
import java.nio.ByteBuffer
1615
import scala.util.{Failure, Success, Try}
1716
import scala.util.control.NonFatal
1817

@@ -80,6 +79,8 @@ object EncodingSetup:
8079

8180
sealed trait JsonApi[T, Config <: Borer.EncodingConfig] extends Api[Config]:
8281

82+
def withPrettyRendering(indent: Int = 2): this.type
83+
8384
/**
8485
* Short-cut for encoding to a plain byte array, throwing an exception in case of any failures,
8586
* and then immediately UTF-8 decoding into a [[String]].
@@ -91,11 +92,14 @@ object EncodingSetup:
9192
target: Target,
9293
defaultConfig: Config,
9394
defaultWrapper: Receiver.Transformer[Config],
94-
rendererCreator: Output => Renderer)
95+
rendererCreator: (Output, Config) => Renderer)
9596
extends CommonApi.Impl[Config](defaultConfig, defaultWrapper) with JsonApi[T, Config] with Sealed[Output, Any]:
9697

9798
private[this] var _output: Output = _
9899

100+
def withPrettyRendering(indent: Int): Impl.this.type =
101+
withConfig(config.asInstanceOf[Json.EncodingConfig].copy(indent = indent).asInstanceOf[Config])
102+
99103
def toUtf8String: String = new String(toByteArray, UTF_8)
100104

101105
def toByteArray: Array[Byte] = to[Array[Byte]].result
@@ -119,37 +123,37 @@ object EncodingSetup:
119123
this.asInstanceOf[Sealed[op.Out, R]]
120124

121125
def result: Any =
122-
val renderer = rendererCreator(_output)
126+
val renderer = rendererCreator(_output, config)
123127
try render(renderer).out.result()
124128
catch
125129
case e: Borer.Error[_] => throw e.withOut(renderer.out)
126130
case NonFatal(e) => throw new Borer.Error.General(renderer.out, e)
127131

128132
def resultTry: Try[Any] =
129-
val renderer = rendererCreator(_output)
133+
val renderer = rendererCreator(_output, config)
130134
try Success(render(renderer).out.result())
131135
catch
132136
case e: Borer.Error[_] => Failure(e.withOut(renderer.out))
133137
case NonFatal(e) => Failure(new Borer.Error.General(renderer.out, e))
134138

135139
def resultEither: Either[Borer.Error[Output], Any] =
136-
val renderer = rendererCreator(_output)
140+
val renderer = rendererCreator(_output, config)
137141
try Right(render(renderer).out.result())
138142
catch
139143
case e: Borer.Error[_] => Left(e.withOut(renderer.out))
140144
case NonFatal(e) => Left(new Borer.Error.General(renderer.out, e))
141145

142-
def output: Output = render(rendererCreator(_output)).out
146+
def output: Output = render(rendererCreator(_output, config)).out
143147

144148
def outputTry: Try[Output] =
145-
val renderer = rendererCreator(_output)
149+
val renderer = rendererCreator(_output, config)
146150
try Success(render(renderer).out)
147151
catch
148152
case e: Borer.Error[_] => Failure(e.withOut(renderer.out))
149153
case NonFatal(e) => Failure(new Borer.Error.General(renderer.out, e))
150154

151155
def outputEither: Either[Borer.Error[Output], Output] =
152-
val renderer = rendererCreator(_output)
156+
val renderer = rendererCreator(_output, config)
153157
try Right(render(renderer).out)
154158
catch
155159
case e: Borer.Error[_] => Left(e.withOut(renderer.out))

Diff for: core/src/main/scala/io/bullet/borer/Output.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
package io.bullet.borer
1010

11-
import io.bullet.borer.output.{ToByteArrayOutput, ToByteBufferOutput, ToFileOutput, ToOutputStreamOutput, ToUnitOutput}
11+
import io.bullet.borer.output.*
1212

1313
import scala.annotation.tailrec
1414

Diff for: core/src/main/scala/io/bullet/borer/cbor/CborRenderer.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
package io.bullet.borer.cbor
1010

11-
import java.nio.charset.StandardCharsets.UTF_8
12-
13-
import io.bullet.borer._
11+
import io.bullet.borer.*
1412
import io.bullet.borer.internal.{Renderer, Util}
1513

14+
import java.nio.charset.StandardCharsets.UTF_8
15+
1616
/**
1717
* Encapsulates basic CBOR encoding logic.
1818
* Has no internal state and can therefore be a singleton object.
@@ -111,5 +111,5 @@ final private[borer] class CborRenderer(var out: Output) extends Renderer:
111111
v += majorType; out
112112
}).writeByte(v.toByte)
113113

114-
object CborRenderer extends (Output => CborRenderer):
115-
def apply(out: Output) = new CborRenderer(out)
114+
object CborRenderer extends ((Output, Borer.EncodingConfig) => CborRenderer):
115+
def apply(out: Output, config: Borer.EncodingConfig) = new CborRenderer(out)

0 commit comments

Comments
 (0)