JsonSubTypes
JsonSubTypes copied to clipboard
Serialize type discriminator when using attributes too
Hi,
I'm using polymorphic deserialization in the context of generated data classes. The fact that everything is configured through Attributes is really helping in this case because it means the models are self sufficient, they don't need a companion class that allow well configured deserialization. I don't want to have to set the type by hand or anything, but I still need to serialize deserializable JSON though (with the discriminator property serialized) ...
I forked this projet and found that adding the JsonSubtypesByDiscriminatorValueConverter like so achieves what I want :
[JsonConverter(typeof(JsonSubtypesByDiscriminatorValueConverter), typeof(Animal), "type")]
(I basically changed the visibility of the constructor in the fork)
I didn't know at the time the reason for this limitation, but from my experience (2 years of production code), I never experienced any problems with it.
Now I'm years later, and I need to use the fallback type attribute (what a relief that this thing exists). The Attribute isn't working with the above method, though creating a new constructor will achieve what I want.
Describe the solution you'd like
Is there a reason for this class to be strictly internal even though you can get a neat feature using it directly ? Would you consider exposing it correctly (I mean in a "compliant with the API vision" way) ?
With the correct directions, I could file a PR
Describe alternatives you've considered
I would keep my fork ...
Hi @MYDIH
I try to put all that is not "documented api" to internal so that I can have less constraint linked to public api breaking changes.
Could you explain why you're not using:
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.FallBackSubType(typeof(UnknownExpression))]
public class Animal
{
}
Hi and thank you for your time,
Sorry that it wasn't clear enough,
Actually I want to serialize polymorphic JSON with a "type" attribute containing the type. I don't want to have to specify this type in the code, it should be set automatically when a class is of a given type based on the mappings I use with KnownSubType. I would prefer that this field isn't taking part of the class even though it wouldn't be blocking if it wasn't possible. My other requirement is that I want to be able to achieve that using Attributes of my classes/fields for the reason I mentioned before.
So far, when I'm using the documented method (the one you describe), the type isn't serialized along with the class. If I want to serialize it, it seems I have two options from my understanding, use the method where I need to generate a JsonSerializerSettings (which is a no go because I need to use the Attributes approach), and declaring the field type in my class (I figured that one searching the issues).
I tried adding the type field to my classes, but it is not automatically filled with the proper type when serializing. It seems I need to specify it manually. Tell me if I'm wrong of course.
What I did in the fork is creating a new constructor for the class JsonSubtypesByDiscriminatorValueConverter that sets the _serializeDiscriminatorProperty to true. I can confirm that it's working like charm 😄
I hope it makes everything clear enough ...
+1, this functional is very necessary
+1, this functional is very necessary
@manuc66 Any chance a PR will be approved if we change the ctor to become public like MYDIH suggests? I have found myself needing this several times as well.
+1