Skip to content

Commit

Permalink
Fix JSON Lines handling using new pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
satabin committed Mar 23, 2024
1 parent b856965 commit cef0a41
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions site/cookbooks/jsonlines.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ val array =
.through(readJsonLines)
.through(fs2.data.json.ast.tokenize)
.through(fs2.data.json.wrap.asTopLevelArray)
.through(fs2.data.json.render.pretty())
.through(fs2.data.json.render.prettyPrint(width = 35))

array
.compile
Expand All @@ -80,9 +80,13 @@ Similarly, using `fs2` and `fs2-data` operators, we can generate a stream that w
```scala mdoc
def writeJsonLines(input: Stream[IO, Json]): Stream[IO, Byte] =
input
.flatMap { data =>
.map { data =>
// rule #2: values must be encoded on single lines
Stream.emit(data).through(fs2.data.json.ast.tokenize).through(fs2.data.json.render.compact)
Stream.emit(data)
.through(fs2.data.json.ast.tokenize)
.through(fs2.data.json.render.compact)
.compile
.string
}
// rule #3: new line delimiter is '\n'
.intersperse("\n")
Expand Down
10 changes: 7 additions & 3 deletions site/cookbooks/scripts/jsonlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ object JsonLines

def writeJsonLines(input: Stream[IO, Json]): Stream[IO, Byte] =
input
.flatMap { data =>
.map { data =>
// rule #2: values must be encoded on single lines
Stream.emit(data).through(fs2.data.json.ast.tokenize).through(fs2.data.json.render.compact)
Stream.emit(data)
.through(fs2.data.json.ast.tokenize)
.through(fs2.data.json.render.compact)
.compile
.string
}
// rule #3: new line delimiter is '\n'
.intersperse("\n")
Expand All @@ -67,7 +71,7 @@ object JsonLines
.through(readJsonLines)
.through(fs2.data.json.ast.tokenize)
.through(fs2.data.json.wrap.asTopLevelArray)
.through(fs2.data.json.render.pretty())
.through(fs2.data.json.render.prettyPrint())
.through(fs2.text.utf8.encode)
.through(fs2.io.stdout)
.compile
Expand Down

0 comments on commit cef0a41

Please sign in to comment.