Created
March 28, 2017 16:35
-
-
Save Timshel/487465f5d81ecb7aa20262f19d325acb to your computer and use it in GitHub Desktop.
Stream a Json array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
val akkaStream = "com.typesafe.akka" %% "akka-stream" % "2.4.17" | |
val akkaStreamJson = "de.knutwalker" %% "akka-stream-json" % "3.2.0" | |
val jawn = "org.spire-math" %% "jawn-parser" % "0.10.4" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.NotUsed | |
import akka.stream.scaladsl.Flow | |
import akka.util.ByteString | |
import jto.validation._ | |
import play.api.libs.json._ | |
object JsonStreaming { | |
implicit val facade: jawn.Facade[JsValue] = | |
new jawn.SimpleFacade[JsValue] { | |
def jnull() = JsNull | |
def jfalse() = JsBoolean(false) | |
def jtrue() = JsBoolean(true) | |
def jnum(s: String) = JsNumber(BigDecimal(s)) | |
def jint(s: String) = JsNumber(BigDecimal(s)) | |
def jstring(s: String) = JsString(s) | |
def jarray(vs: List[JsValue]) = JsArray(vs) | |
def jobject(vs: Map[String, JsValue]) = JsObject(vs.toSeq) | |
} | |
def flow[A](implicit r: RuleLike[JsValue, A]): Flow[ByteString, VA[A], NotUsed] = { | |
de.knutwalker.akka.stream.JsonStreamParser.flow(jawn.AsyncParser.UnwrapArray).map(r.validate) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment