Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct json for nested objects #126

Merged
merged 4 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,26 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO
def toJsonLine: Seq[String] =
if (options contains BuildInfoOption.ToJson)
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)
"""| private def quote(x: Any): String = "\"" + x + "\""
| private def toJsonValue(value: Any): String = {
| value match {
| case elem: Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")
| case elem: Option[_] => elem.map(toJsonValue).getOrElse("null")
| case elem: Map[String, Any] => elem.map {
| case (k, v) => toJsonValue(k) + ":" + toJsonValue(v)
| }.mkString("{", ", ", "}")
| case d: Double => d.toString
| case f: Float => f.toString
| case l: Long => l.toString
| case i: Int => i.toString
| case s: Short => s.toString
| case bool: Boolean => bool.toString
| case str: String => quote(str)
| case other => quote(other.toString)
| }
| }
|
| val toJson: String = toJsonValue(toMap)""".stripMargin)
else Nil

}
28 changes: 19 additions & 9 deletions src/sbt-test/sbt-buildinfo/options/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,26 @@ lazy val root = (project in file(".")).
""" "name" -> name,""" ::
""" "scalaVersion" -> scalaVersion)""" ::
"""""" ::
""" 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)""" ::
""" private def quote(x: Any): String = "\"" + x + "\""""" ::
""" private def toJsonValue(value: Any): String = {""" ::
""" value match {""" ::
""" case elem: Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]")""" ::
""" case elem: Option[_] => elem.map(toJsonValue).getOrElse("null")""" ::
""" case elem: Map[String, Any] => elem.map {""" ::
""" case (k, v) => toJsonValue(k) + ":" + toJsonValue(v)""" ::
""" }.mkString("{", ", ", "}")""" ::
""" case d: Double => d.toString""" ::
""" case f: Float => f.toString""" ::
""" case l: Long => l.toString""" ::
""" case i: Int => i.toString""" ::
""" case s: Short => s.toString""" ::
""" case bool: Boolean => bool.toString""" ::
""" case str: String => quote(str)""" ::
""" case other => quote(other.toString)""" ::
""" }""" ::
""" s"$key : $value"""" ::
""" }.mkString("{", ", ", "}")""" ::
""" }""" ::
"""""" ::
""" val toJson: String = toJsonValue(toMap)""" ::
"""}""" :: Nil =>
case _ => sys.error("unexpected output: \n" + lines.mkString("\n"))
}
Expand Down