Asynchronously parse fs2 streams to JSON values with jawn.
Example
sbt jawn-fs2JVM/Test/run to see it in action:
package com.example import cats.effect.{IO, IOApp, ExitCode} import fs2.{io, text, Stream} import fs2.io.file.Files import java.nio.file.Paths import jawnfs2._ import scala.concurrent.duration._ object Example extends IOApp { // Pick your favorite supported AST (e.g., json4s, argonaut, etc.) implicit val facade = org.typelevel.jawn.ast.JawnFacade def run(args: List[String]) = { // From JSON on disk val jsonStream = Files[IO].readAll(Paths.get("testdata/random.json"), 64) // Simulate lag between chunks val lag = Stream.awakeEvery[IO](100.millis) val laggedStream = jsonStream.chunks.zipWith(lag)((chunk, _) => chunk) // Print each element of the JSON array as we read it val json = laggedStream.unwrapJsonArray .map(_.toString) .intersperse("\n") .through(text.utf8Encode) // run converts the stream into an IO, unsafeRunSync executes the IO for its effects json.through(io.stdout).compile.drain.as(ExitCode.Success) } }
Migration note
In jawn-fs2-2.x, the root package changes from jawn.fs2 to org.typelevel.jawn.fs2.
Add jawn-fs2 to your project
Add to your build.sbt:
// For cats-effect-2
libraryDependencies += "org.http4s" %% "jawn-fs2" % "1.1.0"
// For cats-effect-3
libraryDependencies += "org.typelevel" %% "jawn-fs2" % "2.0.0"
// Pick your AST: https://github.com/non/jawn#supporting-external-asts-with-jawn
libraryDependencies += "org.typelevel" %% "jawn-ast" % "1.0.0"
Compatibility matrix
| Stream Library | You need... | Status |
|---|---|---|
| fs2-3.x | "org.typelevel" %% "jawn-fs2" % "2.0.0" |
stable |
| fs2-2.x | "org.http4s" %% "jawn-fs2" % "1.1.0" |
stable |
| fs2-1.x | "org.http4s" %% "jawn-fs2" % "0.14.2" |
EOL |
| fs2-0.10.x | "org.http4s" %% "jawn-fs2" % "0.12.2" |
EOL |
| fs2-0.9.x | "org.http4s" %% "jawn-fs2" % "0.10.1" |
EOL |
| scalaz-stream-0.8a | "org.http4s" %% "jawn-streamz" % "0.10.1a" |
EOL |
| scalaz-stream-0.8.x | "org.http4s" %% "jawn-streamz" % "0.10.1" |
EOL |
The legacy scalaz-stream artifacts are on the jawn-streamz branch.