Akka Streams Json Support
This is a fork of akka-stream-json since it no longer appears to be maintained. Hence the main differences between akka-stream-json and this project is.
- The project is called akka-streams-json rather than akka-stream-json. This is just to differentiate the git repos and project names.
- This project aims to be updated to the latest versions of akka/akka-http/circe as frequent as possible. PR's are also welcome to maintain the project
- We use ScalaTest rather then Specs2 for testing. This is because ScalaTest has better
Futuresupport compared to Specs2 (in order to do tests against
Futurein Specs2 you have to block on the
Futurewhich degrades performance a lot).
Apart from this, the actual initial code/implementation is exactly the same as the fork, the only difference being the package names (i.e. using
org.mdedetrich rather than
de.knutwalker). The following contents of README.md was also mainly copied from the original repo.
There are two main modules,
akka-stream-json is the basis and provides the stream-based parser while
akka-http-json enabled support to use the desired json library as an Unmarshaller.
libraryDependencies ++= List( "org.mdedetrich" %% "akka-stream-json" % "0.3.0", "org.mdedetrich" %% "akka-http-json" % "0.3.0" )
akka-streams-json depends on
jawn-parser at version
0.14.1 and is compiled against
akka-stream at version
2.5.11. The circe submodule depends on version
circe-jawn The Akka Http submodule depends on version
akka-stream-json is published for Scala 2.12 and 2.11.
The parser lives at
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.akka.http.JsonSupport._ or mixin
... with org.mdedetrich.akka.http.JsonSupport.
Given an implicit jawn facade, this enable you to decode into the respective Json AST using the Akka HTTP marshalling framework. As jawn is only about parsing and does not abstract over rendering, you'll only get an Unmarshaller.
libraryDependencies ++= List( "org.mdedetrich" %% "akka-stream-circe" % "0.3.0", "org.mdedetrich" %% "akka-http-circe" % "0.3.0" )
(Using circe 0.11.1)
These support modules allow you to directly marshall from/unmarshall into your data types using circes
Encoder type classes.
Just mixin or import
org.mdedetrich.akka.http.support.CirceHttpSupport for Http or pipe your
Source[ByteString, _].via(org.mdedetrich.akka.stream.CirceStreamSupport.decode[A]) to get 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.akka.http.support.CirceStreamSupport.JsonParsingException. The exception provides Circe cursor history, current cursor and the type hint of the error.
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.
This code is open source software licensed under the Apache 2.0 License.