FasterXML / FasterXML/jackson-module-kotlin
How to deserialize a kotlin.ranges.ClosedRange<T> with Jackson
- Dominant language
- Kotlin
- Stars
- 1.2k
- Forks
- 187
- Avg merge
- 9h 48m
- Merged PRs (30d)
- 16
Description
**Describe the bug**
I would like to deserialize a data structure which contains a serialized ClosedRage field.
**To Reproduce**
Run this test case:
```
class ClosedRangeTest {
data class TestClass(private val range: ClosedRange)
@Test
fun closedRange() {
val om = jacksonObjectMapper()
val expected = TestClass(Instant.parse("2023-01-01T10:00:00Z")..Instant.parse("2023-01-01T11:00:00Z"))
val json = om.writeValueAsString(expected)
val actual = om.readValue(json)
assert(actual == expected)
}
}
```
**Expected behavior**
The test case should succeed and not to fail with:
```
Cannot construct instance of `kotlin.ranges.ClosedRange` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (String)"{}"; line: 1, column: 2]
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `kotlin.ranges.ClosedRange` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (String)"{}"; line: 1, column: 2]
at app//com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at app//com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1909)
at app//com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:408)
... many more lines ...
```
**Versions**
Kotlin: 1.7.22
Jackson-module-kotlin: 2.14.2
Jackson-databind: 2.14.2
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.