Skip to content

Commit 2ad40cb

Browse files
committed
respect JsonPropertyOrder
1 parent b63fddb commit 2ad40cb

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/main/scala/tools/jackson/module/scala/introspect/ScalaAnnotationIntrospectorModule.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package tools.jackson.module.scala.introspect
22

3-
import com.fasterxml.jackson.annotation.JsonCreator
3+
import com.fasterxml.jackson.annotation.{JsonCreator, JsonPropertyOrder}
44
import tools.jackson.core.Version
55
import tools.jackson.databind.JacksonModule.SetupContext
66
import tools.jackson.databind.`type`.{CollectionLikeType, MapLikeType, ReferenceType, SimpleType}
@@ -42,7 +42,13 @@ class ScalaAnnotationIntrospectorInstance(scalaAnnotationIntrospectorModule: Sca
4242
override def findSerializationSortAlphabetically(config: MapperConfig[_], ann: Annotated): java.lang.Boolean = {
4343
ann match {
4444
case ac: AnnotatedClass if scalaAnnotationIntrospectorModule.isMaybeScalaBeanType(ac.getAnnotated) =>
45-
!config.isEnabled(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
45+
val annotation = _findAnnotation(ac, classOf[JsonPropertyOrder])
46+
if (annotation != null) {
47+
// delegate to JacksonAnnotationIntrospector
48+
None.orNull
49+
} else {
50+
!config.isEnabled(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
51+
}
4652
case _ => None.orNull
4753
}
4854
}

src/test/scala/tools/jackson/module/scala/ser/CaseClassSerializerTest.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ object CaseClassSerializerTest {
6969
case class ClassWithUnorderedFields(f3: Int = 3, f2: Int = 2, f0: Int = 0, f1: Int = 1)
7070
@JsonPropertyOrder(Array("f0", "f1", "f2", "f3"))
7171
case class AnnotatedClassWithUnorderedFields(f3: Int = 3, f2: Int = 2, f0: Int = 0, f1: Int = 1)
72+
@JsonPropertyOrder(alphabetic = true)
73+
case class AnnotatedClassWithUnorderedFields2(f3: Int = 3, f2: Int = 2, f0: Int = 0, f1: Int = 1)
7274
}
7375

7476
class CaseClassSerializerTest extends SerializerTest {
@@ -244,4 +246,8 @@ class CaseClassSerializerTest extends SerializerTest {
244246
it should "respect JsonPropertyOrder" in {
245247
serialize(AnnotatedClassWithUnorderedFields(), newMapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
246248
}
249+
250+
it should "respect JsonPropertyOrder alphabetic=true" in {
251+
serialize(AnnotatedClassWithUnorderedFields(), newMapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
252+
}
247253
}

0 commit comments

Comments
 (0)