Skip to content

Commit

Permalink
change JSON ast to json4s
Browse files Browse the repository at this point in the history
  • Loading branch information
hmil committed Aug 6, 2016
1 parent 8dd0ac1 commit 4677be3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ see something that is missing.
## Changelog

**v2.0.0**

- Removed JSON AST for json4s


**v1.0.0
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ lazy val scalaHttp = crossProject.in(file("."))
pomIncludeRepository := { _ => false },

libraryDependencies += "com.lihaoyi" %%% "utest" % "0.4.3",
libraryDependencies += "org.json4s" %%% "json4s-ast" % "4.0.0-M1",

testFrameworks += new TestFramework("utest.runner.Framework")
)
Expand Down
8 changes: 2 additions & 6 deletions shared/src/main/scala/fr/hmil/roshttp/body/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ package fr.hmil.roshttp.body

import java.nio.ByteBuffer

import fr.hmil.roshttp.body.JSONBody._
import org.json4s.ast.safe.JValue

object Implicits {
implicit def stringToJSONString(value: String): JSONString = new JSONString(value)
implicit def intToJSONNumber(value: Int): JSONNumber = new JSONNumber(value)
implicit def floatToJSONNumber(value: Float): JSONNumber = new JSONNumber(value)
implicit def doubleToJSONNumber(value: Double): JSONNumber = new JSONNumber(value)
implicit def JSONObjectToJSONBody(obj: JSONObject): JSONBody = JSONBody(obj)
implicit def JValueToJSONBody(obj: JValue): JSONBody = JSONBody(obj)
implicit def byteBufferToStreamBody(buff: ByteBuffer): StreamBody = StreamBody(buff)
}
49 changes: 21 additions & 28 deletions shared/src/main/scala/fr/hmil/roshttp/body/JSONBody.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,40 @@ package fr.hmil.roshttp.body

import java.nio.ByteBuffer

import fr.hmil.roshttp.body.JSONBody.JSONValue
import org.json4s.ast.safe._

/** Allows to send arbitrarily complex JSON data.
*
* @param value The JSON value to send.
*/
class JSONBody private(value: JSONValue) extends BodyPart {
class JSONBody private(value: JValue) extends BodyPart {
override def contentType: String = s"application/json; charset=utf-8"

override def content: ByteBuffer = ByteBuffer.wrap(value.toString.getBytes("utf-8"))
override def content: ByteBuffer = {
ByteBuffer.wrap(JSONBody.serialize(value).getBytes("utf-8"))
}
}

object JSONBody {
trait JSONValue {
def toString: String
}
def apply(value: JValue): JSONBody = new JSONBody(value)

class JSONNumber(value: Number) extends JSONValue {
override def toString: String = value.toString
}

class JSONString(value: String) extends JSONValue {
override def toString: String = "\"" + escapeJS(value) + "\""
}

class JSONObject(values: Map[String, JSONValue]) extends JSONValue {
override def toString: String = {
// Homebrew serialization to be removed when json4s supports scalajs
// https://github.com/json4s/json4s/issues/256
private def serialize(v: JValue): String = v match {
case JNull => "null"
case JString(s) => "\"" + escapeJS(s) + "\""
case JNumber(num) => num.toString
case JBoolean(value) => value.toString
case o:JObject =>
"{" +
values.map({case (name, part) =>
"\"" + escapeJS(name) + "\"" +
":" + part
o.value.map({case (key, value) =>
"\"" + escapeJS(key) + "\"" +
":" + serialize(value)
}).mkString(",") +
"}"
}
"}"
case a:JArray =>
"[" + a.value.map(v => serialize(v)).mkString(",") + "]"
}

object JSONObject {
def apply(values: (String, JSONValue)*): JSONObject = new JSONObject(Map(values: _*))
}

def apply(value: JSONValue): JSONBody = new JSONBody(value)

// String escapement taken from scala-js
private final val EscapeJSChars = "\\a\\b\\t\\n\\v\\f\\r\\\"\\\\"
Expand Down Expand Up @@ -110,4 +103,4 @@ object JSONBody {
}
}
}
}
}
28 changes: 14 additions & 14 deletions shared/src/test/scala/fr/hmil/roshttp/client/HttpRequestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package fr.hmil.roshttp.client
import java.nio.ByteBuffer

import fr.hmil.roshttp._
import fr.hmil.roshttp.body.JSONBody._
import fr.hmil.roshttp.body.Implicits._
import fr.hmil.roshttp.body._
import org.json4s.ast.safe._
import utest._

import scala.concurrent.ExecutionContext.Implicits.global
Expand Down Expand Up @@ -170,14 +170,14 @@ object HttpRequestSpec extends TestSuite {
HttpRequest(s"$SERVER_URL/body")
.post(MultiPartBody(
"name" -> PlainTextBody("John"),
"skills" -> JSONObject(
"programming" -> JSONObject(
"C" -> 3,
"PHP" -> 1,
"Scala" -> 5
),
"design" -> 2
),
"skills" -> JObject(Map(
"programming" -> JObject(Map(
"C" -> JNumber(3),
"PHP" -> JNumber(1),
"Scala" -> JNumber(5)
)),
"design" -> JNumber(2)
)),
"picture" -> StreamBody(ByteBuffer.wrap(IMAGE_BYTES), "image/jpeg")
))
}
Expand Down Expand Up @@ -524,11 +524,11 @@ object HttpRequestSpec extends TestSuite {
}

"can be POSTed as json" - {
val part = JSONObject(
"foo" -> 42,
"engine" -> "Heizölrückstoßabdämpfung",
"\"quoted'" -> "Has \" quotes"
)
val part = JObject(Map(
"foo" -> JNumber(42),
"engine" -> JString("Heizölrückstoßabdämpfung"),
"\"quoted'" -> JString("Has \" quotes")
))

HttpRequest(s"$SERVER_URL/body")
.post(part)
Expand Down

0 comments on commit 4677be3

Please sign in to comment.