![JAR search and dependency download from the Maven repository](/logo.png)
spray.httpx.SprayJsonSupport.scala Maven / Gradle / Ivy
/*
* Copyright © 2011-2015 the spray project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spray.httpx
import spray.httpx.marshalling.Marshaller
import spray.httpx.unmarshalling.Unmarshaller
import spray.json._
import spray.http._
/**
* A trait providing automatic to and from JSON marshalling/unmarshalling using an in-scope *spray-json* protocol.
* Note that *spray-httpx* does not have an automatic dependency on *spray-json*.
* You'll need to provide the appropriate *spray-json* artifacts yourself.
*/
trait SprayJsonSupport {
implicit def sprayJsonUnmarshallerConverter[T](reader: RootJsonReader[T]) =
sprayJsonUnmarshaller(reader)
implicit def sprayJsonUnmarshaller[T: RootJsonReader] =
Unmarshaller[T](MediaTypes.`application/json`) {
case x: HttpEntity.NonEmpty ⇒
val json = JsonParser(x.asString(defaultCharset = HttpCharsets.`UTF-8`))
jsonReader[T].read(json)
}
implicit def sprayJsonMarshallerConverter[T](writer: RootJsonWriter[T])(implicit printer: JsonPrinter = PrettyPrinter) =
sprayJsonMarshaller[T](writer, printer)
implicit def sprayJsonMarshaller[T](implicit writer: RootJsonWriter[T], printer: JsonPrinter = PrettyPrinter) =
Marshaller.delegate[T, String](ContentTypes.`application/json`) { value ⇒
val json = writer.write(value)
printer(json)
}
}
object SprayJsonSupport extends SprayJsonSupport
© 2015 - 2025 Weber Informatics LLC | Privacy Policy