Add extension functions for JsonMapper#1094
Conversation
|
One concern: EVERY format backend has their own mapper similar to |
|
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. |
|
I apologize for the delayed response. Personally, I don’t see a compelling reason to implement this. It seems unreasonable for |
|
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. |
|
Am I missing something? |
|
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. |
|
Understood. |
Add extension functions for JsonMapper
Summary
This PR adds reified type extension functions for
JsonMapper, similar to the existing ones forObjectMapper.Changes
Added the following extension functions for
JsonMapper:readValue(jp: JsonParser): TreadValues(jp: JsonParser): MappingIterator<T>readValue(src: File): TreadValue(content: String): TreadValue(src: Reader): TreadValue(src: InputStream): TreadValue(src: ByteArray): TtreeToValue(n: JsonNode): TconvertValue(from: Any?): TAdded unit tests for the new extension functions
Motivation
Spring Boot 4 will use Jackson 3.x, where
JsonMapperis the recommended entry point for JSON processing instead ofObjectMapper. As more Kotlin developers adopt Spring Boot 4, they will primarily work withJsonMapperinstances. Having dedicated extension functions forJsonMapperensures a smooth developer experience with proper IDE support and type inference, without relying on inheritance fromObjectMapper.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)