File tree Expand file tree Collapse file tree 4 files changed +29
-17
lines changed
main/kotlin/tools/jackson/module/kotlin
test/kotlin/tools/jackson/module/kotlin/test/github Expand file tree Collapse file tree 4 files changed +29
-17
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ WrongWrong (@k163377)
3131# 2 .18.3 (not yet released)
3232
3333WrongWrong (@k163377 )
34+ * #908 : Additional fixes related to #904.
3435* #904 : Fixed an error when serializing a `value class` that wraps a `Map`
3536* #900 : Fixed an issue where some tests were not running
3637
Original file line number Diff line number Diff line change @@ -36,7 +36,9 @@ Co-maintainers:
3636
37372.18 .3 (not yet released )
3838
39- #904 : An error that occurred when serializing a `value class` that wraps a `Map`(#873) has been fixed.
39+ #904 : Fixed a problem where context was not being propagated properly when serializing an unboxed value of `value class`
40+ or a value retrieved with `JsonValue `.
41+ This fixes a problem where an error would occur when serializing a `value class ` that wraps a `Map `(#873 ).
4042
41432.18 .2 (27 - Nov - 2024 )
42442.18 .1 (28 - Oct - 2024 )
Original file line number Diff line number Diff line change @@ -50,12 +50,6 @@ private fun Class<*>.getStaticJsonValueGetter(): Method? = this.declaredMethods.
5050object ValueClassUnboxSerializer : StdSerializer<Any>(Any : :class.java) {
5151 override fun serialize (value : Any , gen : JsonGenerator , ctxt : SerializationContext ) {
5252 val unboxed = value::class .java.getMethod(" unbox-impl" ).invoke(value)
53-
54- if (unboxed == null ) {
55- ctxt.findNullValueSerializer(null ).serialize(null , gen, ctxt)
56- return
57- }
58-
5953 ctxt.writeValue(gen, unboxed)
6054 }
6155}
@@ -70,9 +64,7 @@ internal sealed class ValueClassSerializer<T : Any>(t: Class<T>) : StdSerializer
7064 val unboxed = unboxMethod.invoke(value)
7165 // As shown in the processing of the factory function, jsonValueGetter is always a static method.
7266 val jsonValue: Any? = staticJsonValueGetter.invoke(null , unboxed)
73- jsonValue
74- ?.let { ctxt.findValueSerializer(it::class .java).serialize(it, gen, ctxt) }
75- ? : ctxt.findNullValueSerializer(null ).serialize(null , gen, ctxt)
67+ ctxt.writeValue(gen, jsonValue)
7668 }
7769 }
7870
Original file line number Diff line number Diff line change @@ -2,9 +2,20 @@ package tools.jackson.module.kotlin.test.github
22
33import tools.jackson.module.kotlin.defaultMapper
44import tools.jackson.module.kotlin.readValue
5+ import com.fasterxml.jackson.annotation.JsonValue
56import kotlin.test.Test
67
78class GitHub873 {
9+ @JvmInline
10+ value class Person (
11+ val properties : Map <String , Any >,
12+ )
13+
14+ data class TimestampedPerson (
15+ val timestamp : Long ,
16+ val person : Person ,
17+ )
18+
819 @Test
920 fun `should serialize value class` () {
1021
@@ -34,12 +45,18 @@ class GitHub873 {
3445 }
3546
3647 @JvmInline
37- value class Person (
38- val properties : Map <String , Any >,
39- )
48+ value class MapAsJsonValue (val value : String ) {
49+ @get:JsonValue
50+ val jsonValue get() = mapOf (" key" to value)
51+ }
4052
41- data class TimestampedPerson (
42- val timestamp : Long ,
43- val person : Person ,
44- )
53+ data class JsonValueWrapper (val value : MapAsJsonValue )
54+
55+ @Test
56+ fun `JsonValue is serialized in the same way` () {
57+ val data = JsonValueWrapper (MapAsJsonValue (" value" ))
58+ val json = defaultMapper.writeValueAsString(data)
59+
60+ assert (""" {"value":{"key":"value"}}""" == json)
61+ }
4562}
You can’t perform that action at this time.
0 commit comments