Excluding specific items in a collection when serializing to JSON

后端 未结 2 993
南旧
南旧 2020-12-03 23:12

I am trying to \"cherry-pick\" which objects in a collection of a specific type I want to serialize.

Example setup:

public class Person
{
    public          


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 23:38

    You should not put the logic into serializer. Instead filter the collection and then serialize it. In this case, you may use something like this

    Person.Courses = Person.Courses.Where(t=> t.ShouldSerialize == false);
    
    return Person;
    

    If you really need to add the contract resolver into your serializer you may do it in your Global.asax.cs (assuming you are using asp.net)

提交回复
热议问题