Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for inline classes #205

Closed
wants to merge 1 commit into from

Conversation

TjeuKayim
Copy link

@TjeuKayim TjeuKayim commented Dec 31, 2018

This pull request implements support for Kotlin's new inline classes.

Solves issue #199.

Jackson can't deserialize an inline class, because the constructor is private in the generated bytecode.
However, the synthetic method box-impl can be used as JsonCreator. So I added some code to KotlinNamesAnnotationIntrospector.hasCreatorAnnotation().

Previously, inline classes where serialized as object, i.e. {"value":123}. But it should just serialize as a number: 123.
So the property needs to be annotated with @JsonValue. It would be useful to do this by default for inline classes. That's why I overrided hasAsValue().

With these two changes, inline classes can be read and written to JSON without any configuration.

inline class Watt(val value: Long)

@Test
fun readInlineClass() {
    val watt = Watt(1234)
    val json = "1234"
    val result = mapper.readValue<Watt>(json)
    assertEquals(watt, result)
}

@Test
fun writeInlineClass() {
    val watt = Watt(1234)
    val json = mapper.writeValueAsString(watt)
    assertEquals("1234", json)
}

@TjeuKayim TjeuKayim changed the title WIP: Support for inline classes Support for inline classes Jan 1, 2019
@TjeuKayim
Copy link
Author

TjeuKayim commented Jan 4, 2019

I found a problem with my current implementation. Classes that contain a property with inline class type, can't be constructed. It’s because in the bytecode the field is unboxed.

inline class IC(val u: Int)
class HasInlineClassProperty(val ic: IC)

So we also need to cover cases like this.

@TjeuKayim
Copy link
Author

Sorry, I need to stop working on this feature, because it’s too much work.

My current progress:
https://github.com/TjeuKayim/jackson-module-kotlin/commits/class-descriptor-reflection

I hope my contribution will be useful.
Good luck finishing the support for inline classes!

@TjeuKayim TjeuKayim closed this Jan 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant