杰克逊(Jackson)的objectmapper()。writeValueasString()忽略以“ is_ is_”开头的变量

发布于 2025-02-12 17:19:34 字数 465 浏览 0 评论 0原文

如果A有类似的匿名类,

val a = object {
   val is_something = "some value"
   val something = "other value"
}

并且将

println(ObjectMapper().writeValueAsString(a))

结果称为,

"{"something":"other value"}"

则对于以“ IS_”开头的所有变量,就是这样。为什么?

更正,它不会忽略它。它取下“ IS”,并将变量移至字符串的末端。因此,在这里结果

"{"something":"other value","_something":"some value"}"

仍然是,为什么要这样做呢?

If a have an anonymous class like:

val a = object {
   val is_something = "some value"
   val something = "other value"
}

and call

println(ObjectMapper().writeValueAsString(a))

the result would be

"{"something":"other value"}"

And it's like this for all variables that begin with "is_". Why?

Correction, it doesn't ignore it. It takes off the "is" and moves the variable to the end of the string. So here the result would be

"{"something":"other value","_something":"some value"}"

Still, why does it do that?

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

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

发布评论

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

评论(2

怪我太投入 2025-02-19 17:19:34

杰克逊显然扫描您通过的班级
然后使用这些方法进行序列化和挑选化。

“获取”,“ set”,“ is”被消除,这些方法中仍然存在的内容将用作JSON字段。

因此,您的“ is_something”将其更改为“ _something”等等。

Jackson apparently scans the class that you pass in for getters and setters
and then use those methods for serialization and deserialization.

"Get", "Set", "is" are eliminated and what remains in those methods will be used as Json field.

Hence your "is_something" is changed into "_something" and so on.

§普罗旺斯的薰衣草 2025-02-19 17:19:34

通过在“ is_something”之上添加@jsonproperty并利用它来解决问题。
因此,该对象看起来

val a = object {
   @JsonProperty 
   val Is_something = "some value"
   val something = "other value"
}

仍然不知道是什么原因引起了问题

The issue was solved by adding @JsonProperty on top of "is_something" and capitalizing it.
So the object would look like

val a = object {
   @JsonProperty 
   val Is_something = "some value"
   val something = "other value"
}

Still have no idea what caused the problem

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