Skip to content

mdedetrich/pekko-streams-circe

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ba2875a · Sep 8, 2024
Sep 6, 2024
Jun 2, 2023
Sep 8, 2024
Jun 2, 2023
Jun 2, 2023
Jun 2, 2023
Apr 8, 2018
Apr 5, 2024
Sep 6, 2024
Apr 8, 2018
Jan 17, 2024
Sep 6, 2024

Repository files navigation

Build Status Coverage Maven Join at Gitter Apache License

Pekko Streams Circe Support

This library provides Json support for stream based applications using jawn as a parser. It supports all backends that jawn supports with support for circe provided as an example.

It is a fork of akka-streams-json built with Apache Pekko. akka-streams-json which is in itself a fork of https://github.com/knutwalker/akka-stream-json.

Installation

There are two main modules, pekko-stream-json and pekko-http-json. pekko-stream-json is the basis and provides the stream-based parser while pekko-http-json enabled support to use the desired json library as an Unmarshaller.

libraryDependencies ++= List(
  "org.mdedetrich" %% "pekko-stream-json" % <VERSION>,
  "org.mdedetrich" %% "pekko-http-json" % <VERSION>"
)

pekko-stream-circe is published for Scala 2.12, 2.13 and 3.3+.

Usage

The parser lives at org.mdedetrich.pekko.json.stream.JsonStreamParser

Use one of the constructor methods in the companion object to create the parser at various levels of abstraction, either a Stage, a Flow, or a Sink. You just add the jawn support facade of your choice, and you will can parsed into their respective Json AST.

For Http support, either import org.mdedetrich.pekko.http.JsonSupport._ or mixin ... with org.mdedetrich.pekko.http.JsonSupport.

Given an implicit jawn facade, this enables you to decode into the respective Json AST using the Pekko HTTP marshalling framework. As jawn is only about parsing and does not abstract over rendering, you'll only get an Unmarshaller.

Circe

libraryDependencies ++= List(
  "org.mdedetrich" %% "pekko-stream-circe" % "<VERSION>",
  "org.mdedetrich" %% "pekko-http-circe" % "<VERSION>"
)

(Using circe 0.14.x)

Adding support for a specific framework is quite easy.

These support modules allow you to directly marshall from/unmarshall into your data types using circe's Decoder and Encoder type classes.

Just mixin or import org.mdedetrich.pekko.http.support.CirceHttpSupport for Http or pipe your Source[ByteString, _].via(org.mdedetrich.pekko.stream.CirceStreamSupport.decode[A]) to get a Source[A, _].

This flow even supports parsing multiple json documents in whatever fragmentation they may arrive, which is great for consuming stream/sse based APIs.

If there is an error in parsing the Json you can catch org.mdedetrich.pekko.http.support.CirceStreamSupport.JsonParsingException. The exception provides Circe cursor history, current cursor and the type hint of the error.

Why jawn?

Jawn provides a nice interface for asynchronous parsing. Most other Json marshalling provider will consume the complete entity at first, convert it to a string and then start to parse. With jawn, the json is incrementally parsed with each arriving data chunk, using directly the underlying ByteBuffers without conversion.

License

This code is open source software licensed under the Apache 2.0 License.