/circe-fs2

Streaming JSON parsing and decoding with fs2

Primary LanguageScala

circe-fs2

Build status Coverage status Gitter Maven Central

This project provides support for using fs2 for streaming JSON parsing and decoding with circe, a Scala library for encoding and decoding JSON to Scala types.

Parsing

Circe-fs2 provides different pipes to parse your streams of JSONs depending on whether your stream is:

  • a \n-separated stream of JSON values or value stream:
{ "repo": "circe-fs2", "stars": 14 }
{ "repo": "circe-config", "stars": 5 }
  • or a JSON array:
[
  { "repo": "circe-fs2", "stars": 14 },
  { "repo": "circe-config", "stars": 5 }
]

The appropriate pipe for the job also depends on your input stream value type (i.e. String, Byte or Segment[Byte]).

The following table sums up every pipe available as a function of the input stream value type as well as the JSON structure:

String Byte Segment[Byte]
Value stream stringStreamParser byteStreamParser byteStreamParserS
Array stringArrayParser byteArrayParser byteArrayParserS

As an example, let's say we have a stream of strings representing a JSON array, we'll pick the stringArrayParser pipe which converts a stream of String to a stream of Json, Circe's representation of JSONs:

import io.circe.fs2._
val stringStream: Stream[IO, String] = ...
val parsedStream: Stream[IO, Json] = stringStream.through(stringArrayParser)

Decoding

Circe-fs2 also comes with a decoder pipe which, given a Decoder[A], produces a Stream[F[_], Json] => Stream[F[_], A] pipe.

For example, using Circe's fully automatic derivation:

import io.circe.generic.auto._
case class Foo(a: Int, b: String)
val parsedStream: Stream[IO, Json] = ...
val decodedStream: Stream[IO, Foo] = parsedStream.through(decoder[IO, Foo])

Contributors and participation

All circe projects support the Typelevel code of conduct and we want all of their channels (Gitter, GitHub, etc.) to be welcoming environments for everyone.

Please see the circe contributors' guide for details on how to submit a pull request.

License

circe-fs2 is licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.