Skip to content

Instantly share code, notes, and snippets.

@BenFradet
Created July 29, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenFradet/a69e3fa86ad2654ed017e2cb2881c307 to your computer and use it in GitHub Desktop.
Save BenFradet/a69e3fa86ad2654ed017e2cb2881c307 to your computer and use it in GitHub Desktop.
circe-fs2 example
lazy val fs2Version = "0.9.7"
lazy val circeVersion = "0.8.0"
lazy val root = (project in file("."))
.settings(
name := "circe-fs2-example",
scalaVersion := "2.12.3",
libraryDependencies ++=
"co.fs2" %% "fs2-io" % fs2Version +: Seq(
"io.circe" %% "circe-generic",
"io.circe" %% "circe-fs2"
).map(_ % circeVersion)
)
import _root_.fs2.{io, text, Task, Stream}
import _root_.io.circe.Json
import _root_.io.circe.fs2._
import _root_.io.circe.generic.auto._
import java.nio.file.Paths
object Main {
case class Popularity(repo: String, stars: Int)
def main(args: Array[String]): Unit = {
val stringStream: Stream[Task, String] =
io.file.readAll[Task](Paths.get("popularity.json"), 4096)
.through(text.utf8Decode)
val parsedStream: Stream[Task, Json] =
stringStream.through(stringParser)
val popularityStream: Stream[Task, Popularity] =
parsedStream.through(decoder[Task, Popularity])
println(popularityStream.runLog.unsafeAttemptRun())
}
}
[
{
"repo": "circe-fs2",
"stars": 13
},
{
"repo": "circe-yaml",
"stars": 32
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment