3 ms·
Play uses it specifically for in it’s JSON module; you can define readers and writers that allow you to parse the structure of a JSON object. To avoid having to
by Negative1 10y ago
Play uses it specifically for in it’s JSON module; you can define readers and writers that allow you to parse the structure of a JSON object. To avoid having to be very explicit, you can define a case class that directly mirrors the structure of that object. e.g.
given some case class that looks like this:
case class SomeObj(someInt: Int)
you could do:
val theObj = SomeObj((json \ “someInt”).as[Int])
or
implicit val reads: Reads[SomeObj] = ((json \ “someInt”).read[Int])(SomeObj.apply _)
with reflection, you get:
implicit reads: Reads[SomeObj]
val theObj = reads.validate(json)
Seems trivial with this small example but with large objects it keeps your code much more maintainable and consistent since reading and writing from that case class is the same.
As far as it not working in Scala.js, from my understanding it has to do with how the reflection library is shared between both runtime and compile time implementations.
You can read more about it here: http://docs.scala-lang.org/overviews/reflection/overview.html http://docs.scala-lang.org/overviews/reflection/overview.htm...
- bad_user 10y agoThe tricks that Play JSON does don't need runtime reflection. Macros don't use runtime capabilities at all and all macros work in Scala.js. I'm actually surprised by claims of Play JSON needing the runtime reflection of "scala.reflect". No, Play JSON doesn't work because it wraps Jackson, a Java library.
- grogs 10y agoYour correct that it probably doesn't use scala.reflect, but it still uses reflection. Jackson works using reflection. Play JSON therefore at least depends on reflection transatively.