Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better JSON encoding support #151

Merged
merged 1 commit into from Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,6 +5,9 @@
package play.api.libs.ws

/**
* Provides implicit for converting a response to JsValue.
*
* See https://github.com/playframework/play-json for details of Play-JSON.
*/
trait JsonBodyReadables {

Expand All @@ -18,7 +21,20 @@ trait JsonBodyReadables {
* }}}
*/
implicit val readableAsJson: BodyReadable[JsValue] = BodyReadable { response =>
Json.parse(response.body)
val body = response.bodyAsBytes
Json.parse(body.decodeString(detectEncoding(body)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use Json.parse(response.body.toArray[Byte])? It should detect the encoding for you.

}

// Leverage Jackson's RFC-4627 / 7159 JSON encoding support.
private[ws] def detectEncoding(in: akka.util.ByteString): String = {
// https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/json/ByteSourceJsonBootstrapper.java
// Also see https://stackoverflow.com/a/38036753 if this is insufficient
import com.fasterxml.jackson.core.io.IOContext
import com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper
import com.fasterxml.jackson.core.util.BufferRecycler
val ctx = new IOContext(new BufferRecycler, null, false)
val strapper = new ByteSourceJsonBootstrapper(ctx, in.toArray, 0, 4)
strapper.detectEncoding.getJavaName
}
}

Expand Down
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2009-2017 Lightbend Inc. <https://www.lightbend.com>
*/

package play.api.libs.ws

import akka.util.ByteString
import org.specs2.matcher.MustMatchers
import org.specs2.mutable.Specification

class JsonBodyReadablesSpec extends Specification with MustMatchers {

"decode" should {

"read an encoding of UTF-32BE" in {
// 00 00 00 xx - it's UTF-32BE
val readables = new JsonBodyReadables() {}
val encoding = readables.detectEncoding(ByteString.fromArray(Array[Byte](0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20)))
encoding must beEqualTo("UTF-32BE")
}

"read an encoding of UTF-32LE" in {
// xx 00 00 00 - it's UTF-32LE
val readables = new JsonBodyReadables() {}
val encoding = readables.detectEncoding(ByteString.fromArray(Array[Byte](0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20)))
encoding must beEqualTo("UTF-32LE")
}

"read an encoding of UTF-16BE" in {
// 00 xx 00 xx - it's UTF-16BE
val readables = new JsonBodyReadables() {}
val encoding = readables.detectEncoding(ByteString.fromArray(Array[Byte](0x00, 0x20, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20)))
encoding must beEqualTo("UTF-16BE")
}

"read an encoding of UTF-16LE" in {
// xx 00 xx 00 - it's UTF-16LE
val readables = new JsonBodyReadables() {}
val encoding = readables.detectEncoding(ByteString.fromArray(Array[Byte](0x00, 0x20, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20)))
encoding must beEqualTo("UTF-16BE")
}

"read an encoding of UTF-8" in {
// xx xx xx xx - it's UTF-8
val readables = new JsonBodyReadables() {}
val encoding = readables.detectEncoding(ByteString.fromArray(Array[Byte](0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x20)))
encoding must beEqualTo("UTF-8")
}
}

}