-
-
Notifications
You must be signed in to change notification settings - Fork 99
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 to completely disable discriminators #1103
Comments
@ColOfAbRiX Hi, Fabrizio! Thanks for the question! Unfortunately, there no simple direct solution for derivation of such codecs. For your case there is a couple of workaround options:
I will be happy to help you in writing of some acceptable solution based on concrete JSON samples and data structures. |
Thanks, probably the second of your suggestions feels the most appropriate, as I'm already working with the closest data structure to the api |
Also, to avoid XY problem, please give some context to understand why just using codecs for concrete case classes is not enough for your case? Why do you need extending of As example, usually doing REST we use different paths for different types of requests, so corresponding concrete class codecs could be easily pattern matched by paths, no need for generic one for an abstract class... |
You can use:
Example: #!/usr/bin/env -S scala-cli -j system -J -Xmx2g
//> using dep com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-core:2.31.1
//> using dep com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-macros:2.31.1
import com.github.plokhotnyuk.jsoniter_scala.core.*
import com.github.plokhotnyuk.jsoniter_scala.macros.*
sealed trait Base
case class A(a1: Int, a2: Int) extends Base
case class B(b1: Int, b2: Int) extends Base
given CodecMakerConfig.PrintCodec with {}
given JsonValueCodec[Seq[Base]] = JsonCodecMaker.make(CodecMakerConfig.withDiscriminatorFieldName(None))
val json = writeToString(Seq(A(1,2),B(3,4)))
println(json)
println(readFromString[Seq[Base]](json)) |
I'm writing a client in Scala 3 for a database that uses rest endpoints and I want to represent the requests internally using case classes which also include sealed traits/enum:
Because I'm interfacing with an existing service I cannot include any discriminator in the output json but I couldn't find how to. The only output I get is with a
type
field{ "type": "RequestA", "field1": "...", "field2": "..." }
or wrapped in another json object{ "RequestA": { "field1": "...", "field2": "..." } }
and none of these options are acceptable.I couldn't find if this is possible and how to do it.
The text was updated successfully, but these errors were encountered: