summaryrefslogtreecommitdiff
path: root/core/src/main/scala/forge/JsonFormatters.scala
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2017-11-05 00:19:06 -0700
committerLi Haoyi <haoyi.sg@gmail.com>2017-11-05 00:19:06 -0700
commitdfe12f27063874ca6ff86dee98c85b82236e1762 (patch)
tree878ec8b496a5b14c33007962cf24da681a347374 /core/src/main/scala/forge/JsonFormatters.scala
parentcc4596f415c63bb9e03e07f714b6ce29e9cfe533 (diff)
downloadmill-dfe12f27063874ca6ff86dee98c85b82236e1762.tar.gz
mill-dfe12f27063874ca6ff86dee98c85b82236e1762.tar.bz2
mill-dfe12f27063874ca6ff86dee98c85b82236e1762.zip
Turn on `lihaoyi:acyclic` plugin, enforce it, and break up whatever import cycles exist
Diffstat (limited to 'core/src/main/scala/forge/JsonFormatters.scala')
-rw-r--r--core/src/main/scala/forge/JsonFormatters.scala47
1 files changed, 47 insertions, 0 deletions
diff --git a/core/src/main/scala/forge/JsonFormatters.scala b/core/src/main/scala/forge/JsonFormatters.scala
new file mode 100644
index 00000000..4e772128
--- /dev/null
+++ b/core/src/main/scala/forge/JsonFormatters.scala
@@ -0,0 +1,47 @@
+package forge
+
+import ammonite.ops.{Bytes, Path}
+import play.api.libs.json._
+object JsonFormatters extends JsonFormatters
+trait JsonFormatters {
+ implicit object pathFormat extends Format[ammonite.ops.Path]{
+ def reads(json: JsValue) = json match{
+ case JsString(v) => JsSuccess(Path(v))
+ case _ => JsError("Paths must be a String")
+ }
+ def writes(o: Path) = JsString(o.toString)
+ }
+
+ implicit object bytesFormat extends Format[Bytes]{
+ def reads(json: JsValue) = json match{
+ case JsString(v) => JsSuccess(
+ new Bytes(javax.xml.bind.DatatypeConverter.parseBase64Binary(v))
+ )
+ case _ => JsError("Bytes must be a String")
+ }
+ def writes(o: Bytes) = {
+ JsString(javax.xml.bind.DatatypeConverter.printBase64Binary(o.array))
+ }
+ }
+
+ implicit def EitherFormat[T: Format, V: Format] = new Format[Either[T, V]]{
+ def reads(json: JsValue) = json match{
+ case JsObject(struct) =>
+ (struct.get("type"), struct.get("value")) match{
+ case (Some(JsString("Left")), Some(v)) => implicitly[Reads[T]].reads(v).map(Left(_))
+ case (Some(JsString("Right")), Some(v)) => implicitly[Reads[V]].reads(v).map(Right(_))
+ case _ => JsError("Either object layout is unknown")
+ }
+ case _ => JsError("Either must be an Object")
+ }
+ def writes(o: Either[T, V]) = o match{
+ case Left(v) => Json.obj("type" -> "Left", "value" -> implicitly[Writes[T]].writes(v))
+ case Right(v) => Json.obj("type" -> "Right", "value" -> implicitly[Writes[V]].writes(v))
+ }
+ }
+
+ implicit lazy val crFormat: Format[ammonite.ops.CommandResult] = Json.format
+ implicit lazy val modFormat: Format[coursier.Module] = Json.format
+ implicit lazy val depFormat: Format[coursier.Dependency]= Json.format
+ implicit lazy val attrFormat: Format[coursier.Attributes] = Json.format
+}