Hi Pavel,

you are able to transform request to the domain object leveraging
Mashalling/Unmarshalling without any custom code.

Here is an example of how to do it using spray-json:
https://github.com/2m/akka-stream-sandbox/blob/a07f824a1a783eb156f1c52b2b6f7319bf362761/src/test/scala/ServerRequestAsJson.scala

If you want to use something other that spray-json, you can write your own
marshaller/unmarshaller. Here is the source code of spray-json marshaller
and unmarshaller:
https://github.com/akka/akka/blob/release-2.3-dev/akka-http-marshallers-scala/akka-http-spray-json/src/main/scala/akka/http/scaladsl/marshallers/sprayjson/SprayJsonSupport.scala

On Thu, Apr 23, 2015 at 6:45 PM, pavel meledin <[email protected]> wrote:

> Hi everyone,
>
> I'm using akka-http 1.0-M5 and I want to make the following transformation:
>
> From *RequestContext* to *HttpEntity* then *ByteStrings* andthen
> *JsonString* andthen *Deserialize* andthen *[Domain object that came to
> server in payload]*
>
> The transformation path looks like: *RequestContext -> HttpEntity ->
> ByteStrings -> JsonString -> Deserialize -> [Domain object]*
>
> Here is a naive implementation of how to get complete request body as a
> string:
>
> // How to process request body
> val collectBodySink = Sink.fold[String, String]("")(_ + _)
>
> // Extract request body
> val source = ctx.request.entity.getDataBytes().map {
>   chunk ⇒
>     chunk.decodeString(HttpCharsets.`UTF-8`.value)
> }
>
> // Define the way how materialization should follow
> val runnable = source.toMat(collectBodySink)(Keep.right)
>
> // Run materialization process
> val requestBody = runnable.run()
>
> // Wait till materialization will be finished
> println(Await.result(requestBody, 5.seconds))
>
>
> I thought that I can describe a bunch of flows:
>
>    - From *RequestContext* to *HttpEntity*
>    - From *HttpEntity* to *ByteStrings*
>    - From *ByteStrings* to *JsonString*
>    - etc
>
> and later combine them all in the following way (pseudo code):
>
> var domainObject = Source(ctx)
>   .via(Flow[RequestContext].to[HttpEntity]())
>   .via(Flow[HttpEntity].to[ByteStrings]())
>   .via(Flow[ByteStrings].to[JsonString]())
>   .via(Flow[JsonString].to[DomainObject]())
>   .toMat(Sink.ignore)
>
>
> Questions regarding solution of this task:
>
>    1. Is it a correct idea to split such transformation into small pieces
>    in order to be able to combine them later?
>    2. How such things are suppose to be handled / implemented?
>    3. RequestContext contains a Request which contains HttpEntity which
>    contains dataBytes as a Source. In order to implement Flow from HttpEntity
>    to ByteStrings I'll have to materialize & run inside this Flow's body some
>    RunnableFlow. Was it designed like that? Is it ok to do it this ways? Or I
>    have to describe all flows, then combine then in some logical stream and
>    only then call run once in order to start all those transformations ?
>
>
>
> Thank you in advance,
> Appriciate any help/info regarding this topic.
>
> Br,
> Pavel
>
>
>
>
>  --
> >>>>>>>>>> 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.
>



-- 
Martynas Mickevičius
Typesafe <http://typesafe.com/> – Reactive
<http://www.reactivemanifesto.org/> Apps on the JVM

-- 
>>>>>>>>>>      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