-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #470 from k163377/github_464_pr3
`unbox` `value class` in `Map key` when serializing
- Loading branch information
Showing
5 changed files
with
134 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeySerializers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.fasterxml.jackson.module.kotlin | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.databind.* | ||
import com.fasterxml.jackson.databind.ser.Serializers | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer | ||
|
||
internal object ValueClassUnboxKeySerializer : StdSerializer<Any>(Any::class.java) { | ||
override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) { | ||
val method = value::class.java.getMethod("unbox-impl") | ||
val unboxed = method.invoke(value) | ||
|
||
if (unboxed == null) { | ||
val javaType = provider.typeFactory.constructType(method.genericReturnType) | ||
provider.findNullKeySerializer(javaType, null).serialize(null, gen, provider) | ||
return | ||
} | ||
|
||
provider.findKeySerializer(unboxed::class.java, null).serialize(unboxed, gen, provider) | ||
} | ||
} | ||
|
||
internal class KotlinKeySerializers : Serializers.Base() { | ||
override fun findSerializer( | ||
config: SerializationConfig, | ||
type: JavaType, | ||
beanDesc: BeanDescription | ||
): JsonSerializer<*>? = when { | ||
type.rawClass.isUnboxableValueClass() -> ValueClassUnboxKeySerializer | ||
else -> null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters