Skip to content

Instantly share code, notes, and snippets.

@propensive
Created November 26, 2014 17:42
Show Gist options
  • Save propensive/a37ee0df7822457ac630 to your computer and use it in GitHub Desktop.
Save propensive/a37ee0df7822457ac630 to your computer and use it in GitHub Desktop.
package interpolate
import rapture.json._
import jsonBackends.jawn._
object Interpolation {
case class Forced(json: Json)
implicit def toForced[T: JsonSerializer](t: T): Forced = Forced(Json(t))
implicit class Interpolator(sc: StringContext) {
def interpolate(parts: Forced*): Json = {
// just maps the fixed parts of the string to the variable bits that come after, ignoring the last
Json((sc.parts.map(_.trim) zip parts.map(_.json)).toMap)
}
}
}
object Test {
import Interpolation._
val json = interpolate"""
foo ${ 42 }
bar ${ "Hello world!" }
"""
}
@byF
Copy link

byF commented Nov 27, 2014

Thanks for the example, it works, though it's a bit constraining anyway. I'm wondering if a macro would do the trick here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment