playjson-extended


License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.adelegue
ArtifactId

ArtifactId

playjson-extended_2.12
Last Version

Last Version

0.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

playjson-extended
playjson-extended
Project URL

Project URL

https://github.com/larousso/playjson-extended
Project Organization

Project Organization

com.adelegue
Source Code Management

Source Code Management

https://github.com/larousso/playjson-extended

Download playjson-extended_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.adelegue/playjson-extended_2.12/ -->
<dependency>
    <groupId>com.adelegue</groupId>
    <artifactId>playjson-extended_2.12</artifactId>
    <version>0.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.adelegue/playjson-extended_2.12/
implementation 'com.adelegue:playjson-extended_2.12:0.0.4'
// https://jarcasting.com/artifacts/com.adelegue/playjson-extended_2.12/
implementation ("com.adelegue:playjson-extended_2.12:0.0.4")
'com.adelegue:playjson-extended_2.12:jar:0.0.4'
<dependency org="com.adelegue" name="playjson-extended_2.12" rev="0.0.4">
  <artifact name="playjson-extended_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.adelegue', module='playjson-extended_2.12', version='0.0.4')
)
libraryDependencies += "com.adelegue" % "playjson-extended_2.12" % "0.0.4"
[com.adelegue/playjson-extended_2.12 "0.0.4"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.4
com.typesafe.play : play-json_2.12 jar 2.6.5
com.chuusai : shapeless_2.12 jar 2.3.2

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.0.4

Project Modules

There are no modules declared in this project.

Play extension

travis-badge bintray-badge

Usage

resolvers += Resolver.bintrayRepo("larousso", "maven")

libraryDependencies += "com.adelegue" %% "playjson-extended" % "X.X.X"

Rules

With play json you can write this :

    import play.api.libs.json._
    
    case class Village(name: String)
    
    object Village {
      implicit val reads = Json.reads[Village]
    }
    
    case class Viking(name: String, surname: String, weight: Int, village: Seq[Village])

    implicit val reads = Json.reads[Viking]

And if you want to add advanced validation you have to write this

    import play.api.libs.json._
    import play.api.libs.json.Reads._
    import play.api.libs.functional.syntax._
    
    case class Village(name: String)
    
    object Village {
     implicit val reads = Json.reads[Village]
    }
    
    case class Viking(name: String, surname: String, weight: Int, village: Seq[Village])
    
    implicit val reads = (
        (__ \ 'name).read[String] and 
        (__ \ 'surname).read[String] and 
        (__ \ 'weight).read[Int](min(0) keepAnd max(150)) and 
        (__ \ 'village).read[Seq[Village]]
    )(Viking.apply _)

Here we have to write all the parsing rules just to validate weight.

With this lib you can just write

    import play.api.libs.json._
    import play.api.libs.json.Reads._
    import play.api.libs.functional.syntax._
    import shapeless._
    import syntax.singleton._
    import playjson.rules._
  
    case class Village(name: String)
    
    object Village {
      implicit val reads = Json.reads[Village]
    }
    
    case class Viking(name: String, surname: String, weight: Int, village: Seq[Village])
    
    implicit val reads: Reads[Viking] = jsonRead[Viking].withRules(
      'weight ->> read(min(0) keepAnd max(150))
    )

You can just generate a Reads[T] using shapeless instead of scala macro

    import play.api.libs.json._
    import playjson.reads._
    
    val monMojoReads: Reads[Viking] = hReads[Viking]

Transformations

Transform json before converting to case class :

    import play.api.libs.json._
    import play.api.libs.json.Reads._
    import play.api.libs.functional.syntax._
    import shapeless._
    import syntax.singleton._
    import playjson.rules._
    import playjson.transformation._
    
    case class Village(name: String)
    
    object Village {
      implicit val reads = Json.reads[Village]
    }
    
    case class Viking(name: String, surname: String, weight: Int, village: Seq[Village])
        
    val reads: Reads[Viking] =
    transform(
        ((__ \ 'theName) to (__ \ 'name)) and
        ((__ \ 'theSurname) to (__ \ 'surname)) and
        ((__ \ 'theVillage) to (__ \ 'village))
    ) andThen Json.reads[Viking]
    
    val jsResult: JsResult[Viking] = reads.reads(Json.obj(
        "theName" -> "Ragnar",
        "theSurname" -> "Lothbrok",
        "theVillage" -> Seq(Json.obj("name" -> "Kattegat")),
        "weight" -> 2
    ))

You can combine both rules and transformations

      val reads: Reads[Viking] =
        transform(
            ((__ \ 'theName) to (__ \ 'name)) and
            ((__ \ 'theSurname) to (__ \ 'surname)) and
            ((__ \ 'theVillage) to (__ \ 'village))
        ) andThen jsonRead[Viking].withRules(
          'weight ->> read(min(0) keepAnd max(150)) and
          'name ->> read(pattern(".*".r)) and
          'surname ->> read(pattern(".*".r))
        )

Or use reads generated from shapeless LabelledGeneric :

    val reads: Reads[Viking] =
        transform(
          ((__ \ 'theName) to (__ \ 'name)) and
            ((__ \ 'theSurname) to (__ \ 'surname)) and
            ((__ \ 'theVillage) to (__ \ 'village))
        ) andThen hReads[Viking]

Release

sbt "release with-defaults" 

Versions

Version
0.0.4