Skip to content

Commit

Permalink
Merge pull request #123 from tonicebrian/master
Browse files Browse the repository at this point in the history
Make `toJson` more JSONish
  • Loading branch information
eed3si9n authored Apr 11, 2018
2 parents 1dd8861 + cdb3fdb commit e1d2ace
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO

def toJsonLine: Seq[String] =
if (options contains BuildInfoOption.ToJson)
List(""" val toJson: String = toMap.map(i => "\"" + i._1 + "\":\"" + i._2 + "\"").mkString("{", ", ", "}")""")
List(
""" val toJson: String = toMap.map{ i =>
| def quote(x:Any) : String = "\"" + x + "\""
| val key : String = quote(i._1)
| val value : String = i._2 match {
| case elem : Seq[_] => elem.map(quote).mkString("[", ",", "]")
| case elem : Option[_] => elem.map(quote).getOrElse("null")
| case elem => quote(elem)
| }
| s"$key : $value"
| }.mkString("{", ", ", "}")""".stripMargin)
else Nil

}
13 changes: 11 additions & 2 deletions src/sbt-test/sbt-buildinfo/options/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ lazy val root = (project in file(".")).
""" val toMap: Map[String, Any] = Map[String, Any](""" ::
""" "name" -> name,""" ::
""" "scalaVersion" -> scalaVersion)""" ::
"" ::
""" val toJson: String = toMap.map(i => "\"" + i._1 + "\":\"" + i._2 + "\"").mkString("{", ", ", "}")""" ::
"""""" ::
""" val toJson: String = toMap.map{ i =>""" ::
""" def quote(x:Any) : String = "\"" + x + "\""""" ::
""" val key : String = quote(i._1)""" ::
""" val value : String = i._2 match {""" ::
""" case elem : Seq[_] => elem.map(quote).mkString("[", ",", "]")""" ::
""" case elem : Option[_] => elem.map(quote).getOrElse("null")""" ::
""" case elem => quote(elem)""" ::
""" }""" ::
""" s"$key : $value"""" ::
""" }.mkString("{", ", ", "}")""" ::
"""}""" :: Nil =>
case _ => sys.error("unexpected output: \n" + lines.mkString("\n"))
}
Expand Down

0 comments on commit e1d2ace

Please sign in to comment.