-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
KnownSubType with property value #74
Comments
Hi @Blackbaud-ChristiSchneider It seems that it could be supported by the feature implemented in #60 (not yet released) |
I took a few minutes to out and it didn't work - I'll see if I can find some time to dig in. I'm not sure if I'm misunderstanding how it is intended to work - should we expect that |
And why not simply with: [JsonConverter(typeof(JsonSubtypes), "widget_type")]
[JsonSubtypes.KnownSubType(typeof(HeaderWidget), "HEADER")]
[JsonSubtypes.KnownSubType(typeof(NameWidget), "NAME")]
[JsonSubtypes.KnownSubType(typeof(EmailWidget), "EMAIL")]
public abstract class Widget
{
[JsonProperty("order", Required = Required.Always)]
public int Order { get; set; }
[JsonProperty("widget_type", Required = Required.Always)]
public string WidgetType { get; set; }
} See: https://dotnetfiddle.net/z47EJT (#60 is if you have by example an abstract class |
I confirmed this works with 1.6.0, thanks a bunch! |
Is your feature request related to a problem? Please describe.
I am trying to deserialize an array of items which derive from a base class. These items don't have a
$type
property but do have a similar property by a different name, which is set to a constant string per property type. I am able to useKnownSubTypeWithProperty
to get pretty close but I don't think this solution will continue to work as more subtypes are added.Describe the solution you'd like
An attribute that allows you to specify the type, property name, and property value to look for.
Describe alternatives you've considered
I am currently using
KnownSubTypeWithProperty
to differentiate between the types but two of my types are identical except for thewidget_type
field.EmailWidget
TextWidget
Right now I am differentiating by not having any of the
TextWidget
s that I have specified include therequired
property, but that's not going to work in all cases. I could also make these two types actually be the same class but I think that is a less than ideal solution since all other types have their own class and I'd like to keep it consistent.Additional context
I think this is related to #69 but not quite the same.
*** Source/destination types
This is a subset of the types I have.
*** Source/destination JSON
The text was updated successfully, but these errors were encountered: