pushka icon indicating copy to clipboard operation
pushka copied to clipboard

How to ignore some fields from serialization?

Open AlexanderBykin opened this issue 8 years ago • 2 comments

lets look what i mean

  @pushka
  case class BaseMessage(uid: String)

  @pushka
  case class HandshakeRequest(@pushkaIgnore uid: String, user: String, timestamp: String, token: String) extends BaseMessage(uid)

i want to ignore from serialization uid at HandshakeRequest How to do that?

AlexanderBykin avatar Oct 18 '17 19:10 AlexanderBykin

Hi!

In a difference with Jackson which designed to operate mutable objects, pushka designed to work with immutable case classes. Also null is disallowed. So we can't just ignore a fields. If field will be not written, how it can be read without nullability? Right answer is Option. If the field can be ignored it is an optional field.

@pushka
sealed abstract class BaseMessage(uid: Option[String])

object BaseMessage {
  case class HandshakeRequest(user: String, timestamp: String, token: String, uid: Option[String] = None) extends BaseMessage(uid)
}

fomkin avatar Oct 19 '17 04:10 fomkin

uid should be defined but not serialized, in that case Option[...] is not a way

AlexanderBykin avatar Oct 19 '17 05:10 AlexanderBykin