Skip to content

Add extension functions for JsonMapper#1094

Closed
emrearslan wants to merge 1 commit intoFasterXML:3.xfrom
emrearslan:3.x
Closed

Add extension functions for JsonMapper#1094
emrearslan wants to merge 1 commit intoFasterXML:3.xfrom
emrearslan:3.x

Conversation

@emrearslan
Copy link
Copy Markdown

Add extension functions for JsonMapper

Summary

This PR adds reified type extension functions for JsonMapper, similar to the existing ones for ObjectMapper.

Changes

  • Added the following extension functions for JsonMapper:

    • readValue(jp: JsonParser): T
    • readValues(jp: JsonParser): MappingIterator<T>
    • readValue(src: File): T
    • readValue(content: String): T
    • readValue(src: Reader): T
    • readValue(src: InputStream): T
    • readValue(src: ByteArray): T
    • treeToValue(n: JsonNode): T
    • convertValue(from: Any?): T
  • Added unit tests for the new extension functions

Motivation

Spring Boot 4 will use Jackson 3.x, where JsonMapper is the recommended entry point for JSON processing instead of ObjectMapper. As more Kotlin developers adopt Spring Boot 4, they will primarily work with JsonMapper instances. Having dedicated extension functions for JsonMapper ensures a smooth developer experience with proper IDE support and type inference, without relying on inheritance from ObjectMapper.

Example Usage

val mapper: JsonMapper = jsonMapper { addModule(kotlinModule()) }
val person: Person = mapper.readValue(jsonString)
val tree = mapper.readTree(source)
val result: List = mapper.treeToValue(tree)

@cowtowncoder
Copy link
Copy Markdown
Member

cowtowncoder commented Feb 2, 2026

One concern: EVERY format backend has their own mapper similar to JsonMapper, but I think Kotlin module cannot really extend others (like, say, YAMLMapper) similarly, at least without adding direct dependency.
So not sure if this causes discrepancy b/w JSON handling and other formats.

@emrearslan
Copy link
Copy Markdown
Author

Thank you for the concern 🙂

I noticed this gap while migrating my project to Spring Boot 4.x. I've never needed YAMLMapper or XMLMapper extensions before, so this wasn't something I thought about. I just wanted to keep the same habits I had with ObjectMapper when working with JsonMapper. Having similar extensions for JsonMapper would make the transition smoother, at least for JSON users like me.

@k163377
Copy link
Copy Markdown
Contributor

k163377 commented Mar 28, 2026

I apologize for the delayed response.

Personally, I don’t see a compelling reason to implement this.
At least on my local machine, IntelliJ successfully suggests completions for the existing read function even for JsonMapper.
In other words, if the correct completions aren’t being suggested, I suspect that’s not the fault of kotlin-module, but rather an issue with the completion functionality of the IDE being used.

It seems unreasonable for kotlin-module to officially add support for such issues and maintain them going forward.
Shouldn’t such code be managed by individual users as their own workarounds?

@emrearslan
Copy link
Copy Markdown
Author

Thank you for the response.

I want to clarify that the motivation isn't IDE autocomplete. It's about keeping the same habits I had with ObjectMapper when working with JsonMapper. Even if IntelliJ suggests inherited methods, without the reified extensions you still have to pass the type explicitly (mapper.readValue(json, Person::class.java) instead of mapper.readValue(json)).

These extensions are a direct mirror of the existing ObjectMapper ones. No new logic, just a new receiver type. JsonMapper is the recommended mapper in Jackson 3.x, and as Spring Boot 4 adopts it, more Kotlin developers will go through the same transition I did. Having these extensions in kotlin-module would make that transition smoother, rather than leaving each developer to add their own workaround.

@k163377
Copy link
Copy Markdown
Contributor

k163377 commented Apr 2, 2026

JsonMapper is a subclass of ObjectMapper.
Even without adding new functions, the current implementation works for JsonMapper.
If that’s the case, I don’t see any reason to add a completely duplicate implementation for JsonMapper.

Am I missing something?

@emrearslan
Copy link
Copy Markdown
Author

Actually, you're right. When I tried again just now, the existing extensions for the JsonMapper worked. I guess the IDE messed up when I was migrating to Spring Boot 4.x.

@k163377
Copy link
Copy Markdown
Contributor

k163377 commented Apr 4, 2026

Understood.
I will close this PR.

@k163377 k163377 closed this Apr 4, 2026
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.

3 participants