implicit def CurrencyCodecJson: CodecJson[Currency] = casecodec1(Currency.apply, Currency.unapply)("currencyCode")
case class Person(@BeanProperty firstName: String, @BeanProperty lastName: String)
object Person{
implicit def PersonCodecJson: CodecJson[Person] = casecodec2(Person.apply, Person.unapply)("firstName","lastName")
implicit def json2Person(json: Json): Person=
json.as[Person].toOption match {
case Some(p) => p
case None => null
}
}
class ArgonautConverter extends AbstractHttpMessageConverter[Json](new MediaType("application", "json", Charset.forName("UTF-8")), new MediaType("application", "*+json", Charset.forName("UTF-8"))) {
val supportedClasses = List(
classOf[Currency],
classOf[Person]
)
def supports(clazz: Class[_]): Boolean = {
supportedClasses.contains(clazz)
}
def writeInternal(t: Json, outputMessage: HttpOutputMessage) = {
outputMessage getBody() write t.spaces2.getBytes("UTF-8")
}
def readInternal(clazz: Class[_ <: Json], inputMessage: HttpInputMessage): Json = {
Source.fromInputStream(inputMessage.getBody).getLines().mkString.parse.toEither match {
case Left(message) => sys.error(message)
case Right(json) => json