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 9193255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:07:04+00:00 2026-06-17T21:07:04+00:00

I have a Scala case class case class Example(name: String, number: Int) and a

  • 0

I have a Scala case class

case class Example(name: String, number: Int)

and a companion object

object Example {
  implicit object ExampleFormat extends Format[Example] {
    def reads(json: JsValue) = {
      JsSuccess(Example(
       (json \ "name").as[String],
       (json \ "number").as[Int]))
     }

     def writes(...){}
   }
}

which converts JSON to Scala object.

When JSON is valid (i.e. {"name":"name","number": 0} it works fine. However, when number is in quotes {"name":"name","number":"0"} I get an error: validate.error.expected.jsnumber.

Is there a way to implicitly convert String to Int in such a case (assuming that the number is valid) ?

  • 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-17T21:07:06+00:00Added an answer on June 17, 2026 at 9:07 pm

    You can easily handle this case with the Json combinators, thanks to the orElse helper. I rewrote your json formater with the new syntax introduced by Play2.1

    import play.api.libs.json._
    import play.api.libs.functional.syntax._
    
    object Example {
      // Custom reader to handle the "String number" usecase
      implicit val reader = (
        (__ \ 'name).read[String] and
        ((__ \ 'number).read[Int] orElse (__ \ 'number).read[String].map(_.toInt))
      )(Example.apply _)
    
      // write has no specificity, so you can use Json Macro
      implicit val writer = Json.writes[Example] 
    }
    
    object Test extends Controller {
      def index = Action {
        val json1 = Json.obj("name" -> "Julien", "number" -> 1000).as[Example]
        val json2 = Json.obj("name" -> "Julien", "number" -> "1000").as[Example]
        Ok(json1.number + " = " + json2.number) // 1000 = 1000
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a class as follows: case class Person( name:String, age:Int, dependents:List[Person]
I have three objects case class Metric(val name: String, val tags: Map[String, String]) case
Have a look at the following Scala example: class A { def foo(x: Int):
Let's say that I have this class case class Test (id: Long, name: String)
I have the following pattern matching case in a scala function: def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int]
In Scala, I can make a caseclass, case class Foo(x:Int) , and then put
Assuming that I have a Scala case class that is persisted using the Salat/Casbah/Mongo
an example use case: def div2(i: Int): Validation[String, Int] = if (i%2 == 0)
Say I have a Scala class with default arguments class MyClass(p1: Int = 0,
Does Scala have a version of Rubys' each_slice from the Array class?

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.