question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't serialize instances of class hierarchy with constructor parameters

See original GitHub issue

What is your use-case and why do you need this feature?

I have a web app that displays some complex reports. There is a class hierarchy of “queries” that I’d like to serialize to/from server. But something like this is not serializable:

abstract class Query(val type: QueryType) { ... }
abstract class QueryBase(type: QueryType) :Query(type) { ... }
class SpecificQuery1() : QueryBase(QueryType.Specific1) { ... }

The error is:

This class is not serializable automatically because it has primary constructor parameters of which are not properties

The type sort of is a property - it becomes a property once it’s passed up to the root class.

I put a custom serializer on QueryBase, with method bodies of TODO("not implemented"), because I’m not sure yet how I could do it, or if it’s possible.

@Serializable(with = QueryBaseSerializer::class)
abstract class QueryBase(queryType: QueryType)

I then get another error on SpecificQuery1, suggesting that even if I figured out how to write a serializer for QueryBase, I will have more problems.

Impossible to make this class serializable because its parent is not serializable and does not have exactly one constructor without parameters

Describe the solution you’d like

I’d like the auto-generated serializers to handle it. If that’s not possible, I’d like documentation on how to implement custom serializers to handle it. If I need to write custom serializers, it would be nice if there were a way to use some of the plugin’s code-generation abilities, so that I don’t have to remember to manually serialize each property and keep that code in sync if I add properties to a class. In other words, maybe the plugin can’t generate the whole serializer for me, but it could generate something to serialize the properties that it can handle, so I could call that from my serializer once I handled the custom part.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:19
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
sandwwraithcommented, Aug 12, 2020

Hi, Unfortunately, handling arbitrary arguments that are not properties is not an easy task — because they may be involved in arbitrary expressions that should be default values or superclass constructor arguments (e.g. you have class QueryBase(type: QueryType) :Query(type + "myQuery"))

We usually recommend follow this pattern when serializing class hierarchies: make vals abstract in base classes and override them in concrete classes, like this:

abstract class Query { 
    abstract val type: QueryType
}
abstract class QueryBase(override val type: QueryType) :Query() { ... }
6reactions
SeekDaSkycommented, May 13, 2020

I’d like to bump this feature request, it would be really useful in my case.

I have configuration files that are shared between multiple services. All services configurations have a lot of values in common, stored in one abstract class, then each service defines a subclass adding some specific properties.

It is not a polymorphic serialization in the sense that I know which subclass I am deserializing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

kotlinx.serialization - How to properly use class inheritance in ...
To make hierarchy of classes serializable, the properties in the parent class have to be marked abstract, making the [parent] class abstract ...
Read more >
Serializing a class hierarchy with constructor params? - Support
I'm trying to serialize some classes that look like: abstract class ... it has primary constructor parameters of which are not properties.
Read more >
How to serialize properties of derived classes with System ...
In this article. Serialize properties of derived classes; Polymorphic type discriminators; Configure polymorphism with the contract model ...
Read more >
Inheritance in Jackson | Baeldung
This tutorial will demonstrate how to handle inclusion of subtype metadata and ignoring properties inherited from superclasses with Jackson.
Read more >
Serialization in Java - DigitalOcean
Data class should provide writeReplace() method returning DataProxy instance. So when Data object is serialized, the returned stream is of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found