0

I have an object that contains a Func property:

public class Foo {
   public Func<int> DoCalculation {get;init;}
   public string Bar {get;init;}
}

How can I exclude all Func<...> properties in any JSON serialization without using [JsonIgnore] or any other attribute?

Context:

  • System.Text.JSON
  • .net core 6
dbc
  • 104,963
  • 20
  • 228
  • 340
THX-1138
  • 21,316
  • 26
  • 96
  • 160
  • Youb can consider [`[DataMember]`](https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization#what-gets-serialized) However, it depends on your scenario and requirement because you are `using Func` deligate within your class. Thus, need to know more details what exactly you are trying to achieve. – Md Farid Uddin Kiron Mar 02 '23 at 06:23
  • @MdFaridUddinKiron, I'm looking for a solution without using an attribute of any kind. – THX-1138 Mar 03 '23 at 13:44
  • Take a look at [System.Text.Json API is there something like IContractResolver](https://stackoverflow.com/q/58926112). In .NET 7 you will be able to use a [TypeInfo modifier](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/custom-contracts#modifiers) to exclude properties by type. But in .NET 6 this is not available so you will need to write a custom converter or use a 3rd party library like Dahomey.Json. In fact I'd say your question is a duplicate of that one, agree? – dbc Mar 25 '23 at 19:26

0 Answers0