Created
November 26, 2014 17:42
-
-
Save propensive/a37ee0df7822457ac630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!" } | |
| """ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example, it works, though it's a bit constraining anyway. I'm wondering if a macro would do the trick here...