wivuu / Wivuu.JsonPolymorphism

Adds System.Text.Json serializer support for polymorphism with a discriminator attribute
MIT License
25 stars 3 forks source link

[Feature request] String discriminator #2

Closed toddams closed 3 years ago

toddams commented 3 years ago

Hello there,

First of all, great library!

At the moment library only understands enum as a discriminator. In case of using EntityFramework we have a separate string Property inside a class which holds a discriminator value.

Is it possible to have string discriminator, additional to enum, which we have right now?

Thanks

onionhammer commented 3 years ago

Hi @toddams, thanks for peeking in;

Unfortunately the only way we can automagically match up the discriminator with a concrete type to deserialize to is by knowing an exhaustive list of options, which the enum helps us with. If it wasnt an enum, we would have to effectively do all the work of an enum anyway, with something like [JsonDiscriminator("string1", "string2", "string3")], which IMO would not be super beneficial.

HOWEVER, I think you should be able to store the enum value as a string in your database while still using an enum in code, if you know all the values the string can be.

With EF Core the code would look like this: https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions#the-valueconverter-class