Scala 2.10, its impact on JSON libraries and case class validation/creation

前端 未结 3 2018
悲哀的现实
悲哀的现实 2021-02-01 17:35

In Scala 2.10 apparently we\'re getting improved reflection.

How will this impact lift-json, jerkson, sjson and friends? Furthermore, can we expect in the not too distan

3条回答
  •  余生分开走
    2021-02-01 18:17

    Forewords

    Let me give a different solution that doesn't rely on any Java based library but only a pure Scala one.

    Actually, as discussed in the comments of @Steve's results Play 2's scala version was using Jerkson for de/serializing Json to domain model. Where Jerkson is a DSL wrapper around a very good Java library for handling Json.

    Answer

    The above wasn't answering your question, since you were asking if it has been envisioned to used the reflection and the macro features of Scala 2.10 to ease this task!!!! By eliminating most boilerplates.

    And it was a very good thought in fact because from the Play 2.1 version, the Json Scala API is not using Jerkson anymore but it's own mechanism.

    This mechanism is in fact taking advantages of this new 2.10 version of Scala by introducing a brand new API based on two things:

    • a functional construction (Applicative Builder) adapted to be able to Read and Write Json or Domain instances. These builders are used to glue altogether combinators (for both read or write) in order to define coarse grained structured (like we do with Parser Combinators)
    • a bunch of macros that are able to discover which combinators are implicitly available and will construct complex ones for Case Classes (or at least types that have apply and unapply methods).

    In the end of the day, here is what we could do by using this API:

    import play.api.libs.json._
    import play.api.libs.functional.syntax._
    
    case class Person(name: String, age: Int, lovesChocolate: Boolean)
    
    implicit val personReads = Json.format[Person] //a format is a Reader and a Writer
    
    //this format will be implicitly used by the following from/toJson functions
    val person:JsResult[Person] = Json.fromJson(json) //JsResult is like JsSucces/JsError
    val jsObject = Json.toJson(person)
    

    code copied and adapted from: JSON Inception (Based on Scala 2.10 Macros)

    A little note: the new API is even smart enough to be able to validate a "read" by accumulating errors...

    References

    There are a series of blogs from @mandubian that I'd recommend from here, because they are very enlightening it!

    • JsPath & Reads Combinators: http://mandubian.com/2012/09/08/unveiling-play-2-dot-1-json-api-part1-jspath-reads-combinators/
    • Writes/Format Combinators: http://mandubian.com/2012/10/01/unveiling-play-2-dot-1-json-api-part2-writes-format-combinators/
    • JSON Transformers: http://mandubian.com/2012/10/29/unveiling-play-2-dot-1-json-api-part3-json-transformers/
    • JSON Inception (Based on Scala 2.10 Macros) : http://mandubian.com/2012/11/11/JSON-inception/

    Final note

    What is sad is that the modularization of Play 2... doesn't allow us to use this API alone! So, it should be used from the play lib as a whole :/ This might change in the future...

    EDIT

    And the future is getting closer now, since Pascal has this repo enabling us to use the play-json API.

    So, one can use this repo until Play 2.2 will be released. Indeed, this version will be completely decoupled with several APIs like json or iteratees, and thus, we'll be able to use the playframework repo directly.

提交回复
热议问题