@JsonIgnore使用Jackon和Json4s序列化Scala案例类属性

编程入门 行业动态 更新时间:2024-10-18 00:35:55
本文介绍了@JsonIgnore使用Jackon和Json4s序列化Scala案例类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试阻止Scala 案例类的某个属性被序列化。我尝试使用通常的 @JsonIgnore 注释相关属性,我也尝试附加 @JsonIgnoreProperties(Array(property_name)) )到案例类。这两者似乎都达不到我想要的。

I'm trying to prevent one of the properties of a Scala case class being serialised. I've tried annotating the property in question with the usual @JsonIgnore and I've also tried attaching the @JsonIgnoreProperties(Array("property_name")) to the case class. Neither of which seem to achieve what I want.

以下是一个小例子:

import org.json4s._ import org.json4s.jackson._ import org.json4s.jackson.Serialization import org.json4s.jackson.Serialization.{read, write} import com.fasterxml.jackson.annotation._ object Example extends App { @JsonIgnoreProperties(Array("b")) case class Message(a: String, @JsonIgnore b: String) implicit val formats = Serialization.formats(NoTypeHints) val jsonInput = """{ "a": "Hello", "b":"World!" }""" val message = read[Message](jsonInput) println("Read " + message) // "Read Message(Hello,World!) val output = write(message) println("Wrote " + output) // "Wrote {"a":"Hello","b":"World!"}" }

推荐答案

将@JsonIgnore更改为@JsonProperty(b)。你有正确地声明忽略财产'b但是'b还没有安作为一个属性。

Change your @JsonIgnore to @JsonProperty("b"). You have correctly stated to Ignore the property 'b but 'b has not yet been annotated as a property.

@JsonIgnoreProperties(Array("b")) case class Message(a: String, @JsonProperty("b") b: String)

更多推荐

@JsonIgnore使用Jackon和Json4s序列化Scala案例类属性

本文发布于:2023-07-14 14:44:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:案例   序列化   类属   Jackon   JsonIgnore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!