【发布时间】:2023-03-05 14:57:01
【问题描述】:
我一直想知道是否有可能将 json 自动反序列化为特定的对象属性,并在不实现任何自定义反序列化器的情况下使用不同的名称对其进行序列化。我正在努力将我的应用程序与 Github API 集成,我想对其命名约定进行改造。一旦请求资源,我会得到如下信息:
{
full_name: "...",
description: null,
clone_url: "...",
stargazers_count: 0,
created_at: "2018-06-05"
}
但我想将它们公开为:
{
fullName: "...",
description: null,
cloneUrl: "...",
stars: 0,
createdAt: "2018-06-05"
}
这是我的模型现在的样子:
internal data class GithubRepositoryResponse(@JsonProperty("full_name") val fullName: String,
val description: String?,
@JsonProperty("clone_url") val cloneUrl: URL,
@JsonProperty("stargazers_count") val stars: Int,
@JsonProperty("created_at") val createdAt: LocalDate)
使用带有值的@JsonProperty 是双向的,因此我得到的属性名称与注释中声明的属性名称相同,这是我非常想避免的。有没有什么方便的方法来处理这样的场景?
【问题讨论】:
-
@span 这样我就无法将 stargazers_count 保留为星星,我得到了必须满足的规范,所以这不是一个选项。
标签: json spring serialization kotlin jackson