Skip to content

How to deserialize a kotlin.ranges.ClosedRange<T> with Jackson #663

Open
ProjectMapK/jackson-module-kogera
#97
@dibog

Description

@dibog

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<Instant>)

    @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<TestClass>(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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions