0

I am having problems when parsing special characters (e.g., è, é, and ®) JSONs in Play for Scala (version 2.3.x). Since everything in Play is UTF-8 encoded it should not be a problem with the aforementioned examples (è, é, and ®). I should mention that I am using IntelliJ and sbt for my development.

I have verified the following:

  • test file is UTF-8 encoded
  • javac using utf-8 (javacOptions ++= Seq("-encoding", "UTF-8"))
  • setting utf-8 on header (application/json; charset=utf-8)
  • parse.tolerantJson(request) is the culprit of the error (see code below)

Test file:

{"id":"295b1590-220a-11e4-b641-90e08f355b68","blah":"è é ®"}

Controller:

      def putGuid(streamId: String, guid: UUID) = Action.async(parseToEventContent) {
    implicit request =>
      render.async {
        case mimeType@(Accepts.Json()) =>
          val id = msgId.next()
          (request.body match {
            case JsonEventContent(j) =>
              eventActor ? SendEventMessages.SendJsonEvent(id, streamId, Some(guid), remoteAddress(request), j)
            case ... some other cases...
          }) map {
            case SendEventMessages.Done(`id`) => NoContent
            case ... more error cases.. blah blah ...
            case SendEventMessages.Unknown(`id`) =>
              Logger.info("Message ID: {}. Received an unknown from underlying actors", id.asInstanceOf[AnyRef])
              ServiceUnavailableRetryAfter()
          } recover exceptionErrorHandler
      } }

Where the BodyParser is:

  trait EventContent {}
  case class JsonEventContent(data: JsValue) extends EventContent

  def parseToEventContent: BodyParser[EventContent] = BodyParser("eventContent") {
    request =>
      import play.api.libs.iteratee.Execution.Implicits.trampoline
      request.contentType match {
        case Some(MimeTypes.JSON) => parse.tolerantJson(request).map(_.right.map(JsonEventContent(_)))
        case _ => Done(Left(UnsupportedMediaType), Empty)
      }
  }

Edit #1

using something like the below in my test gives me the 400 error for "èé®" but 204 for "abc" (my source files are utf-8 and javac is set to use uff-8 in build.sbt):

val eventC = Json.parse(new String(s"""{"id":"295b1590-220a-11e4-b641-90e08f355b68","blah":"è é ®"}""".getBytes("UTF-8"), "UTF-8"))
val eventCJson = eventC.toString().getBytes("UTF-8")

"return 204 when entity has è, é, ® in json event" in new WithApplication(MyFakeApplication()){
  println(s"json=${eventC}")
  val result = assertPutRequest(s"/streams/com.gilt.clickstream/events/295b1590-220a-11e4-b641-90e08f355b68", 204, event = eventCJson, "application/json", "application/json; charset=utf-8")
  contentType(result) must be equalTo None
  contentAsBytes(result) must be equalTo Array()
}

def assertPutRequest(url: String, httpStatus: Int, event: Array[Byte], acceptHeader: String = "application/json", contentTypeHeader: String = "application/json; charset=utf-8")(implicit app: Application) = {
  Akka.system.actorOf(MockEventActor.props, "event")

  val result = route(
    FakeRequest(PUT, url)
      .withHeaders(
        "Accept" -> acceptHeader,
        "Content-Type" -> contentTypeHeader)
      .withRawBody(event)
  )(new Writeable({ j: AnyContentAsRaw => j.raw.asBytes().get}, None)).get

  status(result) must be equalTo httpStatus
  result
}
4
  • Which version of Play?
    – cchantep
    Apr 13, 2015 at 18:24
  • Try your data with the existing integration tests in Play, I do successfully get the following JsObject: {"id":"295b1590-220a-11e4-b641-90e08f355b68","blah":"è é ®"}
    – cchantep
    Apr 14, 2015 at 11:43
  • Odd, yes I can get that to work too. However, using something like the above Edit #1 in my test gives me the 400 error for "èé®" but 204 for "abc"
    – mhansen
    Apr 15, 2015 at 13:51
  • JDK 1.6 was the culprit, upgrading to 1.7 fixed the issue.
    – mhansen
    May 1, 2015 at 23:29

1 Answer 1

0

The solution to this problem is to upgrade to JDK 1.7. I was using JDK 1.6 and that caused the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.