Skip to content

Instantly share code, notes, and snippets.

@rbuckland
Created January 12, 2016 03:05
Show Gist options
  • Save rbuckland/9049afd919c3885be2cc to your computer and use it in GitHub Desktop.
Save rbuckland/9049afd919c3885be2cc to your computer and use it in GitHub Desktop.
akka-http with Yaml Marshalling / Unmarshalling
SBT > projectX > test
[info] Compiling 1 Scala source to /Users/rbuckland/projects/projectX/target/scala-2.11/test-classes...
[error] /Users/rbuckland/projects/projectX/src/test/scala/sample/WidgetMarshalTest.scala:25: could not find implicit value for parameter m: akka.http.scaladsl.marshalling.Marshaller[sample.Widget,akka.http.scaladsl.model.RequestEntity]
[error] val entity = Marshal(w).to[RequestEntity].futureValue
[error] ^
[error] one error found
[error] (projectX/test:compileIncremental) Compilation failed
[error] Total time: 2 s, completed 12-Jan-2016 14:01:56
package sample
import akka.http.scaladsl.marshalling._
import akka.http.scaladsl.unmarshalling._
import akka.http.scaladsl.model.RequestEntity
import org.scalatest._
import net.jcazevedo.moultingyaml._
case class Widget(name:String,color:String)
trait WidgetUnMProtocols extends DefaultYamlProtocol {
implicit val widgetFormat = yamlFormat2(Widget)
implicit def unmar = Unmarshaller.strict[String,Widget] { _.parseYaml.convertTo[Widget] }
implicit def mar = Marshaller.opaque[Widget, String] { _.toYaml.prettyPrint }
}
class WidgetSpec extends FlatSpec with Matchers with WidgetUnMProtocols {
"marshalling" should "work" in {
val w = Widget("thingy","yellow")
val entity = Marshal(w).to[RequestEntity].futureValue
Unmarshal(entity).to[Widget].futureValue shouldBe w
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment