Skip to content

Commit

Permalink
Merge remote-tracking branch 'FasterXML/2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
k163377 committed Feb 2, 2025
2 parents d5024e6 + 0f765ba commit 4c97452
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ WrongWrong (@k163377)
# 2.18.3 (not yet released)

WrongWrong (@k163377)
* #908: Additional fixes related to #904.
* #904: Fixed an error when serializing a `value class` that wraps a `Map`
* #900: Fixed an issue where some tests were not running

Expand Down
4 changes: 3 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Co-maintainers:

2.18.3 (not yet released)

#904: An error that occurred when serializing a `value class` that wraps a `Map`(#873) has been fixed.
#904: Fixed a problem where context was not being propagated properly when serializing an unboxed value of `value class`
or a value retrieved with `JsonValue`.
This fixes a problem where an error would occur when serializing a `value class` that wraps a `Map`(#873).

2.18.2 (27-Nov-2024)
2.18.1 (28-Oct-2024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ private fun Class<*>.getStaticJsonValueGetter(): Method? = this.declaredMethods.
object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
override fun serialize(value: Any, gen: JsonGenerator, ctxt: SerializationContext) {
val unboxed = value::class.java.getMethod("unbox-impl").invoke(value)

if (unboxed == null) {
ctxt.findNullValueSerializer(null).serialize(null, gen, ctxt)
return
}

ctxt.writeValue(gen, unboxed)
}
}
Expand All @@ -70,9 +64,7 @@ internal sealed class ValueClassSerializer<T : Any>(t: Class<T>) : StdSerializer
val unboxed = unboxMethod.invoke(value)
// As shown in the processing of the factory function, jsonValueGetter is always a static method.
val jsonValue: Any? = staticJsonValueGetter.invoke(null, unboxed)
jsonValue
?.let { ctxt.findValueSerializer(it::class.java).serialize(it, gen, ctxt) }
?: ctxt.findNullValueSerializer(null).serialize(null, gen, ctxt)
ctxt.writeValue(gen, jsonValue)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package tools.jackson.module.kotlin.test.github

import com.fasterxml.jackson.annotation.JsonValue
import tools.jackson.module.kotlin.defaultMapper
import tools.jackson.module.kotlin.readValue
import kotlin.test.Test

class GitHub873 {
@JvmInline
value class Person(
val properties: Map<String, Any>,
)

data class TimestampedPerson(
val timestamp: Long,
val person: Person,
)

@Test
fun `should serialize value class`() {

Expand Down Expand Up @@ -34,12 +45,18 @@ class GitHub873 {
}

@JvmInline
value class Person(
val properties: Map<String, Any>,
)
value class MapAsJsonValue(val value: String) {
@get:JsonValue
val jsonValue get() = mapOf("key" to value)
}

data class TimestampedPerson(
val timestamp: Long,
val person: Person,
)
data class JsonValueWrapper(val value: MapAsJsonValue)

@Test
fun `JsonValue is serialized in the same way`() {
val data = JsonValueWrapper(MapAsJsonValue("value"))
val json = defaultMapper.writeValueAsString(data)

assert("""{"value":{"key":"value"}}""" == json)
}
}

0 comments on commit 4c97452

Please sign in to comment.