| 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
This comment has been minimized.
Thanks for the example, it works, though it's a bit constraining anyway. I'm wondering if a macro would do the trick here...