本文介绍了序列化到JSON时不计特殊项目的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摘樱桃在一个特定的类型我想序列化的集合的对象。

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

示例设置:

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是假的。这需要从ContractResolver内完成 - 该ShouldSerialize属性是一个例子,在我的真实场景中,可能有其他的标准。我想preFER不必创建一个ShouldSerializeCourse(按规定此处的 )

我似乎无法找出在ContractResolver覆盖哪个方法。我怎么会去吗?

I can't seem to find out which method to override in the ContractResolver. How would I go about this?

推荐答案

我不认为你可以过滤使用ContractResolver名单,但你可以使用自定义JsonConverter做到这一点。下面是一个例子:

I don't think you can filter a list using a ContractResolver, but you could do it using a custom JsonConverter. Here is an example:

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"
      }
    ]
  }
]

这篇关于序列化到JSON时不计特殊项目的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 12:42