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

Strange serialization property name internal #607

Closed
entrery opened this issue Nov 18, 2022 · 6 comments
Closed

Strange serialization property name internal #607

entrery opened this issue Nov 18, 2022 · 6 comments
Labels

Comments

@entrery
Copy link

entrery commented Nov 18, 2022

Hi experts,
I am new to Kotlin and I am trying to use Jackson for a simple (de)serialization scenario of a Kotlin class with the Jackson Kotlin module. However when I serialize the object I see a strange property pattern for the high-level object. For example if my class is called Account and it has a property transactions instead of "transactions:[]" in the json output I see transactions$":[]. Why is that ? I presume it is pretty straightforward to answer. Thanks in advance!

@cowtowncoder
Copy link
Member

@entrery Please include relevant class definition(s) instead of verbal explanation of the same.

@entrery
Copy link
Author

entrery commented Nov 20, 2022

Apologies @cowtowncoder I thought it is a simple issue on my side. I deep dived a bit more into the issue. It turns out the strange behaviour is caused by the internal keyword in Kotlin. Here is a simple definition of a class and the json output:

class A {
    internal var number: Int? = null
}

fun main() {
   val v = A().apply {
        number = 100
    };
    val mapper = jacksonObjectMapper()
    val timeModule = JavaTimeModule()
    timeModule.addSerializer(LocalDate::class.java, LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE))
    timeModule.addSerializer(LocalDateTime::class.java, LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
    mapper.registerModule(timeModule)
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)

    println(mapper.writeValueAsString(v))
}

Actual Output:

{"number$<NAME_OF_MY_MAVEN_MODULE>":100}

Expected Output:

{"number":100}

Any hint is appreciated, thanks!

@cowtowncoder
Copy link
Member

Ok I don't know enough about Kotlin or this module to know but I hope someone else can help more.

My only guess is that somehow Polymorphic deserialization was enabled for value class, using "as-wrapper-object" style -- output looks like that, so that key is simple class name of an inner class. But your declaration does not use @JsonTypeInfo nor enable default typing on mapper which would be the usual ways to enable it.

@entrery
Copy link
Author

entrery commented Nov 20, 2022

Hi @cowtowncoder, First thanks for your reply. I continued with the investigation... It turned out you were already involved in a discussion about this issue in 2017. Here is the thread #71. I don't understand at all what the guys are talking about there because I am new to Kotlin but tomorrow when I weak up I will read it carefully :). You can also have a look and give me a hint if the provided solution at the end seems ok. Thanks!

@cowtowncoder cowtowncoder changed the title Strange serialization property name Strange serialization property name internal Nov 21, 2022
@cowtowncoder
Copy link
Member

Oh. :)

I had fogotten about this. Work-arounds suggested may work but aren't really the proper solution.

@k163377
Copy link
Contributor

k163377 commented Feb 19, 2023

@entrery
As you say, the issue is a duplicate of issue #71, so this issue is closed.
Progress on the kotlin-module modifications will be reported in #71 (or #630 ).

@k163377 k163377 closed this as completed Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants