有没有一种方法可以将JSON逐渐化为Kotlin数据类并转换属性类型?

发布于 2025-02-04 15:27:50 字数 472 浏览 1 评论 0原文

我当前有一些JSON

{ 
    "name": "Foo",
    "age": 12,
    "id": "1234567890"
}

,我的数据类,我希望将其归类为看起来像是

data class Example( val name: String, val age: Int, val id: String)

有一种方法可以将数据类简单地将

data class Example( val name: String, val age: Int, val id: Long)

关注ID类型long

关注这可以通过使用将数据类解析为单独的数据类的扩展功能来实现。

但是我想知道这是否可以以其他方式进行。我正在使用杰克逊进行审判。

I have some json

{ 
    "name": "Foo",
    "age": 12,
    "id": "1234567890"
}

Currently, my data class I want to deserialize into looks like this

data class Example( val name: String, val age: Int, val id: String)

Is there a way to simply have the data class as

data class Example( val name: String, val age: Int, val id: Long)

Attention to the id type Long

I suppose this can be achieved through the use of an extension function that parses the data class into a separate data classes.

But I'd like to know if this is possible in any other way. I'm using Jackson for deserialization.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2025-02-11 15:27:50

您不必做任何事情。如果可能的话,杰克逊会自动将字符串转换为长时间:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

data class C(
    val s: String,
    val n: Long
)

fun main() {
    val json = """{"s":"My string","n":"1234567890"}"""

    val mapper = jacksonObjectMapper()
    val c: C = mapper.readValue(json)
    println(c)  // prints "C(s=My string, n=1234567890)"
}

此自动转换由 mapperfeature.allow_coercion_of_scalars ,默认启用。

You don't have to do anything. Jackson automatically converts the String into a Long if it is possible:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

data class C(
    val s: String,
    val n: Long
)

fun main() {
    val json = """{"s":"My string","n":"1234567890"}"""

    val mapper = jacksonObjectMapper()
    val c: C = mapper.readValue(json)
    println(c)  // prints "C(s=My string, n=1234567890)"
}

This automatic conversion is controlled by MapperFeature.ALLOW_COERCION_OF_SCALARS, which is enabled by default.

回首观望 2025-02-11 15:27:50

我知道您说您正在使用杰克逊,但是Kotlin现在有本机对序列化的支持!。使用它,您的代码看起来像这样。

import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.LongAsStringSerializer

@Serializable
data class Example(
  val name: String, 
  val age: Int,
  @Serializable(with = LongAsStringSerializer::class)
  val id: Long
)

您也可以查看此示例来自官方文档

I know you said you're using jackson, but Kotlin now has native support for serialization!. Using that, your code would look like this

import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.LongAsStringSerializer

@Serializable
data class Example(
  val name: String, 
  val age: Int,
  @Serializable(with = LongAsStringSerializer::class)
  val id: Long
)

You can also check out this example from the official docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文