kaml icon indicating copy to clipboard operation
kaml copied to clipboard

Content-based polymorphism

Open eisenwave opened this issue 1 year ago • 3 comments

Describe the problem you'd like to solve

I have a YAML configuration that contains values which can be strings or objects such as:

# monolingual value
key1: File
# multilingual value
key2:
  en_US: File
  de_DE: Datei

I couldn't find any way to deserialize this into something like MonolingualString or MultilingualString depending on the structure (more specifics at https://stackoverflow.com/q/78966200/5740428). I would have to specify the type in a !<type> or type: tag, and that's unnecessary.

Besides this use case, one could easily imagine providing RGB colors either as "#ff0000" hex code strings or { r: 255, g: 0, b: 0 } objects.

Describe the solution you'd like

Something similar to https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#content-based-polymorphic-deserialization, i.e. content-based polymorphism.

Describe alternatives you've considered

No response

Additional context

No response

eisenwave avatar Sep 10 '24 08:09 eisenwave

Would what has been proposed in #607 be suitable for your use case?

charleskorn avatar Sep 13 '24 05:09 charleskorn

Yes, that seems sufficient.

eisenwave avatar Sep 14 '24 12:09 eisenwave

Would what has been proposed in #607 be suitable for your use case?

I'm as well in need of a JsonContentPolymorphicSerializer equivalent.

@charleskorn could you please find the time to move forward with the review of the related PRs by @Jojo4GH?

sschuberth avatar Oct 15 '24 14:10 sschuberth