8
8
9
9
package io .bullet .borer
10
10
11
- import java .nio .charset .StandardCharsets .UTF_8
12
- import java .nio .ByteBuffer
13
-
14
11
import io .bullet .borer .internal .Renderer
15
12
13
+ import java .nio .charset .StandardCharsets .UTF_8
14
+ import java .nio .ByteBuffer
16
15
import scala .util .{Failure , Success , Try }
17
16
import scala .util .control .NonFatal
18
17
@@ -80,6 +79,8 @@ object EncodingSetup:
80
79
81
80
sealed trait JsonApi [T , Config <: Borer .EncodingConfig ] extends Api [Config ]:
82
81
82
+ def withPrettyRendering (indent : Int = 2 ): this .type
83
+
83
84
/**
84
85
* Short-cut for encoding to a plain byte array, throwing an exception in case of any failures,
85
86
* and then immediately UTF-8 decoding into a [[String ]].
@@ -91,11 +92,14 @@ object EncodingSetup:
91
92
target : Target ,
92
93
defaultConfig : Config ,
93
94
defaultWrapper : Receiver .Transformer [Config ],
94
- rendererCreator : Output => Renderer )
95
+ rendererCreator : ( Output , Config ) => Renderer )
95
96
extends CommonApi .Impl [Config ](defaultConfig, defaultWrapper) with JsonApi [T , Config ] with Sealed [Output , Any ]:
96
97
97
98
private [this ] var _output : Output = _
98
99
100
+ def withPrettyRendering (indent : Int ): Impl .this .type =
101
+ withConfig(config.asInstanceOf [Json .EncodingConfig ].copy(indent = indent).asInstanceOf [Config ])
102
+
99
103
def toUtf8String : String = new String (toByteArray, UTF_8 )
100
104
101
105
def toByteArray : Array [Byte ] = to[Array [Byte ]].result
@@ -119,37 +123,37 @@ object EncodingSetup:
119
123
this .asInstanceOf [Sealed [op.Out , R ]]
120
124
121
125
def result : Any =
122
- val renderer = rendererCreator(_output)
126
+ val renderer = rendererCreator(_output, config )
123
127
try render(renderer).out.result()
124
128
catch
125
129
case e : Borer .Error [_] => throw e.withOut(renderer.out)
126
130
case NonFatal (e) => throw new Borer .Error .General (renderer.out, e)
127
131
128
132
def resultTry : Try [Any ] =
129
- val renderer = rendererCreator(_output)
133
+ val renderer = rendererCreator(_output, config )
130
134
try Success (render(renderer).out.result())
131
135
catch
132
136
case e : Borer .Error [_] => Failure (e.withOut(renderer.out))
133
137
case NonFatal (e) => Failure (new Borer .Error .General (renderer.out, e))
134
138
135
139
def resultEither : Either [Borer .Error [Output ], Any ] =
136
- val renderer = rendererCreator(_output)
140
+ val renderer = rendererCreator(_output, config )
137
141
try Right (render(renderer).out.result())
138
142
catch
139
143
case e : Borer .Error [_] => Left (e.withOut(renderer.out))
140
144
case NonFatal (e) => Left (new Borer .Error .General (renderer.out, e))
141
145
142
- def output : Output = render(rendererCreator(_output)).out
146
+ def output : Output = render(rendererCreator(_output, config )).out
143
147
144
148
def outputTry : Try [Output ] =
145
- val renderer = rendererCreator(_output)
149
+ val renderer = rendererCreator(_output, config )
146
150
try Success (render(renderer).out)
147
151
catch
148
152
case e : Borer .Error [_] => Failure (e.withOut(renderer.out))
149
153
case NonFatal (e) => Failure (new Borer .Error .General (renderer.out, e))
150
154
151
155
def outputEither : Either [Borer .Error [Output ], Output ] =
152
- val renderer = rendererCreator(_output)
156
+ val renderer = rendererCreator(_output, config )
153
157
try Right (render(renderer).out)
154
158
catch
155
159
case e : Borer .Error [_] => Left (e.withOut(renderer.out))
0 commit comments