Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:38:40+00:00 2026-06-09T16:38:40+00:00

I’m trying to serialize some relatively simple models into json. For example, I’d like

  • 0

I’m trying to serialize some relatively simple models into json. For example, I’d like to get the json representation of:

case class User(val id: Long, val firstName: String, val lastName: String, val email: Option[String]) {
    def this() = this(0, "","", Some(""))
}

Do i need to write my own Format[User] with the appropriate reads and writes methods or is there some other way? I’ve looked at https://github.com/playframework/Play20/wiki/Scalajson but I’m still a bit lost.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:38:41+00:00Added an answer on June 9, 2026 at 4:38 pm

    Yes, writing your own Format instance is the recommended approach. Given the following class, for example:

    case class User(
      id: Long, 
      firstName: String,
      lastName: String,
      email: Option[String]
    ) {
      def this() = this(0, "","", Some(""))
    }
    

    The instance might look like this:

    import play.api.libs.json._
    
    implicit object UserFormat extends Format[User] {
      def reads(json: JsValue) = User(
        (json \ "id").as[Long],
        (json \ "firstName").as[String],
        (json \ "lastName").as[String],
        (json \ "email").as[Option[String]]
      )
    
      def writes(user: User) = JsObject(Seq(
        "id" -> JsNumber(user.id),
        "firstName" -> JsString(user.firstName),
        "lastName" -> JsString(user.lastName),
        "email" -> Json.toJson(user.email)
      ))
    }
    

    And you’d use it like this:

    scala> User(1L, "Some", "Person", Some("s.p@example.com"))
    res0: User = User(1,Some,Person,Some(s.p@example.com))
    
    scala> Json.toJson(res0)
    res1: play.api.libs.json.JsValue = {"id":1,"firstName":"Some","lastName":"Person","email":"s.p@example.com"}
    
    scala> res1.as[User]
    res2: User = User(1,Some,Person,Some(s.p@example.com))
    

    See the documentation for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.