score:3
you can do this pretty straightforwardly by desugaring a bit:
val writes: writes[foo] = (
(__ \ "i").write[int] and
(__ \ "s").write[string] and
(__ \ "other").write[string]
)(foo => (foo.i, foo.s, "some value."))
the unlift(foo.unapply)
is just a fancy way to get a function from a foo
to a tuple of the kind required by the preceding applicative builder expression, and you can replace it with your own function that can add whatever you want.
if you really wanted even cleaner syntax, you could use shapeless:
import shapeless.syntax.std.tuple._
val writes: writes[foo] = (
(__ \ "i").write[int] and
(__ \ "s").write[string] and
(__ \ "other").write[string]
)(_ :+ "some value.")
it's beautiful, but may be overkill.
score:1
another option is to use an object builder that implements an unapply
that returns the extra values. this makes the writes cleaner, but adds a new object. i have found this useful though as both the apply and unapply method can be used for extra massaging of data into the final object (eg: https://stackoverflow.com/a/22504468/1085606)
example:
case class foo(i: int, s: string)
object foobuilder {
def unapply(foo: foo): option[(int, string, string)] = {
some((foo.i, foo.s, "some extra value"))
}
}
val writes: writes[foo] = ((
(__ \ "i").write[int] and
(__ \ "s").write[string] and
(__ \ "other").write[string]
)(unlift(foobuilder.unapply))
Source: stackoverflow.com
Related Query
- Write an Arbitrary Value Not Found in a Case Class Using Play's (2.2) Scala JSON Combinators
- Parse json array to a case class in scala using playframework with the fields in json not matching the fields in case class
- Using Spark converting nested json with optional fields to Scala case class not working
- How to check which parameters of case class have default value using scala reflection 2.10
- Scala Dynamic Parse Json using case class No Manifest available for T
- Circe Couldn't convert raw json to case class Error: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder
- scala check if at least one case class field value is nonEmpty using reflection
- How to convert a json with a single value to a case class using play json
- Playframework, scala case class and property not found
- Jackson / Scala immutable case classes: parsing a class depending on other value in JSON
- Not able to use JSON Macro Inception to convert nested JSON structure to Scala case class object
- Case class value not found
- bind a JSON value to a Scala class using play
- "Forward reference extends over definition value exp" when using an abstract class with case classes of a Scala tutorial
- remove elements from a List of case class structure when found duplicate scala using foldleft
- How to read optional json values in case class using scala combinators
- Pattern matching json lines using Circe and filtering based upon decoded case class value
- inherited elements of a case class not getting persisted into mongodb using scala mongo driver
- Using function to update case class value in scala Quill
- Value not found when using :load in Spark's Scala REPL
- Java Class not Found Exception while doing Spark-submit Scala using sbt
- json to case class using multiple rows in spark scala
- (Un)marshall JSON with named root for Ember Data using Scala case class on Spray
- How to check if case class parameter has value or not in Scala
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Scala case class update value
- Scala 2.10, its impact on JSON libraries and case class validation/creation
- Scala - No TypeTag Available Exception when using case class to try to get TypeTag?
- After installing Scala using MacPorts, scala command is not found
- Write a simple json REST server using spray in scala
More Query from same tag
- React Component not rending in Play app
- How to extract value from json in scala
- Parsing json in spark-streaming
- Get content from anyContent as Json
- How to convert Dataframe to Map with key as one of column value?
- Divide elements of column by a sum of elements (of same column) grouped by elements of another column
- How to test Peano numbers
- Why is Value String not a stable identifier even when defined as a val
- SBT jetty webapp - files from src are not copied into target folder - can't view simple HTML files i placed there
- How to remove null cases from a List(List(List())) in scala?
- I am trying to delete the data from postgres using spark but unable to delete same code is working for select statements
- How to add a column to the beginning of the schema?
- scala implicit or explicit conversion from iterator to iterable
- How to pass a case class into a function parameter and get a schema accordingly?
- What does "???" stand for in Scala lang?
- Transforming a future in Akka streams
- Are Options and named default arguments like oil and water in a Scala API?
- How to set an expected exception using Scala and JUnit 4
- Custom mkString function for Scala Iterator
- Actors case class simple example
- How to mix-in a construction-time code to a class with a trait in Scala?
- Hadoop Configuration in Spark
- Repeating function call until we'll get non-empty Option result in Scala
- Scala IDE Call Spark But Executor Not Start
- ScalaFX: How to convert an Image object to a byte array
- Dynamic column selection in Spark (based on another column's value)
- Shapeless type disjunction for more then 2 types
- Why is sortByKey so slow in spark? Is their any alternative for that?
- How to write in Scala shortly "filter first/last n elements satisfying a given function"?
- cat: /release: No such file or directory When running scala from Terminal or Scala itself