score:1
Accepted answer
yes, this is possible by using the readwithdefault
method.
having:
case class applyrequest(a: string, b: string, c: option[string], d: option[int])
private val json =
"""
|{
| "a": "test1",
| "b": null,
| "c": null,
| "d": 1
|}
""".stripmargin
the reads
is defined:
implicit val readobj: reads[applyrequest] = (
(jspath \ "a").read[string] and
(jspath \ "b").readwithdefault[string]("null") and // default value here
(jspath \ "c").readnullable[string] and
(jspath \ "d").readnullable[int]
) (applyrequest.apply _)
parsed:
val parsedjsvalue = json.parse(json)
val parsedapplyrequest = json.fromjson[applyrequest](parsedjsvalue).get
println(parsedapplyrequest) // applyrequest(test1,null,none,some(1))
println(parsedapplyrequest.b.getclass) // class java.lang.string
Source: stackoverflow.com
Related Query
- Play framework: Reading possibily null value into string "null" instead of Option[String]
- How to convert String into JsValue in Play Framework
- How to implicitly wrap a value that can be null or an array into an Scala Option
- Null values in JsObject for Option using play framework 2.1
- Spark dataframe get column value into a string variable
- How to integrate Play Framework 2.0 into Gradle build management using Maven dependencies?
- Use of option helper in Play Framework 2.0 templates
- How to convert Play Framework Models into XML and JSON?
- Scala, Play Framework Slick issue - could not find implicit value for parameter rconv
- Which Scala methods return null instead of an Option and why?
- Calling into Play framework app from the Scala console
- Play framework JSON reads: How to read either String or Int?
- How to run Play Framework 2.2.0 with -feature option
- Stepping into JSON Arrays in Play Framework
- play framework 2: test a request with a json string as body
- Getting a raw string back for use in Javascript in Play Framework 2.0
- Parsing a Json String in Scala using Play framework
- Play framework 2.5 logs `?` question marks instead of line numbers
- Reading and filtering a file into a string
- Play Framework 2.2 - could not find implicit value for parameter timeout
- Play Framework 2.2 Null Pointer Exception ArrayList
- Compilation error: not found: value nonEmptyText in Play framework while using Scala
- Not persisting Scala None's instead of persisting as null value
- Append/Add JsObject into JsArray in Play Framework
- Could not import the newly generated play framework project into IntelliJ IDEA 15
- reading cookie value in play framewrok 2.2 scala template
- How to integrate checkstyle and PMD into Play framework Build.scala file
- How to get String value from Rep[String] using slick 3 in Play scala?
- Play framework Scala - get session value in a form
- How to send json string body for FakeRequest without using withJsonBody in Play framework v2.x?
More Query from same tag
- How do I open Try value in scala and add its values by mapping them
- Scala - Problem using MapWrapper when upgrading from 2.7.7 to 2.8.1
- How to send -k or ignore cert verification in scala play framework wsclient?
- Last Date Id Of Previous Months
- Can't Get Correct Fibonacci Sequence From Scala Code
- Rounding in Scala and division
- @Repeat Form Helper with complex object - Play Framework
- Scala - Use of .indexOf() and .indexWhere()
- Scalatra test failing using ScalatraSuite (FunSuiteLike) for standlone sbt project
- scala mutable.Map put/get handles null's in an unexpected way?
- scala scope and pattern matching
- Operations betzeen matrix and scalar in breeze
- Avoid structural type with shapeless alternative
- Error with spark Row.fromSeq for a text file
- Why creating an actor within actor is dangerous
- Convert scala byte to java byte when using containers
- How does the Scala toSeq:_* syntax work?
- Running Spark-Shell on Windows
- Play framework with Scala 3
- Is there a way to deflate & map bit-mapped database columns into scala objects via slick
- Which sender is captured inside Actor message handler?
- Scala final variables in constructor
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- How to make a method synchronous in scala?
- explicitly close db connection in slick
- How to convert csv file to Seq[String]?
- How to convert SearchResponse to JsValue
- Why does type A >: Number means a container that store Number instances?
- PlayScalaJS with bootstrap not working
- Set precision of DecimalType returned by UDF