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

store generated codecs as source in src_managed #1122

Open
domdorn opened this issue Feb 26, 2024 · 2 comments
Open

store generated codecs as source in src_managed #1122

domdorn opened this issue Feb 26, 2024 · 2 comments
Labels

Comments

@domdorn
Copy link

domdorn commented Feb 26, 2024

Hi,

at the moment, jsoniter-scala doesn't store the generated macro code. Debugging using

implicit val printCodec: CodecMakerConfig.PrintCodec = new CodecMakerConfig.PrintCodec {}

is hard as this gets printed everywhere the codec is used.
It would be way easier to debug codec issues, if jsoniter-scala would simply put the generated code in e.g. target/src_managed/ or some place similar and I could simply open that file with my IDE and see the generated code.

Is this possible somehow?

Thanks,
Dominik

@plokhotnyuk
Copy link
Owner

Hi Dominik,

I don't know yet if it possible to generate source files from macros in a way that is suitable for debugging.

Have you tried to limit a scope where implicit val printCodec: CodecMakerConfig.PrintCodec is defined?

As example for some nested data structure you can define that implicit value only for a codec of some sub-structure:

case class Nested(s: String)
case class TopLevel(nested: Nested)

implicit val topLevelCodec: JsonValueCodec[TopLevel] = {
  implicit val nestedCodec: JsonValueCodec[Nested] = {
    implicit val printCodec: CodecMakerConfig.PrintCodec = new CodecMakerConfig.PrintCodec {}
    JsonCodecMaker.make[Nested]
  }

  JsonCodecMaker.make[TopLevel]
}

Also, I'm interested about a context to understand why debugging of generated codecs is required and just unit testing is not enough to solve your issue.

@domdorn
Copy link
Author

domdorn commented Feb 27, 2024

as for context, one example:
We have a case class Timestamp(value: Long) class that in some services / for some calls has to be serialized as millis-since-epoch, in others as ISO-String. We have two codecs for the corresponding serialization. If we forget to bring the right codec in scope, the timestamp gets serialized as an object {"value": 1234..} which is the worst encoding/unwanted.
We usually define most of the codecs in a object JsonSupport { ... } .. sometimes its necessary to see which codec is actually used or if jsoniter-scala even creates a new codec cause non of the existing ones is in scope.

The workaround with scoping you shared in the last comment definetly will make it easier to debug issues, thanks!

e.g.:

case class Sample(t: Timestamp)

one encoding:

{"t": 1709038343000}

other encoding:

{"t": "2024-02-27T13:52:27.000Z"}

encoding if we forget to put the right codec in scope (unwanted):

{"t": { "value": 1709038343000 }}

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

No branches or pull requests

2 participants