Hi,

 Thanks, it works. The following should work:

object MediaVersionTypes {
  val `application/vnd.acme.v2+json` = 
MediaType.custom("application/vnd.acme.v1+json", 
MediaType.Encoding.Fixed(HttpCharsets.`UTF-8`))
}


Then a marshaller can be used:

implicit val personJsonFormatV1 = jsonFormat2(PersonV1)
implicit val personJsonFormatV2 = jsonFormat3(PersonV2)


def marshalPersonsXmlV1(persons: Iterable[PersonV1]) =
  <persons>
    { persons.map(marshalPersonXmlV1) }
  </persons>

implicit def personXmlFormatV1 = Marshaller.opaque[PersonV1, 
NodeSeq](marshalPersonXmlV1)


implicit def personMarshaller: ToResponseMarshaller[Person] = Marshaller.oneOf(
  Marshaller.withFixedCharset(MediaTypes.`application/json`, 
HttpCharsets.`UTF-8`) { person ⇒
    HttpResponse(entity =
      HttpEntity(ContentType(MediaTypes.`application/json`, 
HttpCharsets.`UTF-8`), PersonV1(person.name, person.age).toJson.compactPrint)
    )
  },
  Marshaller.withFixedCharset(MediaVersionTypes.`application/vnd.acme.v2+json`, 
HttpCharsets.`UTF-8`) { person ⇒
    HttpResponse(entity =
      HttpEntity(ContentType(MediaVersionTypes.`application/vnd.acme.v2+json`, 
HttpCharsets.`UTF-8`), PersonV2(person.name, person.age, 
person.married).toJson.compactPrint)
    )
  },
  Marshaller.withFixedCharset(MediaTypes.`application/xml`, 
HttpCharsets.`UTF-8`) { person ⇒
    HttpResponse(entity =
      HttpEntity.CloseDelimited(ContentType(MediaTypes.`application/xml`, 
HttpCharsets.`UTF-8`),
        Source(Marshal(PersonV1(person.name, person.age)).to[NodeSeq])
          .map(ns ⇒ ByteString(ns.toString()))
      )
    )
  }
)


Op zondag 28 juni 2015 19:56:37 UTC+2 schreef Heiko Seeberger:
>
>
> https://github.com/hseeberger/akka-sse/blob/master/akka-sse/src/main/scala/de/heikoseeberger/akkasse/MediaTypes.scala#L30
>
> --
>
> *Heiko Seeberger*
> Home: heikoseeberger.de
> Twitter: @hseeberger <https://twitter.com/hseeberger>
> Public key: keybase.io/hseeberger
>  
> On 27 Jun 2015, at 18:02, dnvriend <[email protected] <javascript:>> 
> wrote:
>
> Hi,
>
>  I want to register a new custom mediatype to use versioning using the 
> Accept header; in spray one can use the MediaTypes.register method like 
> used by the BoldRadius blog post:
>
> blog: 
> http://boldradius.com/blog-post/VAXI9i4AADMA4lXn/using-http-header-to-version-a-restful-api-in-spray
>
> spray method: 
> https://github.com/spray/spray/blob/master/spray-http/src/main/scala/spray/http/MediaType.scala#L227
>
> But using akka-http this method is made private so it cannot be used 
> outside the class:
>
>
> https://github.com/akka/akka/blob/releasing-akka-stream-and-http-experimental-1.0-RC4/akka-http-core/src/main/scala/akka/http/scaladsl/model/MediaType.scala#L266
>
> How does one register a new mediatype using akka-http?
>
> Best Regards,
>
> Dennis
>
>
>
> -- 
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ: 
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected] 
> <javascript:>.
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to