-
Notifications
You must be signed in to change notification settings - Fork 4
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 #47 from ProjectMapK/improve-value-class-ser-support
Improve `value class` serialize support
- Loading branch information
Showing
12 changed files
with
434 additions
and
104 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
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
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/fasterxml/jackson/module/kotlin/ser/ValueClassBoxConverter.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,17 @@ | ||
package com.fasterxml.jackson.module.kotlin.ser | ||
|
||
import com.fasterxml.jackson.databind.util.StdConverter | ||
|
||
// S is nullable because value corresponds to a nullable value class | ||
// @see KotlinNamesAnnotationIntrospector.findNullSerializer | ||
internal class ValueClassBoxConverter<S : Any?, D : Any>( | ||
unboxedClass: Class<S>, | ||
valueClass: Class<D> | ||
) : StdConverter<S, D>() { | ||
private val boxMethod = valueClass.getDeclaredMethod("box-impl", unboxedClass).apply { | ||
if (!this.isAccessible) this.isAccessible = true | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun convert(value: S): D = boxMethod.invoke(null, value) as D | ||
} |
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
...m/fasterxml/jackson/module/kotlin/_integration/ser/value_class/serializer/ValueClasses.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._integration.ser.value_class.serializer | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer | ||
|
||
@JvmInline | ||
value class Primitive(val v: Int) { | ||
class Serializer : StdSerializer<Primitive>(Primitive::class.java) { | ||
override fun serialize(value: Primitive, gen: JsonGenerator, provider: SerializerProvider) { | ||
gen.writeNumber(value.v) | ||
} | ||
} | ||
} | ||
|
||
@JvmInline | ||
value class NonNullObject(val v: String) { | ||
class Serializer : StdSerializer<NonNullObject>(NonNullObject::class.java) { | ||
override fun serialize(value: NonNullObject, gen: JsonGenerator, provider: SerializerProvider) { | ||
gen.writeString(value.v) | ||
} | ||
} | ||
} | ||
|
||
@JvmInline | ||
value class NullableObject(val v: String?) { | ||
class Serializer : StdSerializer<NullableObject>(NullableObject::class.java) { | ||
override fun serialize(value: NullableObject, gen: JsonGenerator, provider: SerializerProvider) { | ||
value.v?.let { gen.writeString(it) } ?: gen.writeNull() | ||
} | ||
} | ||
} |
Oops, something went wrong.