From cff4e970a6ec389994d4f08d4ae15815d6490aae Mon Sep 17 00:00:00 2001 From: Donovan Levinson Date: Sat, 11 May 2024 09:24:18 -1000 Subject: [PATCH] Add tests for field ordering --- .../test/scala/play/api/libs/json/JsonSharedSpec.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/play-json/shared/src/test/scala/play/api/libs/json/JsonSharedSpec.scala b/play-json/shared/src/test/scala/play/api/libs/json/JsonSharedSpec.scala index bdcba06a..163e63c3 100644 --- a/play-json/shared/src/test/scala/play/api/libs/json/JsonSharedSpec.scala +++ b/play-json/shared/src/test/scala/play/api/libs/json/JsonSharedSpec.scala @@ -28,6 +28,10 @@ class JsonSharedSpec extends AnyWordSpec with Matchers with org.scalatestplus.sc (__ \ Symbol("models")).format[Map[String, String]] )(Car.apply, c => (c.id, c.models)) + case class Model(m1: Option[String], c: String, z: Option[Double], a: Int, b: Option[String]) + + implicit val modelFormat: Format[Model] = Json.format[Model] + def json[T](f: JsonFacade => T) = forAll(Gen.oneOf(Json, Json.configured, Json.using[Json.MacroOptions]))(f) "JSON" should { @@ -443,6 +447,12 @@ class JsonSharedSpec extends AnyWordSpec with Matchers with org.scalatestplus.sc test.toString.mustEqual(js.parse(req).toString) // ).ignoreSpace } + + "preserve field ordering" in { + val model = Model(Some("foo"), "bar", Some(1.2), 42, Some("baz")) + val serialized = Json.stringify(Json.toJson(model)) + serialized.mustEqual("""{"m1":"foo","c":"bar","z":1.2,"a":42,"b":"baz"}""") + } } // ---