Merge pull request #60 from dwijnand/additional-formats

Add back additional formats.
This commit is contained in:
eugene yokota 2016-11-28 15:22:47 -05:00 committed by GitHub
commit 613bf8b034
5 changed files with 67 additions and 1 deletions

View File

@ -17,6 +17,8 @@ def commonSettings: Seq[Setting[_]] = Seq(
scalacOptions ++= Seq("-Ywarn-unused", "-Ywarn-unused-import"),
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,
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"),
publishArtifact in Compile := true,
publishArtifact in Test := false

View File

@ -4,3 +4,4 @@ import sjsonnew.BasicJsonProtocol
object CacheImplicits extends BasicCacheImplicits
with BasicJsonProtocol
with HListFormat

View File

@ -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))
}
}
}

View File

@ -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)
}

View File

@ -36,7 +36,7 @@ object Dependencies {
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 sjsonnewScalaJson = "com.eed3si9n" %% "sjson-new-scalajson" % sjsonnewVersion
}