Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
// For Scala, we want to use the declared order of the fields in the class
override def findSerializationSortAlphabetically(ann: Annotated): java.lang.Boolean = {
ann match {
case ac: AnnotatedClass if isMaybeScalaBeanType(ac.getAnnotated) =>
case ac: AnnotatedClass if
ScalaAnnotationIntrospectorModule.isCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder &&
isMaybeScalaBeanType(ac.getAnnotated) =>
val annotation = _findAnnotation(ac, classOf[JsonPropertyOrder])
if (annotation != null) {
// delegate to JacksonAnnotationIntrospector
Expand Down Expand Up @@ -468,7 +470,27 @@ trait ScalaAnnotationIntrospectorModule extends JacksonModule {
def shouldSupportScala3Classes(): Boolean = _shouldSupportScala3Classes
}

object ScalaAnnotationIntrospectorModule extends ScalaAnnotationIntrospectorModule
object ScalaAnnotationIntrospectorModule extends ScalaAnnotationIntrospectorModule {
private var caseClassDefaultOrderBasedOnDeclaredParamOrder = true

/**
* @return Whether to default the serialization order of Case Class params to the defined order in the class.
* This should be set to false if you want to enable <code>MapperFeature.SORT_PROPERTIES_ALPHABETICALLY</code>.
* This is not needed in Jackson 3.
* @since 2.20.1
*/
def isCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder: Boolean =
caseClassDefaultOrderBasedOnDeclaredParamOrder

/**
* @param flag Whether to default the serialization order of Case Class params to the defined order in the class.
* This should be set to false if you want to enable <code>MapperFeature.SORT_PROPERTIES_ALPHABETICALLY</code>.
* This is not needed in Jackson 3.
* @since 2.20.1
*/
def setCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder(flag: Boolean): Unit =
this.caseClassDefaultOrderBasedOnDeclaredParamOrder = flag
}

private case class WrappedCreatorProperty(creatorProperty: CreatorProperty, refHolder: ClassHolder)
extends CreatorProperty(creatorProperty, creatorProperty.getFullName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty.Access
import com.fasterxml.jackson.annotation._
import com.fasterxml.jackson.databind.{MapperFeature, ObjectMapper, PropertyNamingStrategies}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.introspect.ScalaAnnotationIntrospectorModule

import scala.beans.BeanProperty

Expand Down Expand Up @@ -230,12 +231,16 @@ class CaseClassSerializerTest extends SerializerTest {
}

// https://github.com/FasterXML/jackson-module-scala/issues/772
it should "sort properties of the case class" ignore {
it should "sort properties of the case class" in {
val mapper = newBuilder
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.disable(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
.build()
serialize(ClassWithUnorderedFields(), mapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
ScalaAnnotationIntrospectorModule.setCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder(false)
try {
serialize(ClassWithUnorderedFields(), mapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
} finally {
ScalaAnnotationIntrospectorModule.setCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder(true)
}
}

it should "respect JsonPropertyOrder" in {
Expand Down