Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

akka-http integration #4

Open
eld0727 opened this issue Oct 23, 2017 · 2 comments
Open

akka-http integration #4

eld0727 opened this issue Oct 23, 2017 · 2 comments
Assignees

Comments

@eld0727
Copy link
Collaborator

eld0727 commented Oct 23, 2017

No description provided.

@eld0727 eld0727 added this to the v0.6.3 milestone Oct 23, 2017
@eld0727 eld0727 self-assigned this Oct 23, 2017
@NikitaMelnikov
Copy link
Contributor

NikitaMelnikov commented Nov 29, 2017

Hm...

This code should be enough to implement Tethys support in akka-http I think:

import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.ContentTypeRange
import akka.http.scaladsl.model.MediaTypes.`application/json`
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import akka.util.ByteString
import tethys._
import tethys.jackson._

import scala.reflect.ClassTag

trait TethysSupport {
  lazy val unmarshallerContentTypes: Seq[ContentTypeRange] = List(`application/json`)

  implicit def unmarshaller[A](implicit reader: JsonReader[A]): FromEntityUnmarshaller[A] = {
    Unmarshaller.byteStringUnmarshaller
      .forContentTypes(unmarshallerContentTypes: _*)
      .mapWithCharset {
        case (ByteString.empty, _) => throw Unmarshaller.NoContentException
        case (data, charset) => data.decodeString(charset.nioCharset.name).jsonAs[A].fold(throw _, identity)
      }
  }

  implicit def marshaller[A](implicit ct: ClassTag[A], writer: JsonWriter[A]): ToEntityMarshaller[A] = {
    Marshaller.stringMarshaller(`application/json`).compose(_.asJson)
  }
}

Show we place it as separated module or just as an example?

@eld0727 eld0727 removed this from the v0.8.0 milestone Jan 15, 2019
@Odomontois
Copy link

Is there some concern about suggesting PR to https://github.com/hseeberger/akka-http-json?
It's a perfect place for such a module, isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants