diff --git a/README.md b/README.md index d8a3ac7..a7368e6 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ see something that is missing. ## Changelog **v2.0.0** - +- Removed JSON AST for json4s **v1.0.0 diff --git a/build.sbt b/build.sbt index 1741d17..503806f 100644 --- a/build.sbt +++ b/build.sbt @@ -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") ) diff --git a/shared/src/main/scala/fr/hmil/roshttp/body/Implicits.scala b/shared/src/main/scala/fr/hmil/roshttp/body/Implicits.scala index 787f542..929b311 100644 --- a/shared/src/main/scala/fr/hmil/roshttp/body/Implicits.scala +++ b/shared/src/main/scala/fr/hmil/roshttp/body/Implicits.scala @@ -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) } diff --git a/shared/src/main/scala/fr/hmil/roshttp/body/JSONBody.scala b/shared/src/main/scala/fr/hmil/roshttp/body/JSONBody.scala index fb8384f..e6228cb 100644 --- a/shared/src/main/scala/fr/hmil/roshttp/body/JSONBody.scala +++ b/shared/src/main/scala/fr/hmil/roshttp/body/JSONBody.scala @@ -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\\\"\\\\" @@ -110,4 +103,4 @@ object JSONBody { } } } -} +} \ No newline at end of file diff --git a/shared/src/test/scala/fr/hmil/roshttp/client/HttpRequestSpec.scala b/shared/src/test/scala/fr/hmil/roshttp/client/HttpRequestSpec.scala index 87e6787..6dc937c 100644 --- a/shared/src/test/scala/fr/hmil/roshttp/client/HttpRequestSpec.scala +++ b/shared/src/test/scala/fr/hmil/roshttp/client/HttpRequestSpec.scala @@ -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 @@ -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") )) } @@ -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)