一尘不染

序列化为JSON时排除集合中的特定项目

json

我试图“挑选”要序列化的特定类型的集合中的哪些对象。

设置示例:

public class Person
{
    public string Name { get; set; }

    public List<Course> Courses { get; set; }
}

public class Course
{
    ...

    public bool ShouldSerialize { get; set; }
}

我需要能够排除Person.Courses集合中其中ShouldSerialize为false的所有课程。这需要在ContractResolver内完成-
ShouldSerialize属性是一个示例,在我的实际情况下,可能还有其他条件。我希望不必创建ShouldSerializeCourse(如此处指定:http
://james.newtonking.com/json/help/index.html?topic=html/ConditionalProperties.htm

我似乎找不到在ContractResolver中重写的方法。我将如何处理?


阅读 242

收藏
2020-07-27

共1个答案

一尘不染

我认为您不能使用ContractResolver过滤列表,但是可以使用自定义JsonConverter进行过滤。这是一个例子:

class Program
{
    static void Main(string[] args)
    {
        List<Person> people = new List<Person>
        {
            new Person 
            {
                Name = "John",
                Courses = new List<Course>
                {
                    new Course { Name = "Trigonometry", ShouldSerialize = true },
                    new Course { Name = "History", ShouldSerialize = true },
                    new Course { Name = "Underwater Basket Weaving", ShouldSerialize = false },
                }
            },
            new Person
            {
                Name = "Georgia",
                Courses = new List<Course>
                {
                    new Course { Name = "Spanish", ShouldSerialize = true },
                    new Course { Name = "Pole Dancing", ShouldSerialize = false },
                    new Course { Name = "Geography", ShouldSerialize = true },
                }
            }
        };

        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Converters.Add(new CourseListConverter());
        settings.Formatting = Formatting.Indented;

        string json = JsonConvert.SerializeObject(people, settings);
        Console.WriteLine(json);
    }
}

class CourseListConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(List<Course>));
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, ((List<Course>)value).Where(c => c.ShouldSerialize).ToArray());
    }

    public override bool CanRead
    {
        get { return false; }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

public class Person
{
    public string Name { get; set; }
    public List<Course> Courses { get; set; }
}

public class Course
{
    public string Name { get; set; }
    [JsonIgnore]
    public bool ShouldSerialize { get; set; }
}

输出:

[
  {
    "Name": "John",
    "Courses": [
      {
        "Name": "Trigonometry"
      },
      {
        "Name": "History"
      }
    ]
  },
  {
    "Name": "Georgia",
    "Courses": [
      {
        "Name": "Spanish"
      },
      {
        "Name": "Geography"
      }
    ]
  }
]
2020-07-27