From 0131537d5635f0af53314d87eb6ed1fb42ae0c4b Mon Sep 17 00:00:00 2001 From: kenji yoshida <6b656e6a69@gmail.com> Date: Tue, 7 Aug 2018 19:07:58 +0900 Subject: [PATCH] Add support for Scala 2.13.0-M4 (#263) --- .travis.yml | 2 ++ build.sbt | 2 +- .../json/ProductFormatsInstances.scala.template | 2 +- src/main/scala/spray/json/CollectionFormats.scala | 4 ++-- src/main/scala/spray/json/JsValue.scala | 2 +- .../scala/spray/json/AdditionalFormatsSpec.scala | 6 ++++-- .../scala/spray/json/CollectionFormatsSpec.scala | 6 +++--- src/test/scala/spray/json/JsonParserSpec.scala | 13 +++++++++---- src/test/scala/spray/json/ProductFormatsSpec.scala | 6 +++--- 9 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index f7363605..6fc769e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ scala: - 2.10.7 - 2.11.12 - 2.12.6 + - 2.13.0-M4 + script: - sbt "++ ${TRAVIS_SCALA_VERSION}!" test mimaReportBinaryIssues jdk: diff --git a/build.sbt b/build.sbt index 30dc3d29..23bdd9e6 100644 --- a/build.sbt +++ b/build.sbt @@ -67,7 +67,7 @@ mimaBinaryIssueFilters := Seq( // publishing /////////////// -crossScalaVersions := Seq("2.12.6", "2.10.7", "2.11.12") +crossScalaVersions := Seq("2.12.6", "2.10.7", "2.11.12", "2.13.0-M4") publishMavenStyle := true diff --git a/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template b/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template index 401ac83c..fa0d875f 100644 --- a/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template +++ b/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template @@ -31,7 +31,7 @@ trait ProductFormatsInstances { self: ProductFormats with StandardFormats => fields.sizeHint(1 * 2) [#fields ++= productElement##2Field[P1](fieldName1, p, 0)# ] - JsObject(fields: _*) + JsObject(fields.toSeq: _*) } def read(value: JsValue) = { [#val p1V = fromField[P1](value, fieldName1)# diff --git a/src/main/scala/spray/json/CollectionFormats.scala b/src/main/scala/spray/json/CollectionFormats.scala index 9a3e8fda..ef94297b 100644 --- a/src/main/scala/spray/json/CollectionFormats.scala +++ b/src/main/scala/spray/json/CollectionFormats.scala @@ -27,7 +27,7 @@ trait CollectionFormats { implicit def listFormat[T :JsonFormat] = new RootJsonFormat[List[T]] { def write(list: List[T]) = JsArray(list.map(_.toJson).toVector) def read(value: JsValue): List[T] = value match { - case JsArray(elements) => elements.map(_.convertTo[T])(collection.breakOut) + case JsArray(elements) => elements.toIterator.map(_.convertTo[T]).toList case x => deserializationError("Expected List as JsArray, but got " + x) } } @@ -59,7 +59,7 @@ trait CollectionFormats { def read(value: JsValue) = value match { case x: JsObject => x.fields.map { field => (JsString(field._1).convertTo[K], field._2.convertTo[V]) - } (collection.breakOut) + } case x => deserializationError("Expected Map as JsObject, but got " + x) } } diff --git a/src/main/scala/spray/json/JsValue.scala b/src/main/scala/spray/json/JsValue.scala index 08a673b4..7cd8cd83 100644 --- a/src/main/scala/spray/json/JsValue.scala +++ b/src/main/scala/spray/json/JsValue.scala @@ -50,7 +50,7 @@ sealed abstract class JsValue { */ case class JsObject(fields: Map[String, JsValue]) extends JsValue { override def asJsObject(errorMsg: String) = this - def getFields(fieldNames: String*): immutable.Seq[JsValue] = fieldNames.flatMap(fields.get)(collection.breakOut) + def getFields(fieldNames: String*): immutable.Seq[JsValue] = fieldNames.toIterator.flatMap(fields.get).toList } object JsObject { val empty = JsObject(Map.empty[String, JsValue]) diff --git a/src/test/scala/spray/json/AdditionalFormatsSpec.scala b/src/test/scala/spray/json/AdditionalFormatsSpec.scala index eafceb21..01127e62 100644 --- a/src/test/scala/spray/json/AdditionalFormatsSpec.scala +++ b/src/test/scala/spray/json/AdditionalFormatsSpec.scala @@ -64,8 +64,10 @@ class AdditionalFormatsSpec extends Specification { "The lazyFormat wrapper" should { "enable recursive format definitions" in { import FooProtocol._ - Foo(1, "a", Some(Foo(2, "b", Some(Foo(3, "c") :: Nil)) :: Foo(4, "d") :: Nil)).toJson.toString mustEqual - """{"id":1,"name":"a","foos":[{"id":2,"name":"b","foos":[{"id":3,"name":"c"}]},{"id":4,"name":"d"}]}""" + val json = Foo(1, "a", Some(Foo(2, "b", Some(Foo(3, "c") :: Nil)) :: Foo(4, "d") :: Nil)).toJson + + json mustEqual + """{"id":1,"name":"a","foos":[{"id":2,"name":"b","foos":[{"id":3,"name":"c"}]},{"id":4,"name":"d"}]}""".parseJson } } } \ No newline at end of file diff --git a/src/test/scala/spray/json/CollectionFormatsSpec.scala b/src/test/scala/spray/json/CollectionFormatsSpec.scala index 3d953bb5..9d6970bf 100644 --- a/src/test/scala/spray/json/CollectionFormatsSpec.scala +++ b/src/test/scala/spray/json/CollectionFormatsSpec.scala @@ -59,12 +59,12 @@ class CollectionFormatsSpec extends Specification with DefaultJsonProtocol { "The immutableSetFormat" should { val set = Set(1, 2, 3) - val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3)) + val numbers = Set(JsNumber(1), JsNumber(2), JsNumber(3)) "convert a Set[Int] to a JsArray of JsNumbers" in { - set.toJson mustEqual json + set.toJson.asInstanceOf[JsArray].elements.toSet mustEqual numbers } "convert a JsArray of JsNumbers to a Set[Int]" in { - json.convertTo[Set[Int]] mustEqual set + JsArray(numbers.toVector).convertTo[Set[Int]] mustEqual set } } diff --git a/src/test/scala/spray/json/JsonParserSpec.scala b/src/test/scala/spray/json/JsonParserSpec.scala index a97f0214..e5645a4b 100644 --- a/src/test/scala/spray/json/JsonParserSpec.scala +++ b/src/test/scala/spray/json/JsonParserSpec.scala @@ -73,11 +73,16 @@ class JsonParserSpec extends Specification { JsonParser(json.prettyPrint.getBytes("UTF-8")) === json } "be reentrant" in { + import scala.concurrent.{Await, Future} + import scala.concurrent.duration._ + import scala.concurrent.ExecutionContext.Implicits.global + val largeJsonSource = scala.io.Source.fromInputStream(getClass.getResourceAsStream("/test.json")).mkString - import scala.collection.parallel.immutable.ParSeq - ParSeq.fill(20)(largeJsonSource).map(JsonParser(_)).toList.map { - _.asInstanceOf[JsObject].fields("questions").asInstanceOf[JsArray].elements.size - } === List.fill(20)(100) + val list = Await.result( + Future.traverse(List.fill(20)(largeJsonSource))(src => Future(JsonParser(src))), + 5.seconds + ) + list.map(_.asInstanceOf[JsObject].fields("questions").asInstanceOf[JsArray].elements.size) === List.fill(20)(100) } "produce proper error messages" in { diff --git a/src/test/scala/spray/json/ProductFormatsSpec.scala b/src/test/scala/spray/json/ProductFormatsSpec.scala index 30582a8f..9eabc50f 100644 --- a/src/test/scala/spray/json/ProductFormatsSpec.scala +++ b/src/test/scala/spray/json/ProductFormatsSpec.scala @@ -198,12 +198,12 @@ class ProductFormatsSpec extends Specification { "A JsonFormat created with `jsonFormat`, for a case class with mangled-name members," should { import TestProtocol1._ - val json = """{"ü$bavf$u56ú$":true,"=><+-*/!@#%^&~?|":1.0,"foo-bar!":42,"-x-":26,"User ID":"Karl"}""" + val json = """{"ü$bavf$u56ú$":true,"=><+-*/!@#%^&~?|":1.0,"foo-bar!":42,"-x-":26,"User ID":"Karl"}""".parseJson "produce the correct JSON" in { - TestMangled(42, "Karl", true, 26, 1.0f).toJson.compactPrint === json + TestMangled(42, "Karl", true, 26, 1.0f).toJson === json } "convert a JsObject to the respective case class instance" in { - json.parseJson.convertTo[TestMangled] === TestMangled(42, "Karl", true, 26, 1.0f) + json.convertTo[TestMangled] === TestMangled(42, "Karl", true, 26, 1.0f) } } }