score:1
One workaround would be to write an apply method that takes in the default value of count as an option and handles the construction (can't name it apply as we need an unambiguous name when building our Reads):
object Item{
def applyOpt(id:Option[Int], name:String, description:Option[String], count:Option[Int]): Item = count.map{c =>
Item(id, name, description, c)
}.getOrElse{
Item(id, name, description)
}
}
Then you could use readNullable for the default value, which will pass an Option[Int] to applyOpt:
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit val itemReads: Reads[Item] = (
(__ \ "id").readNullable[Int] and
(__ \ "name").read[String] and
(__ \ "description").readNullable[String] and
(__ \ "count").readNullable[Int]
)(Item.applyOpt _)
Certainly not ideal, especially if you have several default fields, but a quick workaround that avoids having to deal with macros or reflection.
score:4
Almost. You can define a default value with an Option like this:
case class Item( description:Option[String] = Some("String"))
If you definitely do not want an option, you can have a look here:
Source: stackoverflow.com
Related Query
- Play Scala JSON body parser default value field
- JSON deserialization in scala - use default value if field doesn't exist
- Scala Generic JSON parser with default value support
- Constant value in Scala Play JSON Reads
- Scala + Play Framework + Slick - Json as Model Field
- Parse json list to two list types by field value in Scala circe
- How to update each field in a list using Play json in Scala
- Parse Numeric OR String value from Json response - Scala - Play
- Parsing HTTP request JSON body in scala with play
- Ignore JSON field Play Controller Scala
- bind a JSON value to a Scala class using play
- Expose only field subset in REST JSON Service. Play framework, scala
- Scala Play JSON parser throws error on simple key name access
- Play framework, scala forms set default value
- Scala declare class with default value for field
- Play Scala JSON - conditionally add field to JSON object in Writes
- how to change json field value in the json level with Play json?
- Play 2.6.x Scala How to Specify a TolerantText Body Parser for an Action
- How to replace value in json using scala play json
- Get filtered JSON value Scala Play
- Play Json Parser: How to ignore field during json parser
- Read a JSON value from request.body in Scala & Play Framework
- How do I write a generic JSON parser in Play 2.7 for Scala that validates inbound requests?
- Scala play JSON, lookup and match defined field holding null value
- Scala case match default value
- Play Framework - add a field to JSON object
- How to load JSON file using Play with Scala
- Default value for type parameter in Scala
- Scala to JSON in Play Framework 2.1
- How to replace a JSON value in Play
More Query from same tag
- Custom display in CRUDified object
- Scala: Convert Json JObject to List
- Set sequencing type puzzle
- Implicit conversion between java and scala collections using JavaConversions
- How to create an optional section in my template that will render near the bottom
- A bigger loop in Scala
- What compromises Scala made to run on JVM?
- Aggregate root implementation with slick
- How to represent dynamic JSON keys in Scala when using circe
- Why does SBT libraryDependencies %% truncate scalaVersion?
- how is scala type casting done in py4j?
- Calling the default constructor in scala
- Scala: AnyVal usage
- Scala equivalent to Haskell pattern Matching on constructors
- How do I set a system property for my project in sbt?
- How scala implicit make abstract method valid parameter so I can use method in java
- Prove upper type bound for generic types
- Get arguments back from partially applied function in scala
- Gatling: Access variables from saved "findAll" list in foreach loop
- Inserting a document with reactivemongo and fetching it afterwards
- How to switch off Auto Import with IntelliJ/Scala Plugin
- ExceptionHandler doesn't work with spray test-kit?
- Merge Rows in Apache spark by eliminating null values
- Scala Monkey Patching Compared to Ruby
- How to implement String contains with equals ignore case to filter content of a dataframe?
- How to set jetty port xsbt plugin 1.0.0-M7
- New desugaring behavior in Scala 2.10.1
- Assign new value to var in loop
- SBT prepare WAR file, duplicate entry: META-INF/MANIFEST.MF
- What is Scala trying to tell me and how do I fix this? [ required: java.util.List[?0] where type ?0]