From cdb3fdb6287ae602fd72eedd4d5885ae5f351f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Cebri=C3=A1n?= Date: Tue, 10 Apr 2018 17:26:39 +0200 Subject: [PATCH] Make toJson more JSONish --- .../sbtbuildinfo/ScalaCaseObjectRenderer.scala | 12 +++++++++++- src/sbt-test/sbt-buildinfo/options/build.sbt | 13 +++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala b/src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala index 85cf0c7..e763faa 100644 --- a/src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala +++ b/src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala @@ -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 } diff --git a/src/sbt-test/sbt-buildinfo/options/build.sbt b/src/sbt-test/sbt-buildinfo/options/build.sbt index e2a7b1c..e59dfbc 100644 --- a/src/sbt-test/sbt-buildinfo/options/build.sbt +++ b/src/sbt-test/sbt-buildinfo/options/build.sbt @@ -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")) }