Expected `json` string interpolator from io.circe.literal to be able to interpolate variable inside of value strings
caenrique opened this issue · comments
I'm having some issues with the behaviour of the json string interpolator.
I was expecting this to compile:
//> using toolkit typelevel:0.1.25
//> using dep io.circe::circe-literal::0.14.6
//> using scala 3.3.3
import io.circe.literal.*
val value = 5
val json = json"""{
"a": "something with value: $value"
}"""
But, instead I had to resort to extracting the whole string from the right hand side of a
and use regular string interpolation there:
//> using toolkit typelevel:0.1.25
//> using dep io.circe::circe-literal::0.14.6
//> using scala 3.3.3
import io.circe.literal.*
val value = 5
val valueString = s"something with value: $value"
val json = json"""{
"a": $valueString
}"""
Is this expected behaviour? I think this wasn't the case in scala 2
This seems to work:
//> using toolkit typelevel:0.1.25
//> using dep io.circe::circe-literal::0.14.6
//> using scala 3.3.3
import io.circe.literal._
val value = 5
val json = json"""{
"a": ${s"something with value: $value"}
}"""
I've check using scala 2.13 and the behaviour is the same, so I was wrong about that. I'm curious to know if this is a bug, a technical limitation or a design decision though 😄
I think that they way this works is the macro replacements happen before parsing the string into the io.circe.Json
AST. So the macro turns this
val value = "5"
val json = json"""{
"a": "something with value: $value"
}"""
into this
val jsonString = """{
"a": "something with value: "5""
}"""
The inner "5"
doesn't have the "
escaped, so it's not valid json. The parsing fails at this point.