zio / zio-json

Fast, secure JSON library with tight ZIO integration.
https://zio.dev/zio-json
Apache License 2.0
407 stars 146 forks source link

Is there a way to manually change field names without using annotations? #147

Closed Lando-L closed 1 year ago

Lando-L commented 3 years ago

It would be great to be able to manually change field names without using annotations. This way it would be easier to separate a case class and its serialization.

fsvehla commented 3 years ago

@Lando-L Are you thinking of an approach like passing params to deriveJsonDecoder instead?

Lando-L commented 3 years ago

@fsvehla Yes, that could be an option. Maybe even allowing a name and a decoder, in case, a specific decoder is needed. E.g. there is a case class Post(id: Long, message: Array[Byte], image: Array[Byte]) and we would like to decode it from JSON strings looking like this {id: 1234, message_bytes: ..., image_bytes: ...}, with message_bytes and image_bytes having Base58 and Base64 encoding respectively. In this case we could pass the parameters (message="message_bytes" -> Base58Decoder, image="image_bytes" -> Base64Decoder).

This is probably a separate enhancement, but I see them related.

AliceCayla commented 2 years ago

I know this is quite old, but I would be very interested in this. It would be especially useful if we could combine it with the change in #527 to support different types of caseing without having to annotate each class. It could be done either with arguments passed to deriveJsonDecoder or with a Configuration class similar to what circe offers.

The context for this is that we are trying out using zio-http with zio-json in our company. For historical reasons we need use snake case in json responses from our services, but the case classes that are used to generate these responses are all using camel case. These case classes are in a common library used by many teams and we don't want to use annotations because we don't want to force zio-json in their codabses. Also, there are dozens of classes, so adding annotations to all of them is not feasible and error prone. This is something that blocks us from adopting zio-json

vasiliybondarenko commented 2 years ago

Any objections if I'll dig in and try to create a PR for this issue (I'm also struggled of missing such feature)?

lachezar commented 1 year ago

I am also looking for this functionality or at least be able to auto derive codec with configuration for pascal case, snake case, etc. and possibly also a way to transform the keys with a custom function that you can pass.

I've stumbled upon this comment from a PR from more than an year ago and I wonder if/when this functionality will be available. https://github.com/zio/zio-json/blob/v0.3.0/zio-json-macros/shared/src/main/scala/zio/json/jsonDerive.scala#L28

ilinandrii commented 1 year ago

Upvote this issue as well. I want to have my case classes without json dependency in separate compilation unit and then have something like JsonSupport trait that will provide json codecs for them on demand. Configuration approach sounds more flexible than annotations.