Akka-http: Show example how to build a protobuf marshaller

Created on 7 Mar 2018  路  4Comments  路  Source: akka/akka-http

This seems to be enjoyed by people and has a ticket here: https://github.com/scalapb/ScalaPB/issues/247

Hope @alanbur from that thread will be able to contribute the docs:)

help wanted docs 1 - triaged

Most helpful comment

I ran into issues smoothly integrating akka-http with scalapb and spent some time getting the datatypes to line up, I came up with this:

import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import scalapb.json4s.JsonFormat
import scalapb.{GeneratedMessage, GeneratedMessageCompanion}

trait ClientMarshallingProtocol {

  implicit def unmarshalProto[T <: GeneratedMessage with scalapb.Message[T] : GeneratedMessageCompanion : Manifest]: FromEntityUnmarshaller[T] = {
    Unmarshaller.stringUnmarshaller.map(JsonFormat.fromJsonString[T](_))
  }

}

This needs a reference to scalapb-json4s in build.sbt (grab the newest version):

"com.thesamet.scalapb" %% "scalapb-json4s" % "0.7.0"

Then just have your Routes mixin ClientMarshallingProtocol and this sample usage should build and run without problem:

pathPrefix(myPrefix) {
        post {
          entity(as[MyIncomingRequest]) { myIncomingRequest => 
          ...

I don't know how/where to put this info in the docs to close the ticket, so I'll just leave it here and hopefully it helps out others :)

All 4 comments

This is NOT about shipping library support, only about "this is how to do it"

On 07/03/18 16:11, ktoso wrote:

This is NOT about shipping library support, only about "this is how to do it"

Exactly so - the discussion in the original bug makes that point and I
agree that all that's really needed is some example code in the docs.

--

Alan Burlison

I ran into issues smoothly integrating akka-http with scalapb and spent some time getting the datatypes to line up, I came up with this:

import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import scalapb.json4s.JsonFormat
import scalapb.{GeneratedMessage, GeneratedMessageCompanion}

trait ClientMarshallingProtocol {

  implicit def unmarshalProto[T <: GeneratedMessage with scalapb.Message[T] : GeneratedMessageCompanion : Manifest]: FromEntityUnmarshaller[T] = {
    Unmarshaller.stringUnmarshaller.map(JsonFormat.fromJsonString[T](_))
  }

}

This needs a reference to scalapb-json4s in build.sbt (grab the newest version):

"com.thesamet.scalapb" %% "scalapb-json4s" % "0.7.0"

Then just have your Routes mixin ClientMarshallingProtocol and this sample usage should build and run without problem:

pathPrefix(myPrefix) {
        post {
          entity(as[MyIncomingRequest]) { myIncomingRequest => 
          ...

I don't know how/where to put this info in the docs to close the ticket, so I'll just leave it here and hopefully it helps out others :)

Thanks for sharing, @micmorris.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samidalouche picture samidalouche  路  6Comments

jrudolph picture jrudolph  路  3Comments

MichaelZinsmaier picture MichaelZinsmaier  路  6Comments

jlprat picture jlprat  路  6Comments

kstokoz picture kstokoz  路  6Comments