Excluding enum properties with default values in j

2020-04-18 03:59发布

问题:

I have a model for my api response which has a property of type enum. The enum property gets populated in only some scenarios rest of the scenario it gets the default value of 0

output {
prop1: "ABCD"
prop2: 0 //// this is enum property defaulting to zero
}

I tried using the custom jsonconverter but this does not really solve the problem as I can only assign a string value to it.
How can i exclude the enum property from the response if it has the default value.

回答1:

This is resolved doing two things
1. Make the enum property as nullable and initialized it in the constructor to null.
2. Use json serializer setting NullValueHandling = NullValueHandling.Ignore