You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an inteface that has [JsonIgnore] attribute on one of its declared properties. I also have a few derived types from it.
The [JsonIgnore] on base interface was respected when serializing until I enabled the polymorphic serialization. Is that by design?
Example:
usingSystem;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;publicclassProgram{//[JsonDerivedType(typeof(Derived))]publicinterfaceIBase{publicstringQuestion{get;}[JsonIgnore]publicintAnswer{get;}}publicrecordDerived:IBase{publicstringQuestion{get;init;}publicintAnswer{get;init;}}publicstaticvoidMain(){varobj=newDerived{Question="The ultimate question to life, the universe, and everything",Answer=42};varserialized=JsonSerializer.Serialize<IBase>(obj);Console.WriteLine(serialized);// expected to NOT contain { "Answer ": 42 }}}
While the [JsonDerivedType] is commented out, the Answer propery is not serialized. But once it is uncommented, the property is serialized. Same behaviour is observed when using class instead of record.
Interesting observation - if abstract record/class is used instead of interface, then [JsonIgnore] on base works even with polymorphic serialization enabled.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an inteface that has
[JsonIgnore]attribute on one of its declared properties. I also have a few derived types from it.The
[JsonIgnore]on base interface was respected when serializing until I enabled the polymorphic serialization. Is that by design?Example:
While the
[JsonDerivedType]is commented out, theAnswerpropery is not serialized. But once it is uncommented, the property is serialized. Same behaviour is observed when using class instead of record.fiddle https://dotnetfiddle.net/TNEE6C
Interesting observation - if abstract record/class is used instead of interface, then
[JsonIgnore]on base works even with polymorphic serialization enabled.Beta Was this translation helpful? Give feedback.
All reactions