Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected json string interpolator from io.circe.literal to be able to interpolate variable inside of value strings #2246

Open
caenrique opened this issue Apr 4, 2024 · 2 comments

Comments

@caenrique
Copy link

caenrique commented Apr 4, 2024

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

@caenrique
Copy link
Author

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 😄

@CJSmith-0141
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants