1
1
package com.fasterxml.jackson.module.kotlin.test
2
2
3
3
import com.fasterxml.jackson.annotation.JsonCreator
4
+ import com.fasterxml.jackson.annotation.JsonFormat
5
+ import com.fasterxml.jackson.annotation.JsonFormat.Shape.STRING
4
6
import com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
5
7
import com.fasterxml.jackson.databind.SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS
6
8
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
@@ -12,7 +14,8 @@ import org.junit.Test
12
14
import java.time.Instant
13
15
import kotlin.test.assertContentEquals
14
16
import kotlin.test.assertEquals
15
- import kotlin.time.Duration
17
+ import kotlin.time.Duration as KotlinDuration
18
+ import java.time.Duration as JavaDuration
16
19
import kotlin.time.Duration.Companion.hours
17
20
18
21
class DurationTests {
@@ -31,7 +34,7 @@ class DurationTests {
31
34
fun `should deserialize Kotlin duration` () {
32
35
val mapper = jacksonObjectMapper().registerModule(JavaTimeModule ())
33
36
34
- val result = mapper.readValue<Duration >(" \" PT1H\" " )
37
+ val result = mapper.readValue<KotlinDuration >(" \" PT1H\" " )
35
38
36
39
assertEquals(1 .hours, result)
37
40
}
@@ -51,7 +54,7 @@ class DurationTests {
51
54
fun `should deserialize Kotlin duration inside list` () {
52
55
val mapper = jacksonObjectMapper().registerModule(JavaTimeModule ())
53
56
54
- val result = mapper.readValue<List <Duration >>(""" ["PT1H","PT2H","PT3H"]""" )
57
+ val result = mapper.readValue<List <KotlinDuration >>(""" ["PT1H","PT2H","PT3H"]""" )
55
58
56
59
assertContentEquals(listOf (1 .hours, 2 .hours, 3 .hours), result)
57
60
}
@@ -75,7 +78,7 @@ class DurationTests {
75
78
fun `should deserialize Kotlin duration inside map` () {
76
79
val mapper = jacksonObjectMapper().registerModule(JavaTimeModule ())
77
80
78
- val result = mapper.readValue<Map <String , Duration >>(""" {"a":"PT1H","b":"PT2H","c":"PT3H"}""" )
81
+ val result = mapper.readValue<Map <String , KotlinDuration >>(""" {"a":"PT1H","b":"PT2H","c":"PT3H"}""" )
79
82
80
83
assertEquals(result[" a" ], 1 .hours)
81
84
assertEquals(result[" b" ], 2 .hours)
@@ -85,12 +88,12 @@ class DurationTests {
85
88
data class Meeting (
86
89
val start : Instant ,
87
90
@get:JsonDeserialize(converter = JavaToKotlinDurationConverter ::class)
88
- val duration : Duration ,
91
+ val duration : KotlinDuration ,
89
92
) {
90
93
companion object {
91
94
@JvmStatic
92
95
@JsonCreator
93
- fun create (start : Instant , duration : Duration ) = Meeting (start, duration)
96
+ fun create (start : Instant , duration : KotlinDuration ) = Meeting (start, duration)
94
97
}
95
98
}
96
99
@@ -115,4 +118,32 @@ class DurationTests {
115
118
assertEquals(result.start, Instant .parse(" 2023-06-20T14:00:00Z" ))
116
119
assertEquals(result.duration, 1.5 .hours)
117
120
}
121
+
122
+ data class JDTO (
123
+ val plain : JavaDuration = JavaDuration .ofHours(1),
124
+ val optPlain : JavaDuration ? = JavaDuration .ofHours(1),
125
+ @field:JsonFormat(shape = STRING )
126
+ val shapeAnnotation : JavaDuration = JavaDuration .ofHours(1),
127
+ @field:JsonFormat(shape = STRING )
128
+ val optShapeAnnotation : JavaDuration ? = JavaDuration .ofHours(1),
129
+ )
130
+
131
+ data class KDTO (
132
+ val plain : KotlinDuration = 1 .hours,
133
+ val optPlain : KotlinDuration ? = 1 .hours,
134
+ @field:JsonFormat(shape = STRING )
135
+ val shapeAnnotation : KotlinDuration = 1 .hours,
136
+ @field:JsonFormat(shape = STRING )
137
+ val optShapeAnnotation : KotlinDuration ? = 1 .hours,
138
+ )
139
+
140
+ @Test
141
+ fun `should serialize Kotlin duration exactly as Java duration` () {
142
+ val mapper = jacksonObjectMapper().registerModule(JavaTimeModule ())
143
+
144
+ val jdto = JDTO ()
145
+ val kdto = KDTO ()
146
+
147
+ assertEquals(mapper.writeValueAsString(jdto), mapper.writeValueAsString(kdto))
148
+ }
118
149
}
0 commit comments