在 Kotlin 中读取 YAML 文件

发布于 2025-01-18 02:51:41 字数 926 浏览 0 评论 0原文

我很难弄清楚如何读取Kotlin中的YAML文件。

简而言之,YAML具有以下格式:

aws:
  foo:
    dev:
      id: '1111'
    pro:
      id: '2222'
  bar:
    dev:
      id: '3333'
    pro:
      id: '4444'

我创建了以下数据类:

data class Account (
        val id: String
)

data class Owner (
        val accounts: List<Account>
)

data class Cloud (
        val owners: List<Owner>
)

然后我尝试用以下方式解析文件:

val mapper = ObjectMapper().registerModule(KotlinModule())
val settings: Cloud = mapper.readValue(Path.of("accounts.yaml").toFile())
# also tried this
val settings: List<Cloud> = mapper.readValue(Path.of("accounts.yaml").toFile())
println(settings)

println在线程“ main” com.fasterxml中使用异常失败。

I'm having a hard time trying to figure out how to read a YAML file in Kotlin.

In short, the YAML has the following format:

aws:
  foo:
    dev:
      id: '1111'
    pro:
      id: '2222'
  bar:
    dev:
      id: '3333'
    pro:
      id: '4444'

I have created these data classes:

data class Account (
        val id: String
)

data class Owner (
        val accounts: List<Account>
)

data class Cloud (
        val owners: List<Owner>
)

And then I try to parse the file with:

val mapper = ObjectMapper().registerModule(KotlinModule())
val settings: Cloud = mapper.readValue(Path.of("accounts.yaml").toFile())
# also tried this
val settings: List<Cloud> = mapper.readValue(Path.of("accounts.yaml").toFile())
println(settings)

the println fails with Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'aws': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

Why?

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

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

发布评论

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

评论(3

堇色安年 2025-01-25 02:51:41

您需要包含 jackson-dataformat-yaml 依赖项,然后像这样创建 ObjectMapper:

val mapper = ObjectMapper(YAMLFactory()).registerModule(KotlinModule())

You need to include the jackson-dataformat-yaml dependency and then create your ObjectMapper like this:

val mapper = ObjectMapper(YAMLFactory()).registerModule(KotlinModule())
椒妓 2025-01-25 02:51:41

另一种方法是使用 Kaml

将以下依赖项添加到您的 build.gradle.kts< /code>

implementation("com.charleskorn.kaml:kaml:0.46.0")

然后将@Serialized注释添加到您的数据类:

@Serializable
data class Cloud (
    val owners: List<Owner>
)

然后您可以从YAML解析它:


val parsedYaml = Yaml.default.decodeFromString(Cloud.serializer(), Path.of("accounts.yaml").toFile().readText())

An alternative would be to use Kaml:

Add the following dependency to your build.gradle.kts

implementation("com.charleskorn.kaml:kaml:0.46.0")

Then add @Serializable annotation to your data classes:

@Serializable
data class Cloud (
    val owners: List<Owner>
)

Then you can parse it from YAML:


val parsedYaml = Yaml.default.decodeFromString(Cloud.serializer(), Path.of("accounts.yaml").toFile().readText())
雪花飘飘的天空 2025-01-25 02:51:41

添加依赖项:

implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.2")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.2")

创建映射器:

val mapper = ObjectMapper(YAMLFactory()).registerKotlinModule()

Add dependencies:

implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.2")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.2")

Create mapper:

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