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

How do I encode an enum in circe style? #1014

Closed
krux02 opened this issue Apr 21, 2023 · 3 comments
Closed

How do I encode an enum in circe style? #1014

krux02 opened this issue Apr 21, 2023 · 3 comments
Labels

Comments

@krux02
Copy link

krux02 commented Apr 21, 2023

Currently I have an enum that is stored in the database like this {"EnumValue":{}} instead of "EnumValue" or {"type":"EnumValue"}. This style comes from circe serialization. Is there an easy option to get this style of serilization with jsoniter as well? I know by now how I can write my own custom serializer, but maybe there is an easier way to make this happen by default with more types.

I have another small question. I've tried several times to call JsoniterCodecMaker.make[MyType](myConfig), the function does exist, but I get weird compilation errors about compilation that I don't understand. I didn't figure out yet how to actually pass a config to make. Is there an explanation for that?

@plokhotnyuk
Copy link
Owner

plokhotnyuk commented Apr 24, 2023

Hi, Arne! Thanks for your feedback again!

There are a couple of special macros for derivation of codecs that are compatible with circe: JsoniterCodecMaker.makeCirceLike and JsoniterCodecMaker.makeCirceLikeSnakeCased.

But currently they do not work properly with Scala objects. I'm going to fix it and cut a new release ASAP.

Using those macros should help you to avoid unexpected compilation errors that are described in the 2nd item of the Know Issues list: https://github.com/plokhotnyuk/jsoniter-scala#known-issues

plokhotnyuk added a commit that referenced this issue Apr 24, 2023
@plokhotnyuk
Copy link
Owner

plokhotnyuk commented Apr 24, 2023

@krux02 Please pick the v2.23.0 release and use JsoniterCodecMaker.makeCirceLike or JsoniterCodecMaker.make(CodecMakerConfig.withCirceLikeObjectEncoding(true).withDiscriminatorFieldName(None)).

To reuse the same configuration for different make calls you can use techniques described here.

@krux02
Copy link
Author

krux02 commented Apr 24, 2023

Thanks a lot, this really helps.

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