score:9
As mentioned in the play-framework documentation here
Format[T] is just a mix of the Reads and Writes traits and can be used for implicit conversion in place of its components.
Format is a combination of Reads[T] and Writes[T]. So you can define a single implicit Format[T] for type T and use it to read and write Json instead of defining a separate implicit Reads[T] and Writes[T] for type T. Thus if you already have Reads[T] and Writes[T] defined for your type T then Format[T] is not required and vice versa.
One advantage with Format is that you can define a single Implicit Format[T] for your type T instead of defining two separate Reads[T] and Writes[T] if they are both symmetrical (i.e. the Reads and Writes). So Format makes your JSON Structure definition less repetitive. For example you can do something like this
implicit val formater: Format[Data] = (
(__ \ "id").format[Int] and
(__ \ "name").format[String] and
(__ \ "value").format[String]
) (Data.apply, unlift(Data.unapply))
Instead of this.
implicit val dataWriter: Writes[Data] = (
(__ \ "id").write[Int] and
(__ \ "name").write[String] and
(__ \ "file_type").write[String]
) (Data.apply)
implicit val dataWriter: Reads[Data] = (
(__ \ "id").read[Int] and
(__ \ "name").read[String] and
(__ \ "file_type").read[String]
) (unlift(Data.unapply))
Source: stackoverflow.com
Related Query
- What is the purpose of Format[T] if you have a Reads[T] and Writes [T]?
- What is the purpose of the emptyCoproduct and coproduct methods of the TypeClass trait in Shapeless
- Do you plan to use the Scala programming language and on what project?
- What is the purpose of *> and <* in Scalaz
- What is the correct way to use typeclass syntax when you have a typeclass within a typeclass?
- What is the purpose of outer and inner function parameters in Scala?
- What are the attributes that make 'types-first' programming in Scala have less code and less bugs?
- What are the exact versions of stuff you have to install in order to be able to step-debug a Scala program?
- What is the difference between Scala's case class and class?
- What is the difference between self-types and trait subclasses?
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- What is the difference between a var and val definition in Scala?
- What is the difference between "def" and "val" to define a function
- What is the difference between JavaConverters and JavaConversions in Scala?
- What is a Manifest in Scala and when do you need it?
- What are the key differences between Scala and Groovy?
- What are the relationships between Any, AnyVal, AnyRef, Object and how do they map when used in Java code?
- What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?
- What is the difference between build.sbt and build.scala?
- Scala: What is the difference between Traversable and Iterable traits in Scala collections?
- What are the differences and similarties between Scala traits vs. Java 8 interfaces?
- What is the purpose of type ascriptions in Scala?
- Scala - What is the difference between size and length of a Seq?
- What are the differences between final class and sealed class in Scala?
- What are the differences and similarities of Scala and Haskell type systems?
- What is the differences between Int and Integer in Scala?
- What is the meaning of colon, underscore and star in lift's SiteMap(entries:_*)?
- What are the biggest differences between Scala 2.8 and Scala 2.7?
- What is the difference between a class and a type in Scala (and Java)?
- What is the difference between def foo = {} and def foo() = {} in Scala?
More Query from same tag
- Creating JSON from mapFields in Scala using Jackson library
- How are final val defined inside a trait treated by Scala Compiler?
- convert a method call with parameters to a Function that takes no parameters
- Message body reader for AsyncResponse
- Merge Dataframes With Differents Schemas - Scala Spark
- Issue with applying partially-applied functions in-line
- scala programming question about importing other objects
- Handle exception in for comprehension
- manipulate list of tuples in a generic way scala
- Testing Akka Actors Fault Tolerance
- Pivoting a Dataframe column transforming on a User ID Spark
- Contravariance in scala
- Using .join in Scala to remove unnecessary lines from one database
- Why Try-> recover returns empty Success in case and condition is not satisfied
- Spray - deserializing optional query parameter
- Using scala.Option functionally
- Running SBT as Daemon
- Implicit conversions for members that are types
- functional programming with scala
- How to extract number from string column?
- Spark returns (LogisticRegression) model with scaled coefficients
- Checkpoint data corruption in Spark Streaming
- scala manipulating Model object values
- couchbase lite - retrieving document properties after reopening database yields different result
- Cartesian product stream scala
- Can a Scala for loop modify variables outside its scope?
- Making a nested if statement more functional in scala
- Multiple type for a variable in spark using scala
- Can you specify a view bound for an abstract type?
- Spark streaming DStream RDD to get file name