-
-
Notifications
You must be signed in to change notification settings - Fork 140
Closed
Description
Play JSON Version (2.5.x / etc)
Affected versions:
"com.typesafe.play" %% "play-json" % "2.10.5""org.playframework" %% "play-json" % "3.0.3"
API (Scala / Java / Neither / Both)
Scala
Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)
MacOS 14.3.1
JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)
java version "17.0.10" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 17.0.10+11-LTS-240)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.10+11-LTS-240, mixed mode, sharing)
Expected Behavior
Expected JSON ordering to be preserved when serializing model class.
Actual Behavior
JSON field ordering is not preserved when compiling using Scala 3
Reproducible Test Case
I created the following project to demonstrate the issue: https://github.com/levinson/play-json-scala3-ordering-bug
Posting the source code here as well for reference:
build.sbt:
scalaVersion := "2.13.14"
// TODO: Test fails when using Scala 3
//scalaVersion := "3.3.3"
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-json" % "2.10.5",
//"org.playframework" %% "play-json" % "3.0.3", // same behavior
"org.scalatest" %% "scalatest" % "3.2.18" % Test
)JsonSerializersTest.scala
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import play.api.libs.json._
case class Model(metadata: Option[String], region: String)
object JsonSerializers {
implicit val modelFmt: Format[Model] = Json.format[Model]
}
class JsonSerializersTest extends AnyWordSpec with Matchers {
import JsonSerializers._
"JsonSerializers" should {
"preserve ordering" in {
val model = Model(Some("metadata"), "region")
val serialized = Json.stringify(Json.toJson(model))
println("serialized is " + serialized)
serialized shouldBe """{"metadata":"metadata","region":"region"}"""
}
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels