Skip to content
This repository has been archived by the owner on Jan 27, 2019. It is now read-only.

Adding Salat built in JSON support #14

Open
mzafer opened this issue Jun 24, 2012 · 8 comments
Open

Adding Salat built in JSON support #14

mzafer opened this issue Jun 24, 2012 · 8 comments

Comments

@mzafer
Copy link

mzafer commented Jun 24, 2012

With the recent release of salat version 0.0.8 snapshot, Salat has very good support for JSON <-> Model. But since salat uses lift json and not jerkson the Play built in JSON BodyParser will not work. It will be nice for this plugin to also support JSON -> Model conversion from the HTTP request using Salats implementation.

@leon
Copy link
Owner

leon commented Jun 26, 2012

I'll have a try at implementing a custom Body Parser for lift json.

@adouba
Copy link

adouba commented Aug 17, 2012

+1

@jcranky
Copy link

jcranky commented Sep 21, 2012

Adding to that, depending on lift-json (and specially on the snapshot...) is causing problems sometimes, since it is not available in mvn central...

@hiltym
Copy link

hiltym commented Nov 13, 2012

@jcranky: If you use the milestone release of lift-json, this is not an issue.

@hiltym
Copy link

hiltym commented Dec 20, 2012

Proposal for such a body parser, based on this here:
http://blog.xebia.com/2012/07/22/play-body-parsing-with-jerkson/

import net.liftweb.json._

import play.api.Play
import play.api.mvc._
import BodyParsers.parse.DEFAULT_MAX_TEXT_LENGTH
import play.api.libs.iteratee._
import play.api.libs.iteratee.Input._

/**
 * Can be used to parse an input as a certain type
 */
class JsonObjectParser[A : Manifest] extends BodyParser[A] { 
  implicit val formats = net.liftweb.json.DefaultFormats
  def apply(request: RequestHeader): Iteratee[Array[Byte], Either[Result, A]] = {
    Traversable.takeUpTo[Array[Byte]](DEFAULT_MAX_TEXT_LENGTH).apply(Iteratee.consume[Array[Byte]]().map { bytes =>
    scala.util.control.Exception.allCatch[A].either {
      val json = parse(new String(bytes, request.charset.getOrElse("utf-8")))
      json.extract[A]
    }.left.map { e =>
      (Play.maybeApplication.map(_.global.onBadRequest(request, "Invalid Json")).getOrElse(Results.BadRequest), bytes)
    }
  }).flatMap(Iteratee.eofOrElse(Results.EntityTooLarge))
    .flatMap {
      case Left(b) => Done(Left(b), Empty)
      case Right(it) => it.flatMap {
        case Left((r, in)) => Done(Left(r), El(in))
        case Right(a) => Done(Right(a), Empty)
      }
    }
  }
}

@Gottexbrokerssa
Copy link

salat uses json4s, not lift-json anymore !

@kailuowang
Copy link

+1

1 similar comment
@ee99ee
Copy link

ee99ee commented Apr 17, 2013

+1

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

No branches or pull requests

8 participants