This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from OlivierBlanvillain/json-ast
Replace json4s-ast with custom AST
- Loading branch information
Showing
25 changed files
with
375 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ notifications: | |
email: | ||
false | ||
|
||
script: bash misc/ci.sh | ||
script: bash scripts/ci.sh | ||
|
||
cache: | ||
directories: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
package controllers | ||
|
||
import jto.validation.playjson.Writes | ||
import jto.validation.Write | ||
import jto.validation._ | ||
import jto.validation.jsonast._ | ||
import play.api.Environment | ||
import play.api.libs.json._ | ||
import play.api.mvc._ | ||
|
||
import model.User | ||
|
||
class Application()(implicit environment: Environment) extends Controller { | ||
|
||
def index = Action { | ||
import Writes._ | ||
val write: Write[User, JsObject] = Write.gen[User, JsObject] | ||
val write: Write[User, JsValue] = Write.toWrite(User.format) andThen Ast.to | ||
val user: User = User("supercat", 20, Some("e@mail.com"), true) | ||
val json: String = Json.prettyPrint(write.writes(user)) | ||
Ok(views.html.index(json)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
package model | ||
|
||
import jto.validation._ | ||
import jto.validation.jsonast._ | ||
import scala.Function.unlift | ||
|
||
case class User( | ||
name: String, | ||
age: Int, | ||
email: Option[String], | ||
isAlive: Boolean | ||
) | ||
|
||
object User { | ||
import Rules._, Writes._ | ||
implicit val format: Format[JValue, JObject, User] = | ||
Formatting[JValue, JObject] { __ => | ||
( | ||
(__ \ "name").format(notEmpty) ~ | ||
(__ \ "age").format(min(0) |+| max(130)) ~ | ||
(__ \ "email").format(optionR(email), optionW(stringW)) ~ | ||
(__ \ "isAlive").format[Boolean] | ||
)(User.apply, unlift(User.unapply)) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package jto.validation | ||
package jsonast | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.JSConverters._ | ||
|
||
object Ast { | ||
val to: Write[JValue, js.Dynamic] = Write[JValue, js.Any] { | ||
case JNull => null | ||
case JObject (value) => value.mapValues(to.writes).toJSDictionary | ||
case JArray (value) => value.map(to.writes).toJSArray | ||
case JBoolean(value) => value | ||
case JString (value) => value | ||
case JNumber (value) => | ||
val d = value.toDouble | ||
if (d.isNaN || d.isInfinity) null else d | ||
}.map(_.asInstanceOf[js.Dynamic]) | ||
|
||
private val undefined = scala.scalajs.js.undefined | ||
private case class FunctionInJsonException(path: Path) extends Exception | ||
|
||
private def unsafeAny2JValue(input: Any, path: Path): JValue = input match { | ||
case null => JNull | ||
case s: String => JString(s) | ||
case b: Boolean => JBoolean(b) | ||
case d: Double => JNumber(d.toString) | ||
case `undefined` => JNull | ||
|
||
case a: js.Array[js.Dynamic @unchecked] => | ||
JArray(a.map(v => unsafeAny2JValue(v, path \ 0))) | ||
|
||
case o: js.Object => | ||
JObject(o.asInstanceOf[js.Dictionary[js.Dynamic]] | ||
.map { case (k, v) => k -> unsafeAny2JValue(v, path \ k) }.toMap) | ||
|
||
case _ => | ||
// This is a trade off between the various option to handle js.Function in json objects. | ||
// We could also go one step further and return all the paths which contain functions, | ||
// but this would imply sequence over Validated, which would throw away the perfs in | ||
// the general case. | ||
// | ||
// This is what other are doing: | ||
// - The native JSON.stringity is completely silent. | ||
// - Circe parses then as nulls https://goo.gl/iQ0ANV. | ||
throw new FunctionInJsonException(path) | ||
} | ||
|
||
val from: Rule[js.Dynamic, JValue] = Rule { j => | ||
try { | ||
Valid(unsafeAny2JValue(j, Path)) | ||
} catch { | ||
case FunctionInJsonException(path) => | ||
Invalid(Seq(path -> Seq(ValidationError("Json cannot contain functions.")))) | ||
} | ||
} | ||
} |
Oops, something went wrong.