-
Notifications
You must be signed in to change notification settings - Fork 620
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
Inheritance/polymorphic serialization #427
Comments
How I really would like it to see is to be able to write the following:
And it works. I.e. be able to figure the properties from the parent class and apply them to the child class. Plus extra annotation to describe the discriminator property/polymorphic behavior. |
We have this goal in mind. Next release of the library will relax some restrictions, e.g. properties which do not have backing fields (an The one big limitation left is the requirement that @serializable class constructor should have only |
Has this ever worked?
because with 0.11.0 it produces :
|
Inheritance doesn't seem to work at all...
==> IllegalStateException: Class Apricot have constructor parameters which are not properties and therefore it is not serializable automatically AND
==> IllegalStateException: class Apricot has duplicate serial name of property name, either in it or its parents. |
Any updates on this topic ? |
Bit of a hijack, but is there a current way to support just the The current behavior of forcing a class name means you need to expose class names to your consumers..? Something like Jacksons |
Oh great! That’s a well hidden nugget, and here was me digging around the code of the builder 🙄 Thanks! |
@sandwwraith: Are there plans to support inheritance/polymorphism with (Apologies for all the pings; you seem to be the main contributor who's dealt with some of these older issues.) I stumbled across this issue in the midst of creating a new one. I'll copy and paste the content from that into here:
|
The problem with Btw, what exact error do you have with |
I'm running into a "usability" issue with inheritance that is similar to the one in the first message. Based on the current design, I need to create a
Example: @Serializable(with = ...) // uses a `JsonContentPolymorphicSerializer`
sealed class MyBaseClass {
val property: String? = null
}
@Serializable data class A(val anotherProperty: String) : MyBaseClass()
@Serializable data class B(val aProperty: Float) : MyBaseClass() The above fails decoding of a
To make it work, I'd have to override |
i'm encountering the same issue, i'm using a class that inherits from an abstract class but with with its own variables (they aren't open nor abstract), i can't alter the super class since its from a library, but i do need the variables to be encoded aswell, any suggestions? |
Don't you think that it would be logical to prefer child property to parent? After all, it's overriden for purpose. |
I have the following hierarchy:
And it already looks "ugly" in order to do the proper serialization, i.e. have to mark parent properties as
open
, andoverride
them (in order to make them properties, as required by the serialization library) in the children.Also in order to not have parent properties duplicated in the output JSON, have to mark them
@Transient
, but it then requires to set the default values (where there are no really meaningful default values).Only then it produces the proper JSON, like the one below:
Although when deserialize the JSON back to the class, the parent properties are set to the default values instead of the values from the child class.
And moreover don't know how to do the polymorphic deserialization, i.e. using the discriminator value, like "type" property.
Is then this library suitable for the above use case, or it is the ultimate end goal, but it is not ready yet to handle this?
Or this library was specifically designed in mind to work only with
data
classes?Here is the unit test to produce the JSON above:
The text was updated successfully, but these errors were encountered: