Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Enum serialization and deserialization #277

@tarangbhalodia

Description

@tarangbhalodia

In our model we have a gender enumeration defined like so:

object Gender extends Enumeration {
  type Gender = Value
  val Male = Value(0, "male")
  val Female = Value(1, "female")
  val Other = Value(2, "other")

  implicit val genderFormat = new PFormat[Gender.Value] {
    def reads(json: JsValue) = JsSuccess(Gender.withName(json.as[String]))
    def writes(myEnum: Gender.Value) = JsString(myEnum.toString)
  }
}

case class User(userName: String,
  firstName: Option[String] = None,
  lastName: Option[String] = None,
  gender: Option[Gender.Value] = None,
  imageUrl: Option[String] = None,
  tel: Option[String] = None,
  id: Long = 0L)

after saving the record in the DB, the `gender becomes Some(0/1/2)`

How can we resolve this issue? Any help will be highly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions