-
Notifications
You must be signed in to change notification settings - Fork 634
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
Creating serial descriptor when serializing to heterogeneous lists #2899
Comments
Hm, why do you need exactly |
I'm not sure, I understood your question correctly. I want to serialize a To serialize/deserialize a JSON array, the |
I just noticed that I made a mistake in the example.
|
Thanks for the clarifications. Yes, if you want |
@odzhychko Note that (in the case of Json) you could use a |
@pdvrieze Thanks for the suggestion, but I do not understand how to apply it. Could you perhaps elaborate further? I found the documentation for |
@odzhychko For efficiency, there is actually a much simpler solution. Just replace your descriptor with the following: override val descriptor = ListSerializer(ContextualSerializer(Any::class)).descriptor This works correctly for the Json format (even when providing the actual descriptors to The reason to go for Contextual is that this is a special type of serializerKind that indicates that it is resolved at runtime (and can not be cached). |
That worked. Thanks for the explanation. |
What is your use-case and why do you need this feature?
I want to serialize a class as a JSON array. The elements can have different types.
For that, I created a custom serializer. When building the corresponding
SerialDescriptor
I need to use the internalbuildSerialDescriptor
builder because the publicbuildClassSerialDescriptor
does not cover this use-case.The following shows a simplified example:
Describe the solution you'd like
listSerialDescriptor
but for heterogeneous lists,buildSerialDescriptor
orbuildListSerialDescriptor
builder similar tobuildClassSerialDescriptor
andbuildSerialDescriptor
.The text was updated successfully, but these errors were encountered: