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

Excluding type tag from json #77

Open
arshadalis opened this issue Feb 23, 2021 · 4 comments
Open

Excluding type tag from json #77

arshadalis opened this issue Feb 23, 2021 · 4 comments

Comments

@arshadalis
Copy link

arshadalis commented Feb 23, 2021

Is there a way to exclude the discriminator type tag from the output json.
For example

sealed trait Foo
case class Bar(s :String, w: Long) extends Foo
case class Baz(q : String, p: Int) extends Foo

the output json should be
lets say for Bar

{
"s": "someabc"
"w": 12
}

i.e. It should not include "_type": "Bar" in the json.

I found following are the ways to customise the type tag

TypeTagSetting.ShortClassName
TypeTagSetting.FullClassName
TypeTagSetting.UserDefinedName

Is there a way to disable it ?

@julienrf
Copy link
Owner

How would you deserialize such JSON blobs? I mean, how would you detect which concrete class, Bar or Baz you should construct?

@julienrf
Copy link
Owner

One way to disable it is to have distinct formats, one format for Bar, and one format for Baz (but no format for Foo)

@vangogiel
Copy link

I can relate to this issue. You could have another TypeTagOWrites function, e.g.

    def default(): TypeTagOWrites =
      new TypeTagOWrites {
        def owrites[A](typeName: String, owrites: OWrites[A]): OWrites[A] =
          OWrites[A](a => owrites.writes(a))
      }

In my case, I want to have all my case classes with different parameters written with one implicit. I do not want to have multiple implicits. However, I want to also add other fields from my sealed trait. I want that to be done in a specific order and different than provided with the "flat" implicit.

I want other fields between my "typeName" and the owrites.writes(a)).

I tested it locally and it works. I can provide the code.

@vangogiel
Copy link

Unless there is a different solution provided with your library that I am unable to see.

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

3 participants