0
votes

I am trying to consume a stream of json objects in akka-http. ( akka http version "10.0.9", akka-http-play-json version 1.10.1)

I follow examples on web but, for String I am getting:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromByteStringUnmarshaller[String]

and for my user defined Foo case class (for which I provided the json protocol):

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromByteStringUnmarshaller[server.Foo]

This is the code that is simplified. I provide a EntityStreamingSupport.json() and for the Foo object a Json Format. I don't think I need one for String. If I don't put the asSourceOf and read a simple String object or a Foo case class object the code works. What am I missing?

package server

import akka.http.scaladsl.common.EntityStreamingSupport
import akka.http.scaladsl.server.{ Directives, Route }
import akka.http.scaladsl.model.StatusCodes
import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._
import play.api.libs.json._

case class Foo(bar: String)

class StreamingMarketDataUpload extends Directives {
  implicit val jsonStreamingSupport = EntityStreamingSupport.json()
  implicit val jsonFooFormat = Json.format[Foo]
  lazy val routes: Route =
    path("upload1") {
      post {
        entity(as[String]) { input =>
          complete(StatusCodes.OK)
        }
      }
    } ~ path("upload2") { 
      post {
        // Compile error here
        entity(asSourceOf[String]) { marks =>
          complete(StatusCodes.OK)
        }
      }
    } ~ path("upload3") {
      post {
        entity(as[Foo]) { input =>
          complete(StatusCodes.OK)
        }
      }
    } ~ path("upload4") {
      post {
        // Compile error here
        entity(asSourceOf[Foo]) { marks =>
          complete(StatusCodes.OK)
        }
      }
    }
}
1

1 Answers

0
votes

asSourceOf[T] tries to consume the incoming data as a Source[T, _]. As its method signature indicates, asSourceOf[T] takes an implicit FromByteStringUnmarshaller[T] parameter. The de.heikoseeberger.akkahttpplayjson.PlayJsonSupport utility doesn't provide an implementation of this unmarshaller (as of version 1.19.0), but it does provide the unmarshallers necessary for consuming a simple String or Foo. This is why your code works when it's not using asSourceOf.

The examples in the documentation use SprayJsonSupport, which is shipped as part of Akka HTTP. If you don't want to use SprayJsonSupport, you'll have to implement a FromByteStringUnmarshaller to use asSourceOf: the linked source code can give you an idea of how to do this.