mirror of https://github.com/sbt/sbt.git
Merge pull request #60 from dwijnand/additional-formats
Add back additional formats.
This commit is contained in:
commit
613bf8b034
|
|
@ -17,6 +17,8 @@ def commonSettings: Seq[Setting[_]] = Seq(
|
||||||
scalacOptions ++= Seq("-Ywarn-unused", "-Ywarn-unused-import"),
|
scalacOptions ++= Seq("-Ywarn-unused", "-Ywarn-unused-import"),
|
||||||
scalacOptions --= // scalac 2.10 rejects some HK types under -Xfuture it seems..
|
scalacOptions --= // scalac 2.10 rejects some HK types under -Xfuture it seems..
|
||||||
(CrossVersion partialVersion scalaVersion.value collect { case (2, 10) => List("-Xfuture", "-Ywarn-unused", "-Ywarn-unused-import") }).toList.flatten,
|
(CrossVersion partialVersion scalaVersion.value collect { case (2, 10) => List("-Xfuture", "-Ywarn-unused", "-Ywarn-unused-import") }).toList.flatten,
|
||||||
|
scalacOptions in console in Compile -= "-Ywarn-unused-import",
|
||||||
|
scalacOptions in console in Test -= "-Ywarn-unused-import",
|
||||||
previousArtifact := None, // Some(organization.value %% moduleName.value % "1.0.0"),
|
previousArtifact := None, // Some(organization.value %% moduleName.value % "1.0.0"),
|
||||||
publishArtifact in Compile := true,
|
publishArtifact in Compile := true,
|
||||||
publishArtifact in Test := false
|
publishArtifact in Test := false
|
||||||
|
|
|
||||||
|
|
@ -4,3 +4,4 @@ import sjsonnew.BasicJsonProtocol
|
||||||
|
|
||||||
object CacheImplicits extends BasicCacheImplicits
|
object CacheImplicits extends BasicCacheImplicits
|
||||||
with BasicJsonProtocol
|
with BasicJsonProtocol
|
||||||
|
with HListFormat
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package sbt.internal.util
|
||||||
|
|
||||||
|
import sjsonnew._
|
||||||
|
import Types.:+:
|
||||||
|
|
||||||
|
trait HListFormat {
|
||||||
|
implicit val lnilFormat1: JsonFormat[HNil] = forHNil(HNil)
|
||||||
|
implicit val lnilFormat2: JsonFormat[HNil.type] = forHNil(HNil)
|
||||||
|
|
||||||
|
private def forHNil[A <: HNil](hnil: A): JsonFormat[A] = new JsonFormat[A] {
|
||||||
|
def write[J](x: A, builder: Builder[J]): Unit = {
|
||||||
|
if (builder.state != BuilderState.InArray) builder.beginArray()
|
||||||
|
builder.endArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): A = {
|
||||||
|
if (unbuilder.state == UnbuilderState.InArray) unbuilder.endArray()
|
||||||
|
hnil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
implicit def hconsFormat[H, T <: HList](implicit hf: JsonFormat[H], tf: JsonFormat[T]): JsonFormat[H :+: T] =
|
||||||
|
new JsonFormat[H :+: T] {
|
||||||
|
def write[J](hcons: H :+: T, builder: Builder[J]) = {
|
||||||
|
if (builder.state != BuilderState.InArray) builder.beginArray()
|
||||||
|
hf.write(hcons.head, builder)
|
||||||
|
tf.write(hcons.tail, builder)
|
||||||
|
}
|
||||||
|
|
||||||
|
def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]) = jsOpt match {
|
||||||
|
case None => HCons(hf.read(None, unbuilder), tf.read(None, unbuilder))
|
||||||
|
case Some(js) =>
|
||||||
|
if (unbuilder.state != UnbuilderState.InArray) unbuilder.beginArray(js)
|
||||||
|
HCons(hf.read(Some(unbuilder.nextElement), unbuilder), tf.read(Some(js), unbuilder))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
package sbt.internal.util
|
||||||
|
|
||||||
|
import scala.json.ast.unsafe._
|
||||||
|
import sjsonnew._, support.scalajson.unsafe._
|
||||||
|
import CacheImplicits._
|
||||||
|
|
||||||
|
class HListFormatSpec extends UnitSpec {
|
||||||
|
val quux = 23 :+: "quux" :+: true :+: HNil
|
||||||
|
|
||||||
|
it should "round trip quux" in assertRoundTrip(quux)
|
||||||
|
it should "round trip hnil" in assertRoundTrip(HNil)
|
||||||
|
|
||||||
|
it should "have a flat structure for quux" in assertJsonString(quux, """[23,"quux",true]""")
|
||||||
|
it should "have a flat structure for hnil" in assertJsonString(HNil, "[]")
|
||||||
|
|
||||||
|
def assertRoundTrip[A: JsonWriter: JsonReader](x: A) = {
|
||||||
|
val jsonString: String = toJsonString(x)
|
||||||
|
val jValue: JValue = Parser.parseUnsafe(jsonString)
|
||||||
|
val y: A = Converter.fromJson[A](jValue).get
|
||||||
|
assert(x === y)
|
||||||
|
}
|
||||||
|
|
||||||
|
def assertJsonString[A: JsonWriter](x: A, s: String) = assert(toJsonString(x) === s)
|
||||||
|
|
||||||
|
def toJsonString[A: JsonWriter](x: A): String = CompactPrinter(Converter.toJson(x).get)
|
||||||
|
}
|
||||||
|
|
@ -36,7 +36,7 @@ object Dependencies {
|
||||||
|
|
||||||
lazy val parserCombinator211 = "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.4"
|
lazy val parserCombinator211 = "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.4"
|
||||||
|
|
||||||
lazy val sjsonnewVersion = "0.4.2"
|
lazy val sjsonnewVersion = "0.5.1"
|
||||||
lazy val sjsonnew = "com.eed3si9n" %% "sjson-new-core" % sjsonnewVersion
|
lazy val sjsonnew = "com.eed3si9n" %% "sjson-new-core" % sjsonnewVersion
|
||||||
lazy val sjsonnewScalaJson = "com.eed3si9n" %% "sjson-new-scalajson" % sjsonnewVersion
|
lazy val sjsonnewScalaJson = "com.eed3si9n" %% "sjson-new-scalajson" % sjsonnewVersion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue